|
1 | | -package com.winterbe.java8.samples.stream; |
2 | | - |
3 | | -import java.util.ArrayList; |
4 | | -import java.util.List; |
5 | | -import java.util.Optional; |
6 | | - |
7 | | -/** |
8 | | - * @author Benjamin Winterberg |
9 | | - */ |
10 | | -public class Streams1 { |
11 | | - |
12 | | - public static void main(String[] args) { |
13 | | - |
14 | | - List<String> stringCollection = new ArrayList<>(); |
15 | | - stringCollection.add("ddd2"); |
16 | | - stringCollection.add("aaa2"); |
17 | | - stringCollection.add("bbb1"); |
18 | | - stringCollection.add("aaa1"); |
19 | | - stringCollection.add("bbb3"); |
20 | | - stringCollection.add("ccc"); |
21 | | - stringCollection.add("bbb2"); |
22 | | - stringCollection.add("ddd1"); |
23 | | - |
24 | | - |
25 | | - // filtering |
26 | | - |
27 | | - stringCollection |
28 | | - .stream() |
29 | | - .filter((s) -> s.startsWith("a")) |
30 | | - .forEach(System.out::println); |
31 | | - |
32 | | - // "aaa2", "aaa1" |
33 | | - |
34 | | - |
35 | | - // sorting |
36 | | - |
37 | | - stringCollection |
38 | | - .stream() |
39 | | - .sorted() |
40 | | - .filter((s) -> s.startsWith("a")) |
41 | | - .forEach(System.out::println); |
42 | | - |
43 | | - // "aaa1", "aaa2" |
44 | | - |
45 | | - |
46 | | - // mapping |
47 | | - |
48 | | - stringCollection |
49 | | - .stream() |
50 | | - .map(String::toUpperCase) |
51 | | - .sorted((a, b) -> b.compareTo(a)) |
52 | | - .forEach(System.out::println); |
53 | | - |
54 | | - // "DDD2", "DDD1", "CCC", "BBB3", "BBB2", "AAA2", "AAA1" |
55 | | - |
56 | | - |
57 | | - // matching |
58 | | - |
59 | | - boolean anyStartsWithA = stringCollection |
60 | | - .stream() |
61 | | - .anyMatch((s) -> s.startsWith("a")); |
62 | | - |
63 | | - System.out.println(anyStartsWithA); // true |
64 | | - |
65 | | - boolean allStartsWithA = stringCollection |
66 | | - .stream() |
67 | | - .allMatch((s) -> s.startsWith("a")); |
68 | | - |
69 | | - System.out.println(allStartsWithA); // false |
70 | | - |
71 | | - boolean noneStartsWithZ = stringCollection |
72 | | - .stream() |
73 | | - .noneMatch((s) -> s.startsWith("z")); |
74 | | - |
75 | | - System.out.println(noneStartsWithZ); // true |
76 | | - |
77 | | - |
78 | | - // counting |
79 | | - |
80 | | - long startsWithB = stringCollection |
81 | | - .stream() |
82 | | - .filter((s) -> s.startsWith("b")) |
83 | | - .count(); |
84 | | - |
85 | | - System.out.println(startsWithB); // 3 |
86 | | - |
87 | | - |
88 | | - // reducing |
89 | | - |
90 | | - Optional<String> reduced = |
91 | | - stringCollection |
92 | | - .stream() |
93 | | - .sorted() |
94 | | - .reduce((s1, s2) -> s1 + "#" + s2); |
95 | | - |
96 | | - reduced.ifPresent(System.out::println); |
97 | | - // "aaa1#aaa2#bbb1#bbb2#bbb3#ccc#ddd1#ddd2" |
98 | | - |
99 | | - |
100 | | - } |
101 | | - |
102 | | -} |
| 1 | +package com.winterbe.java8.samples.stream; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Optional; |
| 6 | + |
| 7 | +/** |
| 8 | + * @author Benjamin Winterberg |
| 9 | + */ |
| 10 | +public class Streams1 { |
| 11 | + |
| 12 | + public static void main(String[] args) { |
| 13 | + |
| 14 | + List<String> stringCollection = new ArrayList<>(); |
| 15 | + stringCollection.add("Sriguna"); |
| 16 | + stringCollection.add("aaa2"); |
| 17 | + stringCollection.add("bbb1"); |
| 18 | + stringCollection.add("aaa1"); |
| 19 | + stringCollection.add("bbb3"); |
| 20 | + stringCollection.add("ccc"); |
| 21 | + stringCollection.add("bbb2"); |
| 22 | + stringCollection.add("ddd1"); |
| 23 | + |
| 24 | + |
| 25 | + // filtering |
| 26 | + |
| 27 | + stringCollection |
| 28 | + .stream() |
| 29 | + .filter((s) -> s.startsWith("a")) |
| 30 | + .forEach(System.out::println); |
| 31 | + |
| 32 | + // "aaa2", "aaa1" |
| 33 | + |
| 34 | + |
| 35 | + // sorting |
| 36 | + |
| 37 | + stringCollection |
| 38 | + .stream() |
| 39 | + .sorted() |
| 40 | + .filter((s) -> s.startsWith("a")) |
| 41 | + .forEach(System.out::println); |
| 42 | + |
| 43 | + // "aaa1", "aaa2" |
| 44 | + |
| 45 | + |
| 46 | + // mapping |
| 47 | + |
| 48 | + stringCollection |
| 49 | + .stream() |
| 50 | + .map(String::toUpperCase) |
| 51 | + .sorted((a, b) -> b.compareTo(a)) |
| 52 | + .forEach(System.out::println); |
| 53 | + |
| 54 | + // "DDD2", "DDD1", "CCC", "BBB3", "BBB2", "AAA2", "AAA1" |
| 55 | + |
| 56 | + |
| 57 | + // matching |
| 58 | + |
| 59 | + boolean anyStartsWithA = stringCollection |
| 60 | + .stream() |
| 61 | + .anyMatch((s) -> s.startsWith("a")); |
| 62 | + |
| 63 | + System.out.println(anyStartsWithA); // true |
| 64 | + |
| 65 | + boolean allStartsWithA = stringCollection |
| 66 | + .stream() |
| 67 | + .allMatch((s) -> s.startsWith("a")); |
| 68 | + |
| 69 | + System.out.println(allStartsWithA); // false |
| 70 | + |
| 71 | + boolean noneStartsWithZ = stringCollection |
| 72 | + .stream() |
| 73 | + .noneMatch((s) -> s.startsWith("z")); |
| 74 | + |
| 75 | + System.out.println(noneStartsWithZ); // true |
| 76 | + |
| 77 | + |
| 78 | + // counting |
| 79 | + |
| 80 | + long startsWithB = stringCollection |
| 81 | + .stream() |
| 82 | + .filter((s) -> s.startsWith("b")) |
| 83 | + .count(); |
| 84 | + |
| 85 | + System.out.println(startsWithB); // 3 |
| 86 | + |
| 87 | + |
| 88 | + // reducing |
| 89 | + |
| 90 | + Optional<String> reduced = |
| 91 | + stringCollection |
| 92 | + .stream() |
| 93 | + .sorted() |
| 94 | + .reduce((s1, s2) -> s1 + "#" + s2); |
| 95 | + |
| 96 | + reduced.ifPresent(System.out::println); |
| 97 | + // "aaa1#aaa2#bbb1#bbb2#bbb3#ccc#ddd1#ddd2" |
| 98 | + |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments