Demonstrate Java 8 Streams

master
Hammy 4 years ago
parent da187675a3
commit ec9fc74ce2

@ -11,16 +11,19 @@ public class StreamProducts {
List<StreamProduct> streamProducts = new ArrayList<>(); List<StreamProduct> streamProducts = new ArrayList<>();
streamProducts.add(door); streamProducts.add(door);
streamProducts.add(floorPanel);
streamProducts.add(window); streamProducts.add(window);
streamProducts.add(floorPanel); streamProducts.add(floorPanel);
streamProducts.add(window);
namesOfLightProductsWeightSortedLoop(streamProducts); namesOfLightProductsWeightSortedLoop(streamProducts);
namesOfLightProductsWeightSortedLoopStreamed(streamProducts); namesOfLightProductsWeightSortedLoopStreamed(streamProducts);
} }
private static void namesOfLightProductsWeightSortedLoopStreamed(List<StreamProduct> streamProducts) { private static void namesOfLightProductsWeightSortedLoopStreamed(List<StreamProduct> streamProducts) {
streamProducts.stream()
.filter(streamProduct -> streamProduct.getWeight() < 30)
.sorted(comparingInt(StreamProduct::getWeight))
.map(StreamProduct::getName)
.forEach(System.out::println);
} }

Loading…
Cancel
Save