Skip to content

Commit fc33a88

Browse files
committed
Refine the documentation of the intermediate operations.
1 parent 6b670e3 commit fc33a88

File tree

6 files changed

+97
-135
lines changed

6 files changed

+97
-135
lines changed
Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,20 @@
1-
# Data Set
2-
3-
```java
4-
List<Footballer> getFootballers() {
5-
return List.of(
6-
new Footballer("Messi", 32, Gender.MALE, List.of("CF", "CAM", "RF")),
7-
new Footballer("Ibrahim", 28, Gender.MALE, List.of("CF", "CAM", "LF")),
8-
new Footballer("Arthur", 23, Gender.MALE, List.of("CM", "CAM")),
9-
new Footballer("Cristiano Ronaldo", 27, Gender.MALE, List.of("GK")),
10-
new Footballer("Surinder", 20, Gender.MALE, List.of("CM", "CDM")),
11-
new Footballer("Jennifer", 29, Gender.FEMALE, List.of("CF", "CAM")),
12-
new Footballer("Jana", 17, Gender.FEMALE, List.of("CB")),
13-
new Footballer("Alexia", 25, Gender.FEMALE, List.of("CAM", "RF", "LF"))
14-
);
15-
}
16-
```
17-
181
# Peek:
19-
20-
It performs the specified operation on each element of the stream and returns a new stream which can be used further.
2+
`Peek` is used to perform an action on each element of the stream without modifying it.
3+
It is mainly used for debugging or logging while the stream is being processed.
214

225
```java
23-
long malePlayerCount = footballerList.stream()
24-
.filter(footballer -> footballer.getGender().equals(Gender.MALE))
25-
.sorted(Comparator.comparing(Footballer::getAge))
26-
.peek(footballer -> {
27-
System.out.println("footballer = " + footballer);
28-
})
29-
.count();
6+
List<Integer> result = List.of(1, 2, 3, 4).stream()
7+
.peek(n -> System.out.println("Processing: " + n))
8+
.map(n -> n * 2)
9+
.toList();
3010

31-
System.out.
32-
33-
println("malePlayerCount = "+malePlayerCount);
34-
// prints
35-
// footballer = Footballer{name='Surinder', age=20, gender=MALE, positions=[CM, CDM]}
36-
// footballer = Footballer{name='Arthur', age=23, gender=MALE, positions=[CM, CAM]}
37-
// footballer = Footballer{name='Cristiano Ronaldo', age=27, gender=MALE, positions=[GK]}
38-
// footballer = Footballer{name='Ibrahim', age=28, gender=MALE, positions=[CF, CAM, LF]}
39-
// footballer = Footballer{name='Messi', age=32, gender=MALE, positions=[CF, CAM, RF]}
40-
// malePlayerCount = 5
11+
System.out.println(result);
12+
```
13+
Output:
14+
```java
15+
Processing: 1
16+
Processing: 2
17+
Processing: 3
18+
Processing: 4
19+
[2, 4, 6, 8]
4120
```
42-
Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,17 @@
1-
# Data Set
2-
3-
```java
4-
List<Footballer> getFootballers() {
5-
return List.of(
6-
new Footballer("Messi", 32, Gender.MALE, List.of("CF","CAM", "RF")),
7-
new Footballer("Ibrahim", 28, Gender.MALE, List.of("CF", "CAM", "LF")),
8-
new Footballer("Arthur", 23, Gender.MALE, List.of("CM", "CAM")),
9-
new Footballer("Cristiano Ronaldo", 27, Gender.MALE, List.of("GK")),
10-
new Footballer("Surinder", 20, Gender.MALE, List.of("CM", "CDM")),
11-
new Footballer("Jennifer", 29, Gender.FEMALE, List.of("CF", "CAM")),
12-
new Footballer("Jana", 17, Gender.FEMALE, List.of("CB")),
13-
new Footballer("Alexia", 25, Gender.FEMALE, List.of("CAM", "RF", "LF"))
14-
);
15-
}
16-
```
17-
181
# Limit:
192

20-
The `limit` method is used to reduce the size of the stream.
3+
It restricts the stream to the first `n` elements and ignores the rest.
214

225
```java
23-
List<Footballer> twoMaleFootballers = footballerList.stream()
24-
.filter(footballer -> footballer.getGender().equals(Gender.MALE))
25-
.limit(2)
26-
.collect(Collectors.toList());
27-
//prints
28-
//twoMaleFootballers = [Footballer{name='Messi', age=32, gender=MALE, positions=[CF, CAM, RF]}, Footballer{name='Ibrahim', age=28, gender=MALE, positions=[CF, CAM, LF]}]
6+
List<Integer> numbers = List.of(1, 2, 3, 4, 5);
297

8+
List<Integer> result = numbers.stream()
9+
.limit(3)
10+
.toList();
11+
12+
System.out.println(result);
13+
```
14+
Output:
15+
```java
16+
[1, 2, 3]
3017
```
Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,16 @@
1-
# Data Set
2-
3-
```java
4-
List<Footballer> getFootballers() {
5-
return List.of(
6-
new Footballer("Messi", 32, Gender.MALE, List.of("CF","CAM", "RF")),
7-
new Footballer("Ibrahim", 28, Gender.MALE, List.of("CF", "CAM", "LF")),
8-
new Footballer("Arthur", 23, Gender.MALE, List.of("CM", "CAM")),
9-
new Footballer("Cristiano Ronaldo", 27, Gender.MALE, List.of("GK")),
10-
new Footballer("Surinder", 20, Gender.MALE, List.of("CM", "CDM")),
11-
new Footballer("Jennifer", 29, Gender.FEMALE, List.of("CF", "CAM")),
12-
new Footballer("Jana", 17, Gender.FEMALE, List.of("CB")),
13-
new Footballer("Alexia", 25, Gender.FEMALE, List.of("CAM", "RF", "LF"))
14-
);
15-
}
16-
```
17-
181
# Skip:
2+
It ignores the first `n` elements of a stream and processes the remaining elements.
193

204
```java
21-
List<Footballer> sortByGenderAndNameSkipping5 = footballerList.stream()
22-
.sorted(Comparator.comparing(Footballer::getGender).thenComparing(Footballer::getName))
23-
.skip(5)
24-
.collect(Collectors.toList());
25-
//prints (prettified)
26-
//sortByGenderAndNameSkipping5 = [
27-
//Footballer{name='Alexia', age=25, gender=FEMALE, positions=[CAM, RF, LF]},
28-
//Footballer{name='Jana', age=17, gender=FEMALE, positions=[CB]},
29-
//Footballer{name='Jennifer', age=29, gender=FEMALE, positions=[CF, CAM]}]
30-
//Note : See the above mentioned Sorted example for comparison
5+
List<Integer> numbers = List.of(1, 2, 3, 4, 5);
316

7+
List<Integer> result = numbers.stream()
8+
.skip(2)
9+
.toList();
3210

11+
System.out.println(result);
12+
```
13+
Output:
14+
```java
15+
[3, 4, 5]
3316
```
Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,19 @@
1-
# Data Set
1+
# Take While:
2+
3+
It keeps elements from the start of the stream as long as a condition is true.
4+
When the condition becomes false, the stream stops processing further elements.
25

36
```java
4-
List<Footballer> getFootballers() {
5-
return List.of(
6-
new Footballer("Messi", 32, Gender.MALE, List.of("CF", "CAM", "RF")),
7-
new Footballer("Ibrahim", 28, Gender.MALE, List.of("CF", "CAM", "LF")),
8-
new Footballer("Arthur", 23, Gender.MALE, List.of("CM", "CAM")),
9-
new Footballer("Cristiano Ronaldo", 27, Gender.MALE, List.of("GK")),
10-
new Footballer("Surinder", 20, Gender.MALE, List.of("CM", "CDM")),
11-
new Footballer("Jennifer", 29, Gender.FEMALE, List.of("CF", "CAM")),
12-
new Footballer("Jana", 17, Gender.FEMALE, List.of("CB")),
13-
new Footballer("Alexia", 25, Gender.FEMALE, List.of("CAM", "RF", "LF"))
14-
);
15-
}
16-
```
7+
List<Integer> numbers = List.of(1, 2, 3, 6, 2, 4);
178

18-
# Take While:
9+
List<Integer> result = numbers.stream()
10+
.takeWhile(n -> n < 5)
11+
.toList();
1912

13+
System.out.println(result);
2014
```
21-
//Normal Filter
22-
List<Integer> filteredList = Stream.of(2, 4, 6, 8, 9, 10, 11, 12)
23-
.filter(n -> n % 2 == 0)
24-
.collect(Collectors.toList());
25-
System.out.println("filteredList = " + filteredList);
26-
//prints filteredList = [2, 4, 6, 8, 10, 12]
27-
28-
//Take, While ...
29-
List<Integer> takeAWhile = Stream.of(2, 4, 6, 8, 9, 10, 11, 12)
30-
.takeWhile(n -> n % 2 == 0)
31-
.collect(Collectors.toList());
32-
33-
System.out.println("takeAWhile = " + takeAWhile);
34-
//prints takeAWhile = [2, 4, 6, 8]
3515

16+
Output:
17+
```java
18+
[1, 2, 3]
3619
```
Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,15 @@
1-
# Data Set
2-
3-
```java
4-
List<Footballer> getFootballers() {
5-
return List.of(
6-
new Footballer("Messi", 32, Gender.MALE, List.of("CF", "CAM", "RF")),
7-
new Footballer("Ibrahim", 28, Gender.MALE, List.of("CF", "CAM", "LF")),
8-
new Footballer("Arthur", 23, Gender.MALE, List.of("CM", "CAM")),
9-
new Footballer("Cristiano Ronaldo", 27, Gender.MALE, List.of("GK")),
10-
new Footballer("Surinder", 20, Gender.MALE, List.of("CM", "CDM")),
11-
new Footballer("Jennifer", 29, Gender.FEMALE, List.of("CF", "CAM")),
12-
new Footballer("Jana", 17, Gender.FEMALE, List.of("CB")),
13-
new Footballer("Alexia", 25, Gender.FEMALE, List.of("CAM", "RF", "LF"))
14-
);
15-
}
16-
```
17-
181
# Drop While:
2+
It skips elements from the beginning of a stream as long as a given condition is true.
3+
Once the condition becomes false, it stops dropping elements and processes the rest of the stream normally.
194

205
```
216
List<Integer> dropWhile = Stream.of(2, 4, 6, 8, 9, 10, 11, 12)
227
.dropWhile(n -> n % 2 == 0)
238
.collect(Collectors.toList());
249
2510
System.out.println("dropWhile = " + dropWhile);
26-
//prints dropWhile = [9, 10, 11, 12]
11+
```
12+
Output:
13+
```java
14+
[9, 10, 11, 12]
2715
```
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
## MapMulti:
2+
Instead of creating a new stream for each element, it directly sends the generated values to the next step in the stream.
3+
This makes it simpler and sometimes more efficient when you want to expand or flatten data.
4+
5+
6+
### Example 1:
7+
8+
```java
9+
List<String> words = List.of("Hi", "Java");
10+
11+
List<Character> result = words.stream()
12+
.<Character>mapMulti((word, consumer) -> {
13+
for (char c : word.toCharArray()) {
14+
consumer.accept(c);
15+
}
16+
})
17+
.toList();
18+
19+
System.out.println(result);
20+
```
21+
Output:
22+
```java
23+
[H, i, J, a, v, a]
24+
```
25+
### Example 2:
26+
27+
````java
28+
List<Integer> numbers = List.of(1, 2, 3, 4, 5);
29+
30+
List<Integer> result = numbers.stream()
31+
.<Integer>mapMulti((n, consumer) -> {
32+
if (n % 2 == 0) {
33+
consumer.accept(n);
34+
}
35+
})
36+
.toList();
37+
````
38+
39+
Output:
40+
```java
41+
[1, 2, 3, 4]
42+
```
43+

0 commit comments

Comments
 (0)