There are following difference between limit() and skip() in java 8.
limit(n) :- this method is used to return the Stream of first n elements.
example:- here in the list we use the limit,to print only size of 5 elements.
skip(n) :- this method is used to skip the first n elements and process the remaining elements.
example : in skip case we are not print the first 4 elements, print start from 5th position elements.
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class LimitSkipExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(2, 4, 10, 5, 20, 15, 25, 6, 12, 56);
List<Integer> lmt = numbers.stream().limit(5).collect(Collectors.toList());
System.out.println("using limit method :- " + lmt);
List<Integer> skp = numbers.stream().skip(4).collect(Collectors.toList());
System.out.println("using skip method :- " + skp);
}
}
Output :-
using limit method :- [2, 4, 10, 5, 20]
using skip method :- [20, 15, 25, 6, 12, 56]