Skip to content

Commit b587791

Browse files
author
amigoscode
committed
examples
1 parent d2a0883 commit b587791

12 files changed

+320
-23
lines changed

pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@
77
<groupId>com.amigoscode</groupId>
88
<artifactId>streams</artifactId>
99
<version>1.0-SNAPSHOT</version>
10+
<build>
11+
<plugins>
12+
<plugin>
13+
<groupId>org.apache.maven.plugins</groupId>
14+
<artifactId>maven-compiler-plugin</artifactId>
15+
<configuration>
16+
<source>16</source>
17+
<target>16</target>
18+
</configuration>
19+
</plugin>
20+
</plugins>
21+
</build>
1022

1123
<properties>
1224
<maven.compiler.source>17</maven.compiler.source>

src/test/java/com/amigoscode/examples/DistinctAndSets.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,26 @@
44
import org.junit.jupiter.api.Test;
55

66
import java.util.List;
7+
import java.util.Set;
8+
import java.util.stream.Collectors;
9+
10+
import static org.assertj.core.api.Assertions.assertThat;
711

812
public class DistinctAndSets {
913

1014
@Test
1115
public void distinct() throws Exception {
1216
List<Integer> numbers = List.of(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 9, 9, 9);
13-
17+
List<Integer> distinct = numbers.stream().distinct().collect(Collectors.toList());
18+
assertThat(distinct).hasSize(9);
19+
System.out.println(distinct);
1420
}
1521

1622
@Test
1723
public void distinctWithSet() throws Exception {
1824
List<Integer> numbers = List.of(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 9, 9, 9);
19-
25+
Set<Integer> distinct = numbers.stream().collect(Collectors.toSet());
26+
assertThat(distinct).hasSize(9);
27+
System.out.println(distinct);
2028
}
2129
}

src/test/java/com/amigoscode/examples/Filtering.java

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,86 @@
55
import com.amigoscode.mockdata.MockData;
66
import org.junit.jupiter.api.Test;
77

8+
import java.util.Arrays;
89
import java.util.List;
10+
import java.util.OptionalInt;
11+
import java.util.function.Predicate;
12+
import java.util.stream.Collectors;
13+
import java.util.stream.Stream;
914

1015
public class Filtering {
1116

1217
@Test
1318
public void filter() throws Exception {
1419
List<Car> cars = MockData.getCars();
20+
21+
Predicate<Car> carPredicate = car -> car.getPrice() < 20_000.00;
22+
Predicate<Car> yellow = car -> car.getColor().equals("Yellow");
23+
24+
List<Car> carsLessThan20k = cars.stream()
25+
.filter(carPredicate)
26+
.filter(yellow)
27+
.collect(Collectors.toList());
28+
29+
carsLessThan20k.forEach(System.out::println);
1530
}
1631

1732
@Test
1833
public void dropWhile() throws Exception {
19-
List<Car> cars = MockData.getCars();
34+
System.out.println("using filter");
35+
Stream.of(2, 4, 6, 8, 9, 10, 12).filter(n -> n % 2 == 0)
36+
.forEach(n -> System.out.print(n + " "));
37+
System.out.println();
38+
System.out.println("using dropWhile");
39+
Stream.of(2, 4, 6, 8, 9, 10, 12).dropWhile(n -> n % 2 == 0)
40+
.forEach(n -> System.out.print(n + " "));
41+
2042
}
2143

2244
@Test
2345
public void takeWhile() throws Exception {
24-
List<Car> cars = MockData.getCars();
46+
// using filter
47+
System.out.println("using filter");
48+
Stream.of(2, 4, 6, 8, 9, 10, 12).filter(n -> n % 2 == 0)
49+
.forEach(n -> System.out.print(n + " "));
50+
51+
System.out.println();
52+
System.out.println("using take while");
53+
Stream.of(2, 4, 6, 8, 9, 10, 12).takeWhile(n -> n % 2 == 0)
54+
.forEach(n -> System.out.print(n + " "));
2555
}
2656

2757
@Test
2858
public void findFirst() throws Exception {
29-
Integer[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
59+
int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
60+
int result = Arrays.stream(numbers).filter(n -> n == 50)
61+
.findFirst()
62+
.orElse(-1);
63+
System.out.println(result);
64+
3065
}
3166

3267
@Test
3368
public void findAny() throws Exception {
34-
Integer[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
69+
int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 10};
70+
int result = Arrays.stream(numbers).filter(n -> n == 9)
71+
.findAny()
72+
.orElse(-1);
73+
System.out.println(result);
74+
}
75+
76+
@Test
77+
public void allMatch() throws Exception {
78+
int[] even = {2, 4, 6, 8, 10};
79+
boolean allMatch = Arrays.stream(even).allMatch(n -> n % 2 == 0);
80+
System.out.println(allMatch);
81+
}
82+
83+
@Test
84+
public void anyMatch() throws Exception {
85+
int[] evenAndOneOdd = {2, 4, 6, 8, 10, 11};
86+
boolean anyMatch = Arrays.stream(evenAndOneOdd).anyMatch(n -> !(n % 2 == 0));
87+
System.out.println(anyMatch);
3588
}
3689

3790
}

src/test/java/com/amigoscode/examples/GettingStarted.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import org.junit.jupiter.api.Test;
66

77
import java.io.IOException;
8+
import java.util.ArrayList;
89
import java.util.List;
10+
import java.util.stream.Collectors;
911

1012

1113
public class GettingStarted {
@@ -15,11 +17,29 @@ public void imperativeApproach() throws IOException {
1517
// 1. Find people aged less or equal 18
1618
// 2. Then change implementation to find first 10 people
1719
List<Person> people = MockData.getPeople();
20+
List<Person> youngPeople = new ArrayList<>();
21+
int limit = 10;
22+
int counter = 0;
23+
for (Person person : people) {
24+
if (person.getAge() <= 18) {
25+
youngPeople.add(person);
26+
counter++;
27+
if (counter == limit) {
28+
break;
29+
}
30+
}
31+
}
32+
youngPeople.forEach(System.out::println);
1833

1934
}
2035

2136
@Test
2237
public void declarativeApproachUsingStreams() throws Exception {
2338
List<Person> people = MockData.getPeople();
39+
List<Person> youngPeople = people.stream()
40+
.filter(p -> p.getAge() <= 18)
41+
.limit(10)
42+
.collect(Collectors.toList());
43+
youngPeople.forEach(System.out::println);
2444
}
2545
}
Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,52 @@
11
package com.amigoscode.examples;
22

33

4+
import com.amigoscode.beans.Car;
5+
import com.amigoscode.mockdata.MockData;
46
import org.junit.jupiter.api.Test;
57

68
import java.util.List;
9+
import java.util.Map;
10+
import java.util.function.Function;
11+
import java.util.stream.Collectors;
712

813
public class GroupingData {
914

1015
@Test
1116
public void simpleGrouping() throws Exception {
17+
Map<String, List<Car>> map = MockData.getCars()
18+
.stream()
19+
.collect(Collectors.groupingBy(Car::getMake));
20+
map.forEach((s, cars) -> {
21+
System.out.println("Make " + s);
22+
cars.forEach(System.out::println);
23+
System.out.println("---------------------");
24+
});
1225

1326
}
1427

1528
@Test
1629
public void groupingAndCounting() throws Exception {
1730
List<String> names = List.of(
18-
"John",
19-
"John",
20-
"Mariam",
21-
"Alex",
22-
"Mohammado",
23-
"Mohammado",
24-
"Vincent",
25-
"Alex",
26-
"Alex"
31+
"John",
32+
"John",
33+
"Mariam",
34+
"Alex",
35+
"Mohammado",
36+
"Mohammado",
37+
"Vincent",
38+
"Alex",
39+
"Alex"
40+
);
41+
42+
Map<String, Long> map = names.stream()
43+
.collect(Collectors.groupingBy(
44+
Function.identity(),
45+
Collectors.counting())
2746
);
2847

48+
System.out.println(map);
49+
2950
}
3051

3152
}

src/test/java/com/amigoscode/examples/IntStreams.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,38 @@
66
import org.junit.jupiter.api.Test;
77

88
import java.util.List;
9+
import java.util.stream.IntStream;
10+
import java.util.stream.Stream;
911

1012
public class IntStreams {
1113

1214
@Test
1315
public void range() throws Exception {
14-
16+
System.out.println("with fori");
17+
for (int i = 0; i <= 10; i++) {
18+
System.out.println(i);
19+
}
20+
System.out.println("with int stream exclusive");
21+
IntStream.range(0, 10).forEach(System.out::println);
22+
23+
System.out.println("with int stream inclusive");
24+
IntStream.rangeClosed(0, 10).forEach(System.out::println);
1525
}
1626

1727
// Loop through people using IntStream
1828
@Test
1929
public void rangeIteratingLists() throws Exception {
2030
List<Person> people = MockData.getPeople();
31+
IntStream.range(0, people.size())
32+
.forEach(index -> {
33+
System.out.println(people.get(index));
34+
});
2135
}
2236

2337
@Test
2438
public void intStreamIterate() {
25-
39+
IntStream.iterate(0, value -> value + 1)
40+
.limit(11)
41+
.forEach(System.out::println);
2642
}
2743
}

src/test/java/com/amigoscode/examples/JoiningStrings.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,35 @@
33
import org.junit.jupiter.api.Test;
44

55
import java.util.List;
6+
import java.util.stream.Collectors;
67

78
public class JoiningStrings {
89

910
@Test
10-
public void joiningStringsWithStream() throws Exception {
11+
public void joiningStrings() throws Exception {
1112
List<String> names = List.of("anna", "john", "marcos", "helena", "yasmin");
13+
// "Anna, John, Marcos, Helena, Yasmin"
14+
StringBuilder join = new StringBuilder();
15+
16+
for (String name : names) {
17+
join.append(name.substring(0, 1).toUpperCase())
18+
.append(name.substring(1))
19+
.append(", ");
20+
}
21+
22+
System.out.println(join);
23+
System.out.println(join.substring(0, join.length() - 2));
1224
}
1325

1426
@Test
15-
public void joiningStrings() throws Exception {
27+
public void joiningStringsWithStream() throws Exception {
1628
List<String> names = List.of("anna", "john", "marcos", "helena", "yasmin");
29+
// "Anna, John, Marcos, Helena, Yasmin"
30+
String join = names.stream()
31+
.map(name -> name.substring(0, 1).toUpperCase() + name.substring(1))
32+
.collect(Collectors.joining("|"));
33+
System.out.println(join);
34+
1735
}
1836

1937
}

src/test/java/com/amigoscode/examples/MinMax.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,22 @@
22

33
import org.junit.jupiter.api.Test;
44

5+
import java.util.Comparator;
56
import java.util.List;
67

78
public class MinMax {
89

910
@Test
1011
public void min() {
1112
List<Integer> numbers = List.of(1, 2, 3, 100, 23, 93, 99);
13+
Integer min = numbers.stream().min(Comparator.naturalOrder()).get();
14+
System.out.println(min);
1215
}
1316

1417
@Test
1518
public void max() {
1619
List<Integer> numbers = List.of(1, 2, 3, 100, 23, 93, 99);
20+
Integer max = numbers.stream().max(Comparator.naturalOrder()).get();
21+
System.out.println(max);
1722
}
1823
}

0 commit comments

Comments
 (0)