fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.stream.Collectors;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11.  
  12. private static final class Inner {
  13.  
  14. public Inner(String id, int type) {
  15. setId(id); setType(type);
  16. }
  17.  
  18. String id;
  19. String getId() { return id;}
  20. void setId(String id) { this.id=id; }
  21. int type;
  22. int getType() { return type; }
  23. void setType(int type) { this.type = type; }
  24. }
  25.  
  26. public static void main (String[] args) throws java.lang.Exception {
  27. ArrayList<Inner> list = new ArrayList<>();
  28. list.add(new Inner("1", 2));
  29. list.add(new Inner("2", 3));
  30. list.add(new Inner("3", 3));
  31. list.add(new Inner("4", 3));
  32. list.add(new Inner("5", 2));
  33. list.add(new Inner("6", 1));
  34. // 1 item of type 1, 2 of type 2 and 3 of type 3, so the expected result is {1=1, 2=2, 3=3}
  35. System.out.println(list.stream().collect(
  36. Collectors.groupingBy(Inner::getType, Collectors.counting())
  37. ));
  38. }
  39. }
Success #stdin #stdout 0.14s 2184192KB
stdin
Standard input is empty
stdout
{1=1, 2=2, 3=3}