Skip to content

Commit a6e8045

Browse files
hugosama1pivovarit
authored andcommitted
StreamEx tutorial (eugenp#2685)
1 parent 66db01d commit a6e8045

3 files changed

Lines changed: 129 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.streamex;
2+
3+
public class Role {
4+
5+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.baeldung.streamex;
2+
3+
import java.util.Arrays;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.Objects;
8+
import java.util.Set;
9+
import java.util.stream.Collectors;
10+
import one.util.streamex.DoubleStreamEx;
11+
import one.util.streamex.EntryStream;
12+
import one.util.streamex.IntStreamEx;
13+
import one.util.streamex.StreamEx;
14+
15+
public class StreamEX {
16+
17+
public static void main(String[] args) {
18+
//Collector shortcut methods (toList, toSet, groupingBy, joining, etc.)
19+
List<User> users = Arrays.asList(
20+
new User("name"), new User(), new User());
21+
users.stream()
22+
.map(User::getName)
23+
.collect(Collectors.toList());
24+
List<String> userNames = StreamEx.of(users)
25+
.map(User::getName)
26+
.toList();
27+
Map<Role, List<User>> role2users = StreamEx.of(users)
28+
.groupingBy(User::getRole);
29+
StreamEx.of(1, 2, 3).joining("; "); // "1; 2; 3"
30+
//Selecting stream elements of specific type
31+
List usersAndRoles = Arrays.asList(new User(), new Role());
32+
List<Role> roles = IntStreamEx.range(usersAndRoles.size())
33+
.mapToObj(usersAndRoles::get)
34+
.select(Role.class)
35+
.toList();
36+
System.out.println(roles);
37+
//adding elements to Stream
38+
List<String> appendedUsers = StreamEx.of(users)
39+
.map(User::getName)
40+
.prepend("(none)")
41+
.append("LAST")
42+
.toList();
43+
System.out.println(appendedUsers);
44+
//Removing unwanted elements and using the stream as Iterable:
45+
for (String line : StreamEx.of(users).map(User::getName)
46+
.nonNull()) {
47+
System.out.println(line);
48+
}
49+
//Selecting map keys by value predicate:
50+
Map<String, Role> nameToRole = new HashMap<>();
51+
nameToRole.put("first", new Role());
52+
nameToRole.put("second", null);
53+
Set<String> nonNullRoles = StreamEx.
54+
ofKeys(nameToRole, Objects::nonNull)
55+
.toSet();
56+
System.out.println(nonNullRoles);
57+
//Operating on key-value pairs:
58+
Map<User, List<Role>> users2roles = transformMap(role2users);
59+
Map<String, String> mapToString = EntryStream.of(users2roles)
60+
.mapKeys(String::valueOf)
61+
.mapValues(String::valueOf)
62+
.toMap();
63+
//Support of byte/char/short/float types:
64+
short[] src = {1, 2, 3};
65+
char[] output = IntStreamEx.of(src)
66+
.map(x -> x * 5)
67+
.toCharArray();
68+
}
69+
70+
public double[] getDiffBetweenPairs(double... numbers) {
71+
return DoubleStreamEx.of(numbers)
72+
.pairMap((a, b) -> b - a).toArray();
73+
}
74+
75+
public static Map<User, List<Role>> transformMap(
76+
Map<Role, List<User>> role2users) {
77+
Map<User, List<Role>> users2roles = EntryStream.of(role2users)
78+
.flatMapValues(List::stream)
79+
.invert()
80+
.grouping();
81+
return users2roles;
82+
}
83+
84+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.streamex;
2+
3+
public class User {
4+
5+
int id;
6+
String name;
7+
Role role = new Role();
8+
9+
public User() {
10+
}
11+
12+
public User(String name) {
13+
this.name = name;
14+
}
15+
16+
public int getId() {
17+
return id;
18+
}
19+
20+
public void setId(int id) {
21+
this.id = id;
22+
}
23+
24+
public String getName() {
25+
return name;
26+
}
27+
28+
public void setName(String name) {
29+
this.name = name;
30+
}
31+
32+
public Role getRole() {
33+
return role;
34+
}
35+
36+
public void setRole(Role role) {
37+
this.role = role;
38+
}
39+
40+
}

0 commit comments

Comments
 (0)