diff --git a/Java Streams/src/EnterTheCollector.java b/Java Streams/src/EnterTheCollector.java index 1bd1cf7..1a28b6b 100644 --- a/Java Streams/src/EnterTheCollector.java +++ b/Java Streams/src/EnterTheCollector.java @@ -1,7 +1,11 @@ import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; import static java.util.Comparator.comparingInt; +import static java.util.stream.Collectors.counting; +import static java.util.stream.Collectors.groupingBy; public class EnterTheCollector { public static void main(String[] args) { @@ -22,5 +26,32 @@ public class EnterTheCollector { .filter(product -> product.getWeight() < 30) .sorted(comparingInt(StreamProduct::getWeight)) .forEach(System.out::println); + + // Printing out list + List lightProducts = streamProducts + .stream() + .filter(streamProduct -> streamProduct.getWeight() < 30) + .sorted(comparingInt(StreamProduct::getWeight)) + .collect(Collectors.toList()); + + System.out.println(lightProducts); + + // Mapping the name of the products to product(s) itself + Map> lightProducts1 = streamProducts + .stream() + .filter(streamProduct -> streamProduct.getWeight() < 30) + .sorted(comparingInt(StreamProduct::getWeight)) + .collect(groupingBy(StreamProduct::getName)); + + System.out.println(lightProducts1); + + // Using 1 or more collectors on the data + Map lightProducts2 = streamProducts + .stream() + .filter(streamProduct -> streamProduct.getWeight() < 30) + .sorted(comparingInt(StreamProduct::getWeight)) + .collect(groupingBy(StreamProduct::getName, counting())); + + System.out.println(lightProducts2); } }