From 070e687c073ecab939923ca63b72a4bab3410d00 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Wed, 1 Sep 2021 21:57:28 +0800 Subject: [PATCH 1/7] Update README.md --- persistence-modules/java-jpa-3/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/persistence-modules/java-jpa-3/README.md b/persistence-modules/java-jpa-3/README.md index c024d7c540..d517c55d7a 100644 --- a/persistence-modules/java-jpa-3/README.md +++ b/persistence-modules/java-jpa-3/README.md @@ -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) From 38595a7e508140b270e54da7ea64d3f262d3ecdb Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Wed, 1 Sep 2021 22:04:37 +0800 Subject: [PATCH 2/7] Update README.md --- patterns/design-patterns-architectural/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/patterns/design-patterns-architectural/README.md b/patterns/design-patterns-architectural/README.md index a8a5a98b88..bb11eea5e1 100644 --- a/patterns/design-patterns-architectural/README.md +++ b/patterns/design-patterns-architectural/README.md @@ -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) From 41884317913f63920feb05e7111f5d3aca910aad Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Wed, 1 Sep 2021 22:06:22 +0800 Subject: [PATCH 3/7] Update README.md --- reactor-core/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/reactor-core/README.md b/reactor-core/README.md index f7c36f5897..a4ced49096 100644 --- a/reactor-core/README.md +++ b/reactor-core/README.md @@ -11,3 +11,4 @@ This module contains articles about Reactor Core. - [How to Convert Mono> Into Flux](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) From 1debf64e6fd994f90a9f17001785d3f4bb0404e4 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Wed, 1 Sep 2021 22:07:46 +0800 Subject: [PATCH 4/7] Update README.md --- spring-kafka/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-kafka/README.md b/spring-kafka/README.md index 2b71beaac9..0be741b393 100644 --- a/spring-kafka/README.md +++ b/spring-kafka/README.md @@ -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 From 54d42ee7b4cf864be805416348571b37dc910990 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Wed, 1 Sep 2021 22:10:12 +0800 Subject: [PATCH 5/7] Update README.md --- core-java-modules/core-java-datetime-string/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-modules/core-java-datetime-string/README.md b/core-java-modules/core-java-datetime-string/README.md index cf00bdeb1f..aac123a884 100644 --- a/core-java-modules/core-java-datetime-string/README.md +++ b/core-java-modules/core-java-datetime-string/README.md @@ -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) From 707972a384bf229fd75f1cc07d005610a5de4457 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Thu, 2 Sep 2021 16:18:52 +0800 Subject: [PATCH 6/7] Update README.md --- apache-kafka/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apache-kafka/README.md b/apache-kafka/README.md index 5e724f95b6..3e817b2fa6 100644 --- a/apache-kafka/README.md +++ b/apache-kafka/README.md @@ -12,7 +12,8 @@ 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 -You can build the project from the command line using: *mvn clean install*, or in an IDE. \ No newline at end of file +You can build the project from the command line using: *mvn clean install*, or in an IDE. From 9bcb0c4b1b66c0ee2845dc765c1674f42d410b2f Mon Sep 17 00:00:00 2001 From: mbarriola <85458535+mbarriola@users.noreply.github.com> Date: Thu, 2 Sep 2021 06:55:08 -0400 Subject: [PATCH 7/7] 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 --- .../java_16_features/mapmulti/Album.java | 49 +++++++ .../java_16_features/mapmulti/Artist.java | 50 +++++++ .../mapmulti/JavaStreamMapMultiUnitTest.java | 134 ++++++++++++++++++ 3 files changed, 233 insertions(+) create mode 100644 core-java-modules/core-java-16/src/main/java/com/baeldung/java_16_features/mapmulti/Album.java create mode 100644 core-java-modules/core-java-16/src/main/java/com/baeldung/java_16_features/mapmulti/Artist.java create mode 100644 core-java-modules/core-java-16/src/test/java/com/baeldung/java_16_features/mapmulti/JavaStreamMapMultiUnitTest.java diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/java_16_features/mapmulti/Album.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/java_16_features/mapmulti/Album.java new file mode 100644 index 0000000000..fa7ec02307 --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/java_16_features/mapmulti/Album.java @@ -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 artists; + + Album(String name, int albumCost, List artists) { + this.albumName = name; + this.artists = artists; + this.albumCost = albumCost; + } + + public void artistAlbumPairsToMajorLabels(Consumer> 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 getArtists() { + return artists; + } + + public String toString() { + return albumName; + } +} diff --git a/core-java-modules/core-java-16/src/main/java/com/baeldung/java_16_features/mapmulti/Artist.java b/core-java-modules/core-java-16/src/main/java/com/baeldung/java_16_features/mapmulti/Artist.java new file mode 100644 index 0000000000..b3b9b7aa10 --- /dev/null +++ b/core-java-modules/core-java-16/src/main/java/com/baeldung/java_16_features/mapmulti/Artist.java @@ -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 majorLabels; + + Artist(String name, boolean associatedMajorLabels, List majorLabels) { + this.name = name; + this.associatedMajorLabels = associatedMajorLabels; + this.majorLabels = majorLabels; + } + + public String getName() { + return name; + } + + public boolean isAssociatedMajorLabels() { + return associatedMajorLabels; + } + + public List 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; + } +} diff --git a/core-java-modules/core-java-16/src/test/java/com/baeldung/java_16_features/mapmulti/JavaStreamMapMultiUnitTest.java b/core-java-modules/core-java-16/src/test/java/com/baeldung/java_16_features/mapmulti/JavaStreamMapMultiUnitTest.java new file mode 100644 index 0000000000..0ab1a95ed0 --- /dev/null +++ b/core-java-modules/core-java-16/src/test/java/com/baeldung/java_16_features/mapmulti/JavaStreamMapMultiUnitTest.java @@ -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 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 integers = Arrays.asList(1, 2, 3, 4, 5); + double percentage = .01; + List evenDoubles = integers.stream() + . 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 integers = Arrays.asList(1, 2, 3, 4, 5); + double percentage = .01; + List evenDoubles = integers.stream() + .filter(integer -> integer % 2 == 0) + . map(integer -> ((double) integer * (1 + percentage))) + .collect(toList()); + + assertThat(evenDoubles).containsAll(Arrays.asList(2.02D, 4.04D)); + } + + @Test + public void givenAListOfintegers_whenMapMultiToDouble_thenGetSumOfEvenNumbersAfterApplyPercentage() { + + List 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> artistAlbum = albums.stream() + .flatMap(album -> album.getArtists() + .stream() + .map(artist -> new ImmutablePair(artist.getName(), album.getAlbumName()))) + .collect(toList()); + + assertThat(artistAlbum).contains(new ImmutablePair("bob", "album1"), new ImmutablePair("tom", "album1"), new ImmutablePair("bill", "album2"), new ImmutablePair("tom", "album2")); + } + + @Test + public void givenAListOfAlbums_whenMapMulti_thenGetListOfPairsOfArtistAlbum() { + + List> artistAlbum = albums.stream() + .> mapMulti((album, consumer) -> { + for (Artist artist : album.getArtists()) { + consumer.accept(new ImmutablePair(artist.getName(), album.getAlbumName())); + } + }) + .collect(toList()); + + assertThat(artistAlbum).contains(new ImmutablePair("bob", "album1"), new ImmutablePair("tom", "album1"), + new ImmutablePair("bill", "album2"), new ImmutablePair("tom", "album2")); + } + + @Test + public void givenAListOfAlbums_whenFlatMap_thenGetListOfArtistAlbumjPairsBelowGivenCost() { + + int upperCost = 9; + List> artistAlbum = albums.stream() + .flatMap(album -> album.getArtists() + .stream() + .filter(artist -> upperCost > album.getAlbumCost()) + .map(artist -> new ImmutablePair(artist.getName(), album.getAlbumName()))) + .collect(toList()); + + assertTrue(artistAlbum.isEmpty()); + } + + @Test + public void givenAListOfAlbums_whenMapMulti_thenGetListOfArtistAlbumPairsBelowGivenCost() { + + int upperCost = 9; + List> artistAlbum = albums.stream() + .> mapMulti((album, consumer) -> { + if (album.getAlbumCost() < upperCost) { + for (Artist artist : album.getArtists()) { + consumer.accept(new ImmutablePair(artist.getName(), album.getAlbumName())); + } + } + }) + .collect(toList()); + + assertTrue(artistAlbum.isEmpty()); + } + + @Test + public void givenAListOfAlbums_whenMapMulti_thenGetPairsOfArtistMajorLabelsUsingMethodReference() { + + List> artistLabels = albums.stream() + .mapMulti(Album::artistAlbumPairsToMajorLabels) + .collect(toList()); + + assertThat(artistLabels).contains(new ImmutablePair("bob:album1", "label1,label3"), new ImmutablePair("tom:album1", "label2,label3"), + new ImmutablePair("bill:album2", "label2,label3"), new ImmutablePair("tom:album2", "label2,label4")); + } +} \ No newline at end of file