3 ms·
I recently discovered Java streams. Love it and couldn't notice that example readme could be easily written with streams : List<ProductDisplayInfo> list = Ar
by vincnetas 8y ago
I recently discovered Java streams. Love it and couldn't notice that example readme could be easily written with streams :
List<ProductDisplayInfo> list = Arrays
.asList(products)
.stream()
.filter(p -> p.getCategory() == 2 || p.getCategory() == 5 && isProductUnderDiscount(p))
.map(p -> new ProductDisplayInfo(p))
.collect(Collectors.toList());
https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html https://docs.oracle.com/javase/8/docs/api/java/util/stream/p...
- tytytytytytytyt 8y agoYou should try Scala.
- akdas 8y agoJava streams are great. Having to go from collections to streams (`.stream()`) and back (`.collect(...)`) is annoying, but understandable from a compositional point of view. For fun, you can further reduce the size of the code above (and IMO, make it clearer), but using `Arrays.stream(product)` and a method reference for the `map`: List<ProductDisplayInfo> list = Arrays .stream(products) .filter(p -> p.getCategory() == 2 || p.getCategory() == 5 && isProductUnderDiscount(p)) .map(ProductDisplayInfo::new) .collect(Collectors.toList());
- deleted 8y ago[deleted]