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

순수함수

스트림패러다임을 이해하지 못한 코드

Map<String ,Long> freq = new HashMap<>();
try(Stream<String> words = new Scanner(file).tokens()) {
  words.forEach(word ->{
    freq.merge(word,1L,Long::sum);
  });
} 

개선된 버전

Map<String ,Long> freq;
try(Stream<String> words = new Scanner(file).tokens()) {
  freq=words.collect(Collectors.groupingBy(String::toLowerCase,counting()));
}