46. 스트림에서는 부작용 없는 함수를 사용하라.

무한성장개발자·2025년 9월 25일

package me.whiteship.chapter07.item46;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import java.util.stream.Stream;

import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.*;

// 빈도표 초기화에 스트림을 적절하지 못하게 혹은 적절하게 사용하는 예 (277-279쪽)
public class Freq {
    public static void main(String[] args) throws FileNotFoundException {
        Stream<String> words = Arrays.asList("book", "BooK", "the", "book").stream();

        // 코드 46-1 스트림 패러다임을 이해하지 못한 채 API만 사용했다 - 따라 하지 말 것! (277쪽)
//        Map<String, Long> freq = new HashMap<>();
//        words.forEach(word -> freq.merge(word.toLowerCase(), 1L, Long::sum));

        // 코드 46-2 스트림을 제대로 활용해 빈도표를 초기화한다. (278쪽)
        Map<String, Long> freq = words.collect(groupingBy(String::toLowerCase, counting()));
        System.out.println(freq);

        // 코드 46-3 빈도표에서 가장 흔한 단어 10개를 뽑아내는 파이프라인 (279쪽)
        List<String> topTen = freq.keySet().stream()
                .sorted(comparing(freq::get).reversed())
                .limit(10)
                .collect(toList());

        System.out.println(topTen);
    }
}

외부에 있는 것을 사용하기 때문에 위에꺼는 적절하지 않다.

0개의 댓글