Java 8 Stream to determine a maximum count in a text file

  • A+

For my assignment I have to replace for loops with streams that count the frequency of words in a text document, and I am having trouble figuring the TODO part out.

String filename = "SophieSallyJack.txt"; if (args.length == 1) {     filename = args[0]; } Map<String, Integer> wordFrequency = new TreeMap<>();  List<String> incoming = Utilities.readAFile(filename);  wordFrequency =     .map(String::toLowerCase)     .filter(word -> !word.trim().isEmpty())     .collect(Collectors.toMap(word -> word, word -> 1, (a, b) -> a + b, TreeMap::new));                  int maxCnt = 0;  // TODO add a single statement that uses streams to determine maxCnt for (String word : incoming) {     Integer cnt = wordFrequency.get(word);     if (cnt != null) {         if (cnt > maxCnt) {             maxCnt = cnt;         }     } } System.out.print("Words that appear " + maxCnt + " times:"); 

I have tried this:

wordFrequency = incoming.parallelStream().     collect(Collectors.toConcurrentMap(w -> w, w -> 1, Integer::sum)); 

But that is not right and I'm not sure how to incorporate maxCnt into the stream.


Assuming you have all the words extracted from a file in a List<String> this word count for each word can be computed using this approach,

Map<String, Long> wordToCountMap =                 .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); 

The most freequent word can then be computed using the above map like so,

Entry<String, Long> mostFreequentWord = wordToCountMap.entrySet().stream()     .max(Map.Entry.comparingByValue())     .orElse(new AbstractMap.SimpleEntry<>("Invalid", 0l)); 

You may change the above two pipelines together if you wish like this,

Entry<String, Long> mostFreequentWord =     .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))     .entrySet().stream()     .max(Map.Entry.comparingByValue())     .orElse(new AbstractMap.SimpleEntry<>("Invalid", 0l)); 


As per the following discussion it is always good to return an Optional from your computation like so,

Optional<Entry<String, Long>> mostFreequentWord =     .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))     .entrySet().stream()     .max(Map.Entry.comparingByValue()); 


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: