Merge branch 'eugenp:master' into master
This commit is contained in:
commit
a84a483214
@ -12,6 +12,7 @@ This module contains articles about Apache Kafka.
|
||||
- [Kafka Connect Example with MQTT and MongoDB](https://www.baeldung.com/kafka-connect-mqtt-mongodb)
|
||||
- [Building a Data Pipeline with Flink and Kafka](https://www.baeldung.com/kafka-flink-data-pipeline)
|
||||
- [Exactly Once Processing in Kafka with Java](https://www.baeldung.com/kafka-exactly-once)
|
||||
- [Custom Serializers in Apache Kafka](https://www.baeldung.com/kafka-custom-serializer)
|
||||
|
||||
|
||||
##### Building the project
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -0,0 +1,134 @@
|
||||
package com.baeldung.java_16_features.mapmulti;
|
||||
|
||||
import static java.util.stream.Collectors.toList;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.offset;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.lang3.tuple.ImmutablePair;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class JavaStreamMapMultiUnitTest {
|
||||
|
||||
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")))),
|
||||
new Album("album2", 20, Arrays.asList(new Artist("bill", true, Arrays.asList("label2", "label3")), new Artist("tom", true, Arrays.asList("label2", "label4")))));
|
||||
|
||||
@Test
|
||||
public void givenAListOfintegers_whenMapMulti_thenGetListOfOfEvenDoublesPlusPercentage() {
|
||||
|
||||
List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5);
|
||||
double percentage = .01;
|
||||
List<Double> evenDoubles = integers.stream()
|
||||
.<Double> mapMulti((integer, consumer) -> {
|
||||
if (integer % 2 == 0) {
|
||||
consumer.accept((double) integer * (1 + percentage));
|
||||
}
|
||||
})
|
||||
.collect(toList());
|
||||
|
||||
assertThat(evenDoubles).containsAll(Arrays.asList(2.02D, 4.04D));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAListOfintegers_whenFilterMap_thenGetListOfOfEvenDoublesPlusPercentage() {
|
||||
|
||||
List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5);
|
||||
double percentage = .01;
|
||||
List<Double> evenDoubles = integers.stream()
|
||||
.filter(integer -> integer % 2 == 0)
|
||||
.<Double> map(integer -> ((double) integer * (1 + percentage)))
|
||||
.collect(toList());
|
||||
|
||||
assertThat(evenDoubles).containsAll(Arrays.asList(2.02D, 4.04D));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAListOfintegers_whenMapMultiToDouble_thenGetSumOfEvenNumbersAfterApplyPercentage() {
|
||||
|
||||
List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
|
||||
double percentage = .01;
|
||||
double sum = integers.stream()
|
||||
.mapMultiToDouble((integer, consumer) -> {
|
||||
if (integer % 2 == 0) {
|
||||
consumer.accept(integer * (1 + percentage));
|
||||
}
|
||||
})
|
||||
.sum();
|
||||
|
||||
assertThat(sum).isEqualTo(12.12, offset(0.001d));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAListOfAlbums_whenFlatMap_thenGetListOfArtistAlbumPairs() {
|
||||
|
||||
List<Pair<String, String>> artistAlbum = albums.stream()
|
||||
.flatMap(album -> album.getArtists()
|
||||
.stream()
|
||||
.map(artist -> new ImmutablePair<String, String>(artist.getName(), album.getAlbumName())))
|
||||
.collect(toList());
|
||||
|
||||
assertThat(artistAlbum).contains(new ImmutablePair<String, String>("bob", "album1"), new ImmutablePair<String,
|
||||
String>("tom", "album1"), new ImmutablePair<String, String>("bill", "album2"), new ImmutablePair<String, String>("tom", "album2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAListOfAlbums_whenMapMulti_thenGetListOfPairsOfArtistAlbum() {
|
||||
|
||||
List<Pair<String, String>> artistAlbum = albums.stream()
|
||||
.<Pair<String, String>> mapMulti((album, consumer) -> {
|
||||
for (Artist artist : album.getArtists()) {
|
||||
consumer.accept(new ImmutablePair<String, String>(artist.getName(), album.getAlbumName()));
|
||||
}
|
||||
})
|
||||
.collect(toList());
|
||||
|
||||
assertThat(artistAlbum).contains(new ImmutablePair<String, String>("bob", "album1"), new ImmutablePair<String, String>("tom", "album1"),
|
||||
new ImmutablePair<String, String>("bill", "album2"), new ImmutablePair<String, String>("tom", "album2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAListOfAlbums_whenFlatMap_thenGetListOfArtistAlbumjPairsBelowGivenCost() {
|
||||
|
||||
int upperCost = 9;
|
||||
List<Pair<String, String>> artistAlbum = albums.stream()
|
||||
.flatMap(album -> album.getArtists()
|
||||
.stream()
|
||||
.filter(artist -> upperCost > album.getAlbumCost())
|
||||
.map(artist -> new ImmutablePair<String, String>(artist.getName(), album.getAlbumName())))
|
||||
.collect(toList());
|
||||
|
||||
assertTrue(artistAlbum.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAListOfAlbums_whenMapMulti_thenGetListOfArtistAlbumPairsBelowGivenCost() {
|
||||
|
||||
int upperCost = 9;
|
||||
List<Pair<String, String>> artistAlbum = albums.stream()
|
||||
.<Pair<String, String>> mapMulti((album, consumer) -> {
|
||||
if (album.getAlbumCost() < upperCost) {
|
||||
for (Artist artist : album.getArtists()) {
|
||||
consumer.accept(new ImmutablePair<String, String>(artist.getName(), album.getAlbumName()));
|
||||
}
|
||||
}
|
||||
})
|
||||
.collect(toList());
|
||||
|
||||
assertTrue(artistAlbum.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAListOfAlbums_whenMapMulti_thenGetPairsOfArtistMajorLabelsUsingMethodReference() {
|
||||
|
||||
List<Pair<String, String>> artistLabels = albums.stream()
|
||||
.mapMulti(Album::artistAlbumPairsToMajorLabels)
|
||||
.collect(toList());
|
||||
|
||||
assertThat(artistLabels).contains(new ImmutablePair<String, String>("bob:album1", "label1,label3"), new ImmutablePair<String, String>("tom:album1", "label2,label3"),
|
||||
new ImmutablePair<String, String>("bill:album2", "label2,label3"), new ImmutablePair<String, String>("tom:album2", "label2,label4"));
|
||||
}
|
||||
}
|
@ -11,3 +11,4 @@ This module contains articles about parsing and formatting Java date and time ob
|
||||
- [Display All Time Zones With GMT And UTC in Java](http://www.baeldung.com/java-time-zones)
|
||||
- [Convert between String and Timestamp](https://www.baeldung.com/java-string-to-timestamp)
|
||||
- [Convert String to Date in Java](http://www.baeldung.com/java-string-to-date)
|
||||
- [Format a Milliseconds Duration to HH:MM:SS](https://www.baeldung.com/java-ms-to-hhmmss)
|
||||
|
@ -3,3 +3,4 @@
|
||||
- [The DAO Pattern in Java](https://www.baeldung.com/java-dao-pattern)
|
||||
- [DAO vs Repository Patterns](https://www.baeldung.com/java-dao-vs-repository)
|
||||
- [Difference Between MVC and MVP Patterns](https://www.baeldung.com/mvc-vs-mvp-pattern)
|
||||
- [The DTO Pattern (Data Transfer Object)](https://www.baeldung.com/java-dto-pattern)
|
||||
|
@ -13,3 +13,4 @@ This module contains articles about the Java Persistence API (JPA) in Java.
|
||||
- [Returning an Auto-Generated Id with JPA](https://www.baeldung.com/jpa-get-auto-generated-id)
|
||||
- [How to Return Multiple Entities In JPA Query](https://www.baeldung.com/jpa-return-multiple-entities)
|
||||
- [Defining Unique Constraints in JPA](https://www.baeldung.com/jpa-unique-constraints)
|
||||
- [How to Check Field Existence in MongoDB?](https://www.baeldung.com/mongodb-check-field-exists)
|
||||
|
@ -11,3 +11,4 @@ This module contains articles about Reactor Core.
|
||||
- [How to Convert Mono<List<T\>> Into Flux<T\>](https://www.baeldung.com/java-mono-list-to-flux)
|
||||
- [Project Reactor: map() vs flatMap()](https://www.baeldung.com/java-reactor-map-flatmap)
|
||||
- [What Does Mono.defer() Do?](https://www.baeldung.com/java-mono-defer)
|
||||
- [Handling Exceptions in Project Reactor](https://www.baeldung.com/reactor-exceptions)
|
||||
|
@ -8,6 +8,7 @@ This module contains articles about Spring with Kafka
|
||||
- [Testing Kafka and Spring Boot](https://www.baeldung.com/spring-boot-kafka-testing)
|
||||
- [Monitor the Consumer Lag in Apache Kafka](https://www.baeldung.com/java-kafka-consumer-lag)
|
||||
- [Send Large Messages With Kafka](https://www.baeldung.com/java-kafka-send-large-message)
|
||||
- [Configuring Kafka SSL Using Spring Boot](https://www.baeldung.com/spring-boot-kafka-ssl)
|
||||
|
||||
### Intro
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user