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