package nilpointer.net;
import java.util.ArrayList;
import java.util.List;
public class Demo {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<Integer>();
numbers.add(5);
numbers.add(2);
numbers.add(11);
numbers.add(-9);
numbers.add(7);
numbers.add(-4);
System.out.println("Limit");
numbers.stream().limit(2).forEach(number -> {
System.out.print(number + " ");
});
System.out.println("\nLimit with Sort");
numbers.stream().sorted((number1, number2) -> number2 - number1).limit(3).forEach(number -> {
System.out.print(number + " ");
});
System.out.println("\nLimit with Sort and Condition");
numbers.stream().filter(p -> p > 0).sorted((number1, number2) -> number2 - number1).limit(2).forEach(number -> {
System.out.print(number + " ");
});
}
}
Limit 2
5 2
Limit with Sort
11 7 5
Limit with Sort and Condition
11 7