Skip to content

Commit f867b5f

Browse files
author
amigoscode
committed
Understanding steams
1 parent a843e96 commit f867b5f

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.amigoscode.examples;
2+
3+
import com.amigoscode.beans.Person;
4+
import com.amigoscode.mockdata.MockData;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.io.IOException;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
import java.util.stream.Collectors;
11+
12+
public class UnderstandingStreams {
13+
14+
@Test
15+
void collect() throws IOException {
16+
List<String> emails = MockData.getPeople()
17+
.stream()
18+
.map(Person::getEmail)
19+
.collect(
20+
ArrayList::new,
21+
ArrayList::add,
22+
ArrayList::addAll
23+
);
24+
emails.forEach(System.out::println);
25+
}
26+
27+
@Test
28+
public void lazy() throws Exception {
29+
System.out.println(
30+
MockData.getCars()
31+
.stream()
32+
.filter(car -> {
33+
System.out.println("filter car " + car);
34+
return car.getPrice() < 10000;
35+
})
36+
.map(car -> {
37+
System.out.println("mapping car " + car);
38+
return car.getPrice();
39+
})
40+
.map(price -> {
41+
System.out.println("mapping price " + price);
42+
return price + (price * .14);
43+
})
44+
.collect(Collectors.toList())
45+
);
46+
}
47+
}

0 commit comments

Comments
 (0)