Bael 5073 guide to multi map (#11179)
* Commit source code to branch * BAEL-5065 improvement of groupBy with complex key * Source code for Guide to Stream::mapMulti
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
package com.baeldung.java_16_features.mapmulti;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.commons.lang3.tuple.ImmutablePair;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
|
||||
public class Album {
|
||||
|
||||
private String albumName;
|
||||
private int albumCost;
|
||||
private List<Artist> artists;
|
||||
|
||||
Album(String name, int albumCost, List<Artist> artists) {
|
||||
this.albumName = name;
|
||||
this.artists = artists;
|
||||
this.albumCost = albumCost;
|
||||
}
|
||||
|
||||
public void artistAlbumPairsToMajorLabels(Consumer<Pair<String, String>> consumer) {
|
||||
|
||||
for (Artist artist : artists) {
|
||||
if (artist.isAssociatedMajorLabels()) {
|
||||
String concatLabels = artist.getMajorLabels()
|
||||
.stream()
|
||||
.collect(Collectors.joining(","));
|
||||
consumer.accept(new ImmutablePair<>(artist.getName() + ":" + albumName, concatLabels));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String getAlbumName() {
|
||||
return albumName;
|
||||
}
|
||||
|
||||
public int getAlbumCost() {
|
||||
return albumCost;
|
||||
}
|
||||
|
||||
List<Artist> getArtists() {
|
||||
return artists;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return albumName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.baeldung.java_16_features.mapmulti;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class Artist {
|
||||
|
||||
private final String name;
|
||||
private boolean associatedMajorLabels;
|
||||
private List<String> majorLabels;
|
||||
|
||||
Artist(String name, boolean associatedMajorLabels, List<String> majorLabels) {
|
||||
this.name = name;
|
||||
this.associatedMajorLabels = associatedMajorLabels;
|
||||
this.majorLabels = majorLabels;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public boolean isAssociatedMajorLabels() {
|
||||
return associatedMajorLabels;
|
||||
}
|
||||
|
||||
public List<String> getMajorLabels() {
|
||||
return majorLabels;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Artist other = (Artist) obj;
|
||||
return Objects.equals(name, other.name);
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user