Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. package com.scalefocus;
  2.  
  3. import java.util.*;
  4. import java.util.stream.Collectors;
  5.  
  6. public class Main {
  7.  
  8. public static void main(String[] args) {
  9. Scanner sc = new Scanner(System.in);
  10.  
  11. String[] words = sc.nextLine().split(" ");
  12.  
  13. LinkedHashMap<String, Integer> counts = new LinkedHashMap<>();
  14. for (String word : words) {
  15. String lowerWord = word.toLowerCase();
  16. if (!counts.containsKey(lowerWord)) {
  17. counts.put(lowerWord, 1);
  18. } else {
  19. counts.put(lowerWord, counts.get(lowerWord) + 1);
  20. }
  21. }
  22.  
  23.  
  24. // List<Integer> result = new ArrayList<>(); - if used with other type than String
  25. String result = counts.entrySet().stream()
  26. .filter(x -> x.getValue() % 2 != 0)
  27. .map(x->x.getKey()) // .getValue() for word counts
  28. // .collect(Collectors.toList()); - for other type of mappable (used in map) value
  29. .collect(Collectors.joining(", ")); //for strings only
  30. System.out.println(result);
  31. /*ArrayList<String> result = new ArrayList<>();
  32. for (Map.Entry<String, Integer> entry : counts.entrySet()) {
  33. if(entry.getValue() % 2 != 0) {
  34. result.add(entry.getKey());
  35. }
  36. }*/
  37.  
  38. //System.out.println(String.join(", ", result));
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement