// Declare and Initialize the Collection
Set<Integer> intSet = new HashSet<Integer>();
// Add Elements
intSet.add(1);
intSet.add(2);
intSet.add(3);
intSet.add(4);
// Use the stream and Collector to Group by Even and Odd
System.out.println(intSet.stream().collect(Collectors.groupingBy(p->((Integer)p)%2))); Prints {0=[2, 4], 1=[1, 3]}
|