BAEL-5350: Java - How to create new Entry (key, value) (#11817)

* BAEL-5350:
1. Added example usage of creating entries with Java itself
2. Added example usage of creating entries with Apache commons collecttions & Guava
3. Custom entry class and its usages

* BAEL-5350:
1. Created unit tests

* BAEL-5350:
1. simplifed assertion for java 9 entry creation

* BAEL-5350:
1. moved into core-java-collections-maps-4 module
2. updated custom entry class and its unit test
This commit is contained in:
Elmar Mammadov
2022-02-26 05:24:07 +01:00
committed by GitHub
parent 10c3299125
commit 816cfd356c
6 changed files with 222 additions and 3 deletions

View File

@@ -0,0 +1,53 @@
package com.baeldung.entries;
import java.util.Map;
import java.util.Objects;
import java.util.StringJoiner;
public class SimpleCustomKeyValue<K, V> implements Map.Entry<K, V> {
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();
}
}

View File

@@ -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<String, String> firstEntry = new AbstractMap.SimpleEntry<>("key1", "value1");
AbstractMap.SimpleEntry<String, String> secondEntry = new AbstractMap.SimpleEntry<>("key2", "value2");
AbstractMap.SimpleEntry<String, String> 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<String, String> firstEntry = new AbstractMap.SimpleImmutableEntry<>("key1", "value1");
AbstractMap.SimpleImmutableEntry<String, String> secondEntry = new AbstractMap.SimpleImmutableEntry<>("key2", "value2");
AbstractMap.SimpleImmutableEntry<String, String> 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<String, String> entry = Map.entry("key", "value");
assertThat(entry.getKey())
.isEqualTo("key");
assertThat(entry.getValue())
.isEqualTo("value");
}
@Test
public void givenEntriesWithApacheCommons_whenVerifying_thenShouldContainKeyValues() {
Map.Entry<String, String> firstEntry = new DefaultMapEntry<>("key1", "value1");
KeyValue<String, String> secondEntry = new DefaultMapEntry<>("key2", "value2");
KeyValue<String, String> thirdEntry = new DefaultMapEntry<>(firstEntry);
KeyValue<String, String> 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<String, String> firstEntry = new UnmodifiableMapEntry<>("key1", "value1");
KeyValue<String, String> secondEntry = new UnmodifiableMapEntry<>("key2", "value2");
KeyValue<String, String> thirdEntry = new UnmodifiableMapEntry<>(firstEntry);
KeyValue<String, String> 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<String, String> firstEntry = Maps.immutableEntry("key1", "value1");
Map.Entry<String, String> secondEntry = Maps.immutableEntry("key2", "value2");
assertThat(Stream.of(firstEntry, secondEntry))
.extracting("key", "value")
.containsExactly(
tuple("key1", "value1"),
tuple("key2", "value2"));
}
}

View File

@@ -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<String, String> firstEntry = new SimpleCustomKeyValue<>("key1", "value1");
Map.Entry<String, String> secondEntry = new SimpleCustomKeyValue<>("key2", "value2");
secondEntry.setValue("different value");
Map<String, String> map = Map.ofEntries(firstEntry, secondEntry);
assertThat(map)
.isEqualTo(ImmutableMap.<String,String>builder()
.put("key1", "value1")
.put("key2", "different value")
.build());
}
}