-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLimitAndSkip.java
More file actions
38 lines (33 loc) · 1.32 KB
/
Copy pathLimitAndSkip.java
File metadata and controls
38 lines (33 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class LimitAndSkip {
// Метод Limit позволяет ограничить выборку определенным количеством первых элементов
private static void testLimit() {
Collection<String> collection = Arrays.asList("a1", "a2", "a3", "a1");
// Вернуть первые два элемента
List<String> limit = collection
.stream()
.limit(2)
.collect(Collectors.toList());
System.out.println("limit = " + limit);
// Вернуть два элемента начиная со второго
List<String> fromTo = collection
.stream()
.skip(1)
.limit(2)
.collect(Collectors.toList());
System.out.println("fromTo = " + fromTo);
// вернуть последний элемент коллекции
String last = collection
.stream()
.skip(collection.size() - 1)
.findAny().orElse("1");
System.out.println("last = " + last );
}
public static void main(String[] args) {
testLimit();
}
}