Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class _06_WordCount {
- public static void main(String[] args) {
- String pathIn1 = ".......";
- String pathIn2 = "........";
- String pathOut = ".........";
- Map<String, Integer> map = new LinkedHashMap<>();
- try (Scanner sc1 = new Scanner(new FileReader(pathIn1));
- Scanner sc2 = new Scanner(new FileReader(pathIn2));
- PrintWriter pw = new PrintWriter(new FileWriter(pathOut))
- ) {
- Arrays
- .stream(sc1.nextLine()
- .split("\\s+"))
- .forEach(e -> map.putIfAbsent(e, 0));
- while (sc2.hasNext()) {
- String w = sc2.next();
- if (map.containsKey(w)) {
- map.put(w, map.get(w) + 1);
- }
- }
- map.entrySet()
- .stream()
- .sorted((x, y) -> Integer.compare(y.getValue(), x.getValue()))
- .forEach(e -> pw.println(String.format("%s - %d",
- e.getKey(),
- e.getValue())));
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement