Code modifications by lambda and Streams
-
I've done a program that counts how many times every word is repeated in the file I'm trying to do with lambda and Streams, but it doesn't work.
Map<String, Integer> wordCount = new HashMap<String, Integer>();
String[] words = learn.split(" "); for (int i=0 ; i < words.length ; i++){ if (!wordCount.containsKey(words[i])){ wordCount.put(words[i],1); } else{ wordCount.put(words[i],wordCount.get(words[i])+1); } } Set<Map.Entry<String, Integer>> setOfEntries = wordCount.entrySet(); Iterator<Map.Entry<String, Integer>> iterator = setOfEntries.iterator(); while (iterator.hasNext()) { Map.Entry<String, Integer> entry = iterator.next(); Integer value = entry.getValue(); String key = entry.getKey(); if ((value.compareTo(Integer.valueOf(10)) <0) || key.length() <4) { iterator.remove(); } } List<Map.Entry<String, Integer>> list = new ArrayList<>(); list.addAll(wordCount.entrySet());
How can this piece of code be changed with lambda and Streams
-
- Help
Files.lines
Get a stream in the file.Stream<String>
- Every line to break into words and use
flatMap
for conversion - If necessary, filter the words in length
- Use Collectors.groupingBy + Collectors.counting for a map of the frequency of words
Map<String, Long>
Map<String, Long> wordFreq = Files.lines(Paths.get("input.txt")) // Stream<String> .flatMap(line -> Arrays.stream(line.split("\\s+"))) .filter(word -> word.length() > 2) // игнорировать слова короче 3 символов .collect(Collectors.groupingBy( word -> word, Collectors.counting() ));
Integer frequency can be used
Collectors.summingInt
:// ... .collect(Collectors.groupingBy( word -> word, Collectors.summingInt(word -> 1) // каждому слову отвечает единица )); // Map<String, Integer>
The words could also lead to the same register
word -> word.toLowerCase()
to break not only the gaps, but also the sign of preferenceline -> line.split("[\\p{Punct}\\s]+")
useSortedMap/TreeMap
in the box orLinkedHashMap
to conclude the words as they appear in the entry file, etc.
- Help