Skip to content

Commit 7932bd3

Browse files
authored
Bael 5073 guide to multi map (eugenp#11179)
* Commit source code to branch * BAEL-5065 improvement of groupBy with complex key * Source code for Guide to Stream::mapMulti
1 parent 19a23bb commit 7932bd3

3 files changed

Lines changed: 233 additions & 0 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baeldung.java_16_features.mapmulti;
2+
3+
import java.util.List;
4+
import java.util.function.Consumer;
5+
import java.util.stream.Collectors;
6+
7+
import org.apache.commons.lang3.tuple.ImmutablePair;
8+
import org.apache.commons.lang3.tuple.Pair;
9+
10+
public class Album {
11+
12+
private String albumName;
13+
private int albumCost;
14+
private List<Artist> artists;
15+
16+
Album(String name, int albumCost, List<Artist> artists) {
17+
this.albumName = name;
18+
this.artists = artists;
19+
this.albumCost = albumCost;
20+
}
21+
22+
public void artistAlbumPairsToMajorLabels(Consumer<Pair<String, String>> consumer) {
23+
24+
for (Artist artist : artists) {
25+
if (artist.isAssociatedMajorLabels()) {
26+
String concatLabels = artist.getMajorLabels()
27+
.stream()
28+
.collect(Collectors.joining(","));
29+
consumer.accept(new ImmutablePair<>(artist.getName() + ":" + albumName, concatLabels));
30+
}
31+
}
32+
}
33+
34+
public String getAlbumName() {
35+
return albumName;
36+
}
37+
38+
public int getAlbumCost() {
39+
return albumCost;
40+
}
41+
42+
List<Artist> getArtists() {
43+
return artists;
44+
}
45+
46+
public String toString() {
47+
return albumName;
48+
}
49+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.baeldung.java_16_features.mapmulti;
2+
3+
import java.util.List;
4+
import java.util.Objects;
5+
6+
public class Artist {
7+
8+
private final String name;
9+
private boolean associatedMajorLabels;
10+
private List<String> majorLabels;
11+
12+
Artist(String name, boolean associatedMajorLabels, List<String> majorLabels) {
13+
this.name = name;
14+
this.associatedMajorLabels = associatedMajorLabels;
15+
this.majorLabels = majorLabels;
16+
}
17+
18+
public String getName() {
19+
return name;
20+
}
21+
22+
public boolean isAssociatedMajorLabels() {
23+
return associatedMajorLabels;
24+
}
25+
26+
public List<String> getMajorLabels() {
27+
return majorLabels;
28+
}
29+
30+
@Override
31+
public int hashCode() {
32+
return Objects.hash(name);
33+
}
34+
35+
@Override
36+
public boolean equals(Object obj) {
37+
if (this == obj)
38+
return true;
39+
if (obj == null)
40+
return false;
41+
if (getClass() != obj.getClass())
42+
return false;
43+
Artist other = (Artist) obj;
44+
return Objects.equals(name, other.name);
45+
}
46+
47+
public String toString() {
48+
return name;
49+
}
50+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package com.baeldung.java_16_features.mapmulti;
2+
3+
import static java.util.stream.Collectors.toList;
4+
import static org.assertj.core.api.Assertions.assertThat;
5+
import static org.assertj.core.api.Assertions.offset;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
8+
import java.util.Arrays;
9+
import java.util.List;
10+
11+
import org.apache.commons.lang3.tuple.ImmutablePair;
12+
import org.apache.commons.lang3.tuple.Pair;
13+
import org.junit.jupiter.api.Test;
14+
15+
public class JavaStreamMapMultiUnitTest {
16+
17+
private static final List<Album> albums = Arrays.asList(new Album("album1", 10, Arrays.asList(new Artist("bob", true, Arrays.asList("label1", "label3")), new Artist("tom", true, Arrays.asList("label2", "label3")))),
18+
new Album("album2", 20, Arrays.asList(new Artist("bill", true, Arrays.asList("label2", "label3")), new Artist("tom", true, Arrays.asList("label2", "label4")))));
19+
20+
@Test
21+
public void givenAListOfintegers_whenMapMulti_thenGetListOfOfEvenDoublesPlusPercentage() {
22+
23+
List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5);
24+
double percentage = .01;
25+
List<Double> evenDoubles = integers.stream()
26+
.<Double> mapMulti((integer, consumer) -> {
27+
if (integer % 2 == 0) {
28+
consumer.accept((double) integer * (1 + percentage));
29+
}
30+
})
31+
.collect(toList());
32+
33+
assertThat(evenDoubles).containsAll(Arrays.asList(2.02D, 4.04D));
34+
}
35+
36+
@Test
37+
public void givenAListOfintegers_whenFilterMap_thenGetListOfOfEvenDoublesPlusPercentage() {
38+
39+
List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5);
40+
double percentage = .01;
41+
List<Double> evenDoubles = integers.stream()
42+
.filter(integer -> integer % 2 == 0)
43+
.<Double> map(integer -> ((double) integer * (1 + percentage)))
44+
.collect(toList());
45+
46+
assertThat(evenDoubles).containsAll(Arrays.asList(2.02D, 4.04D));
47+
}
48+
49+
@Test
50+
public void givenAListOfintegers_whenMapMultiToDouble_thenGetSumOfEvenNumbersAfterApplyPercentage() {
51+
52+
List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
53+
double percentage = .01;
54+
double sum = integers.stream()
55+
.mapMultiToDouble((integer, consumer) -> {
56+
if (integer % 2 == 0) {
57+
consumer.accept(integer * (1 + percentage));
58+
}
59+
})
60+
.sum();
61+
62+
assertThat(sum).isEqualTo(12.12, offset(0.001d));
63+
}
64+
65+
@Test
66+
public void givenAListOfAlbums_whenFlatMap_thenGetListOfArtistAlbumPairs() {
67+
68+
List<Pair<String, String>> artistAlbum = albums.stream()
69+
.flatMap(album -> album.getArtists()
70+
.stream()
71+
.map(artist -> new ImmutablePair<String, String>(artist.getName(), album.getAlbumName())))
72+
.collect(toList());
73+
74+
assertThat(artistAlbum).contains(new ImmutablePair<String, String>("bob", "album1"), new ImmutablePair<String,
75+
String>("tom", "album1"), new ImmutablePair<String, String>("bill", "album2"), new ImmutablePair<String, String>("tom", "album2"));
76+
}
77+
78+
@Test
79+
public void givenAListOfAlbums_whenMapMulti_thenGetListOfPairsOfArtistAlbum() {
80+
81+
List<Pair<String, String>> artistAlbum = albums.stream()
82+
.<Pair<String, String>> mapMulti((album, consumer) -> {
83+
for (Artist artist : album.getArtists()) {
84+
consumer.accept(new ImmutablePair<String, String>(artist.getName(), album.getAlbumName()));
85+
}
86+
})
87+
.collect(toList());
88+
89+
assertThat(artistAlbum).contains(new ImmutablePair<String, String>("bob", "album1"), new ImmutablePair<String, String>("tom", "album1"),
90+
new ImmutablePair<String, String>("bill", "album2"), new ImmutablePair<String, String>("tom", "album2"));
91+
}
92+
93+
@Test
94+
public void givenAListOfAlbums_whenFlatMap_thenGetListOfArtistAlbumjPairsBelowGivenCost() {
95+
96+
int upperCost = 9;
97+
List<Pair<String, String>> artistAlbum = albums.stream()
98+
.flatMap(album -> album.getArtists()
99+
.stream()
100+
.filter(artist -> upperCost > album.getAlbumCost())
101+
.map(artist -> new ImmutablePair<String, String>(artist.getName(), album.getAlbumName())))
102+
.collect(toList());
103+
104+
assertTrue(artistAlbum.isEmpty());
105+
}
106+
107+
@Test
108+
public void givenAListOfAlbums_whenMapMulti_thenGetListOfArtistAlbumPairsBelowGivenCost() {
109+
110+
int upperCost = 9;
111+
List<Pair<String, String>> artistAlbum = albums.stream()
112+
.<Pair<String, String>> mapMulti((album, consumer) -> {
113+
if (album.getAlbumCost() < upperCost) {
114+
for (Artist artist : album.getArtists()) {
115+
consumer.accept(new ImmutablePair<String, String>(artist.getName(), album.getAlbumName()));
116+
}
117+
}
118+
})
119+
.collect(toList());
120+
121+
assertTrue(artistAlbum.isEmpty());
122+
}
123+
124+
@Test
125+
public void givenAListOfAlbums_whenMapMulti_thenGetPairsOfArtistMajorLabelsUsingMethodReference() {
126+
127+
List<Pair<String, String>> artistLabels = albums.stream()
128+
.mapMulti(Album::artistAlbumPairsToMajorLabels)
129+
.collect(toList());
130+
131+
assertThat(artistLabels).contains(new ImmutablePair<String, String>("bob:album1", "label1,label3"), new ImmutablePair<String, String>("tom:album1", "label2,label3"),
132+
new ImmutablePair<String, String>("bill:album2", "label2,label3"), new ImmutablePair<String, String>("tom:album2", "label2,label4"));
133+
}
134+
}

0 commit comments

Comments
 (0)