diff --git a/core-java-modules/core-java-collections-maps-4/pom.xml b/core-java-modules/core-java-collections-maps-4/pom.xml index be467dd57b..e5afd87bbe 100644 --- a/core-java-modules/core-java-collections-maps-4/pom.xml +++ b/core-java-modules/core-java-collections-maps-4/pom.xml @@ -15,6 +15,16 @@ + + org.apache.commons + commons-collections4 + ${commons-collections4.version} + + + com.google.guava + guava + ${guava.version} + junit junit @@ -37,12 +47,23 @@ + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + ${maven.compiler.source} + ${maven.compiler.target} + + + UTF-8 - 1.8 - 1.8 + 1.9 + 1.9 \ No newline at end of file diff --git a/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/entries/SimpleCustomKeyValue.java b/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/entries/SimpleCustomKeyValue.java new file mode 100644 index 0000000000..b2925cd508 --- /dev/null +++ b/core-java-modules/core-java-collections-maps-4/src/main/java/com/baeldung/entries/SimpleCustomKeyValue.java @@ -0,0 +1,53 @@ +package com.baeldung.entries; + +import java.util.Map; +import java.util.Objects; +import java.util.StringJoiner; + +public class SimpleCustomKeyValue implements Map.Entry { + + private final K key; + private V value; + + public SimpleCustomKeyValue(K key, V value) { + this.key = key; + this.value = value; + } + + @Override + public K getKey() { + return key; + } + + @Override + public V getValue() { + return value; + } + + @Override + public V setValue(V value) { + return this.value = value; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + SimpleCustomKeyValue that = (SimpleCustomKeyValue) o; + return Objects.equals(key, that.key) && Objects.equals(value, that.value); + } + + @Override + public int hashCode() { + return Objects.hash(key, value); + } + + @Override + public String toString() { + return new StringJoiner(", ", SimpleCustomKeyValue.class.getSimpleName() + "[", "]").add("key=" + key).add("value=" + value).toString(); + } +} diff --git a/core-java-modules/core-java-collections-maps-4/src/test/java/com/baeldung/entries/EntriesExampleUnitTest.java b/core-java-modules/core-java-collections-maps-4/src/test/java/com/baeldung/entries/EntriesExampleUnitTest.java new file mode 100644 index 0000000000..954a4a4f22 --- /dev/null +++ b/core-java-modules/core-java-collections-maps-4/src/test/java/com/baeldung/entries/EntriesExampleUnitTest.java @@ -0,0 +1,113 @@ +package com.baeldung.entries; + +import com.google.common.collect.Maps; +import org.apache.commons.collections4.KeyValue; +import org.apache.commons.collections4.keyvalue.DefaultMapEntry; +import org.apache.commons.collections4.keyvalue.UnmodifiableMapEntry; +import org.junit.jupiter.api.Test; + +import java.util.AbstractMap; +import java.util.Map; +import java.util.stream.Stream; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.tuple; + +public class EntriesExampleUnitTest { + + @Test + public void givenEntries_whenVerifying_thenShouldContainKeyValues() { + AbstractMap.SimpleEntry firstEntry = new AbstractMap.SimpleEntry<>("key1", "value1"); + AbstractMap.SimpleEntry secondEntry = new AbstractMap.SimpleEntry<>("key2", "value2"); + AbstractMap.SimpleEntry thirdEntry = new AbstractMap.SimpleEntry<>(firstEntry); + thirdEntry.setValue("a different value"); + + assertThat(Stream.of(firstEntry, secondEntry, thirdEntry)) + .extracting("key", "value") + .containsExactly( + tuple("key1", "value1"), + tuple("key2", "value2"), + tuple("key1", "a different value")); + } + + @Test + public void givenImmutableEntries_whenVerifying_thenShouldContainKeyValues() { + AbstractMap.SimpleImmutableEntry firstEntry = new AbstractMap.SimpleImmutableEntry<>("key1", "value1"); + AbstractMap.SimpleImmutableEntry secondEntry = new AbstractMap.SimpleImmutableEntry<>("key2", "value2"); + AbstractMap.SimpleImmutableEntry thirdEntry = new AbstractMap.SimpleImmutableEntry<>(firstEntry); + + assertThat(Stream.of(firstEntry, secondEntry, thirdEntry)) + .extracting("key", "value") + .containsExactly( + tuple("key1", "value1"), + tuple("key2", "value2"), + tuple("key1", "value1")); + } + + @Test + public void givenImmutableEntryUsingJava9_whenVerifying_thenShouldContainKeyValues() { + Map.Entry entry = Map.entry("key", "value"); + + assertThat(entry.getKey()) + .isEqualTo("key"); + assertThat(entry.getValue()) + .isEqualTo("value"); + } + + + @Test + public void givenEntriesWithApacheCommons_whenVerifying_thenShouldContainKeyValues() { + Map.Entry firstEntry = new DefaultMapEntry<>("key1", "value1"); + KeyValue secondEntry = new DefaultMapEntry<>("key2", "value2"); + + KeyValue thirdEntry = new DefaultMapEntry<>(firstEntry); + KeyValue fourthEntry = new DefaultMapEntry<>(secondEntry); + + firstEntry.setValue("a different value"); + + assertThat(firstEntry) + .extracting("key", "value") + .containsExactly("key1", "a different value"); + + assertThat(Stream.of(secondEntry, thirdEntry, fourthEntry)) + .extracting("key", "value") + .containsExactly( + tuple("key2", "value2"), + tuple("key1", "value1"), + tuple("key2", "value2")); + } + + @Test + public void givenImmutableEntriesWithApacheCommons_whenVerifying_thenShouldContainKeyValues() { + Map.Entry firstEntry = new UnmodifiableMapEntry<>("key1", "value1"); + KeyValue secondEntry = new UnmodifiableMapEntry<>("key2", "value2"); + + KeyValue thirdEntry = new UnmodifiableMapEntry<>(firstEntry); + KeyValue fourthEntry = new UnmodifiableMapEntry<>(secondEntry); + + assertThat(firstEntry) + .extracting("key", "value") + .containsExactly("key1", "value1"); + + assertThat(Stream.of(secondEntry, thirdEntry, fourthEntry)) + .extracting("key", "value") + .containsExactly( + tuple("key2", "value2"), + tuple("key1", "value1"), + tuple("key2", "value2")); + } + + + @Test + public void givenImmutableEntriesWithGuava_whenVerifying_thenShouldContainKeyValues() { + Map.Entry firstEntry = Maps.immutableEntry("key1", "value1"); + Map.Entry secondEntry = Maps.immutableEntry("key2", "value2"); + + assertThat(Stream.of(firstEntry, secondEntry)) + .extracting("key", "value") + .containsExactly( + tuple("key1", "value1"), + tuple("key2", "value2")); + } + +} diff --git a/core-java-modules/core-java-collections-maps-4/src/test/java/com/baeldung/entries/SimpleCustomKeyValueUnitTest.java b/core-java-modules/core-java-collections-maps-4/src/test/java/com/baeldung/entries/SimpleCustomKeyValueUnitTest.java new file mode 100644 index 0000000000..d055bb1653 --- /dev/null +++ b/core-java-modules/core-java-collections-maps-4/src/test/java/com/baeldung/entries/SimpleCustomKeyValueUnitTest.java @@ -0,0 +1,31 @@ +package com.baeldung.entries; + +import com.google.common.collect.ImmutableMap; +import org.junit.jupiter.api.Test; + +import java.util.Map; +import java.util.stream.Stream; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.tuple; + +class SimpleCustomKeyValueUnitTest { + + + @Test + public void givenModifiableEntries_whenVerifying_thenShouldContainKeyValues() { + Map.Entry firstEntry = new SimpleCustomKeyValue<>("key1", "value1"); + + Map.Entry secondEntry = new SimpleCustomKeyValue<>("key2", "value2"); + secondEntry.setValue("different value"); + + Map map = Map.ofEntries(firstEntry, secondEntry); + + assertThat(map) + .isEqualTo(ImmutableMap.builder() + .put("key1", "value1") + .put("key2", "different value") + .build()); + } + +} \ No newline at end of file diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml index a25cf11454..60319b4de4 100644 --- a/core-java-modules/pom.xml +++ b/core-java-modules/pom.xml @@ -38,7 +38,6 @@ core-java-collections-maps core-java-collections-maps-2 core-java-collections-maps-3 - core-java-collections-maps-4 core-java-concurrency-2 core-java-concurrency-advanced core-java-concurrency-advanced-2 diff --git a/pom.xml b/pom.xml index cab9a34f02..3138b5c610 100644 --- a/pom.xml +++ b/pom.xml @@ -1329,6 +1329,7 @@ core-java-modules/core-java-collections-set + core-java-modules/core-java-collections-maps-4 core-java-modules/core-java-date-operations-1 core-java-modules/core-java-datetime-conversion core-java-modules/core-java-datetime-string @@ -1387,6 +1388,7 @@ core-java-modules/core-java-collections-set + core-java-modules/core-java-collections-maps-4 core-java-modules/core-java-date-operations-1 core-java-modules/core-java-datetime-conversion core-java-modules/core-java-datetime-string