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 &lt; words.length ; i++){
            if (!wordCount.containsKey(words[i])){
                wordCount.put(words[i],1);
            } else{
                wordCount.put(words[i],wordCount.get(words[i])+1);
            }
        }
        Set&lt;Map.Entry&lt;String, Integer&gt;&gt; setOfEntries = wordCount.entrySet();
        Iterator&lt;Map.Entry&lt;String, Integer&gt;&gt; iterator = setOfEntries.iterator();
        while (iterator.hasNext()) {
            Map.Entry&lt;String, Integer&gt; entry = iterator.next();
            Integer value = entry.getValue();
            String key = entry.getKey();
            if ((value.compareTo(Integer.valueOf(10)) &lt;0) || key.length() &lt;4)  {
    
                iterator.remove();
            }
        }
        List&lt;Map.Entry&lt;String, Integer&gt;&gt; list = new ArrayList&lt;&gt;();
        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 preference line -> line.split("[\\p{Punct}\\s]+")use SortedMap/TreeMap in the box or LinkedHashMap to conclude the words as they appear in the entry file, etc.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2