Modern Java 8+ Features · The Stream API
Describe what you want from a list as a pipeline. Watch each price travel through filter and map one at a time, and see that nothing runs until collect.
import java.util.List;
import java.util.stream.Collectors;
public class StreamBasicsDemo {
public static void main(String[] args) {
List<Integer> prices = List.of(12, 45, 7, 30);
List<Integer> withTax = prices.stream()
.filter(p -> p >= 10) // keep prices of 10 or more
.map(p -> p + p / 10) // add 10% tax
.collect(Collectors.toList()); // gather the results into a list
System.out.println(withTax + " from " + prices);
}
}List<Integer> withTax = prices.stream()..map(p -> p + p / 10) // add 10% tax.lambda$main$0(), with p = 12. It gets its own variables on the stack.lambda$main$1(), with p = 45. It gets its own variables on the stack. lambda$main$0() has finished, so its variables are gone.lambda$main$0(), with p = 7. It gets its own variables on the stack. lambda$main$1() has finished, so its variables are gone.lambda$main$1() has finished, so its variables are gone.withTax.[13, 49, 33] from [12, 45, 7, 30].