Advertisement
damesova

Word Count [Mimi][JA]

Jun 2nd, 2019
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class _06_WordCount {
  5.     public static void main(String[] args) {
  6.  
  7.         String pathIn1 = ".......";
  8.  
  9.         String pathIn2 = "........";
  10.  
  11.         String pathOut = ".........";
  12.  
  13.  
  14.         Map<String, Integer> map = new LinkedHashMap<>();
  15.  
  16.         try (Scanner sc1 = new Scanner(new FileReader(pathIn1));
  17.              Scanner sc2 = new Scanner(new FileReader(pathIn2));
  18.              PrintWriter pw = new PrintWriter(new FileWriter(pathOut))
  19.  
  20.         ) {
  21.             Arrays
  22.                     .stream(sc1.nextLine()
  23.                             .split("\\s+"))
  24.                     .forEach(e -> map.putIfAbsent(e, 0));
  25.  
  26.             while (sc2.hasNext()) {
  27.                 String w = sc2.next();
  28.                 if (map.containsKey(w)) {
  29.                     map.put(w, map.get(w) + 1);
  30.                 }
  31.             }
  32.  
  33.             map.entrySet()
  34.                     .stream()
  35.                     .sorted((x, y) -> Integer.compare(y.getValue(), x.getValue()))
  36.                     .forEach(e -> pw.println(String.format("%s - %d",
  37.                             e.getKey(),
  38.                             e.getValue())));
  39.  
  40.         } catch (IOException e) {
  41.             e.printStackTrace();
  42.         }
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement