diff --git a/java-collections-maps-3/src/main/java/com/baeldung/map/bytearrays/BytesKey.java b/java-collections-maps-3/src/main/java/com/baeldung/map/bytearrays/BytesKey.java new file mode 100644 index 0000000000..4bdcfe4b06 --- /dev/null +++ b/java-collections-maps-3/src/main/java/com/baeldung/map/bytearrays/BytesKey.java @@ -0,0 +1,28 @@ +package com.baeldung.map.bytearrays; + +import java.util.Arrays; + +public final class BytesKey { + private final byte[] array; + + public BytesKey(byte[] array) { + this.array = array; + } + + public byte[] getArray() { + return array.clone(); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + BytesKey bytesKey = (BytesKey) o; + return Arrays.equals(array, bytesKey.array); + } + + @Override + public int hashCode() { + return Arrays.hashCode(array); + } +} diff --git a/java-collections-maps-3/src/test/java/com/baeldung/map/bytearrays/ByteArrayKeyUnitTest.java b/java-collections-maps-3/src/test/java/com/baeldung/map/bytearrays/ByteArrayKeyUnitTest.java new file mode 100644 index 0000000000..8f5b89e11e --- /dev/null +++ b/java-collections-maps-3/src/test/java/com/baeldung/map/bytearrays/ByteArrayKeyUnitTest.java @@ -0,0 +1,104 @@ +package com.baeldung.map.bytearrays; + +import com.google.common.collect.ImmutableList; +import org.junit.jupiter.api.Test; + +import java.util.Base64; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +class ByteArrayKeyUnitTest { + @Test + void givenPrimitiveByteArrayKey_whenRetrievingFromMap_shouldRetrieveDifferentObjects() { + // given + byte[] key1 = {1, 2, 3}; + byte[] key2 = {1, 2, 3}; + String value1 = "value1"; + String value2 = "value2"; + Map map = new HashMap<>(); + map.put(key1, value1); + map.put(key2, value2); + + // when + String retrievedValue1 = map.get(key1); + String retrievedValue2 = map.get(key2); + String retrievedValue3 = map.get(new byte[]{1, 2, 3}); + + // then + assertThat(retrievedValue1).isEqualTo(value1); + assertThat(retrievedValue2).isEqualTo(value2); + assertThat(retrievedValue3).isNull(); + } + + @Test + void givenEncodedStringKey_whenRetrievingFromMap_shouldRetrieveLastPutObject() { + // given + String key1 = Base64.getEncoder().encodeToString(new byte[]{1, 2, 3}); + String key2 = Base64.getEncoder().encodeToString(new byte[]{1, 2, 3}); + String value1 = "value1"; + String value2 = "value2"; + Map map = new HashMap<>(); + map.put(key1, value1); + map.put(key2, value2); + + // when + String retrievedValue1 = map.get(key1); + String retrievedValue2 = map.get(key2); + + // then + assertThat(key1).isEqualTo(key2); + assertThat(retrievedValue1).isEqualTo(value2); + assertThat(retrievedValue2).isEqualTo(value2); + assertThat(retrievedValue1).isEqualTo(retrievedValue2); + } + + @Test + void givenByteListKey_whenRetrievingFromMap_shouldRetrieveLastPutObject() { + // given + List key1 = ImmutableList.of((byte)1, (byte)2, (byte)3); + List key2 = ImmutableList.of((byte)1, (byte)2, (byte)3); + String value1 = "value1"; + String value2 = "value2"; + Map, String> map = new HashMap<>(); + map.put(key1, value1); + map.put(key2, value2); + + // when + String retrievedValue1 = map.get(key1); + String retrievedValue2 = map.get(key2); + + // then + assertThat(key1).isEqualTo(key2); + assertThat(retrievedValue1).isEqualTo(value2); + assertThat(retrievedValue2).isEqualTo(value2); + assertThat(retrievedValue1).isEqualTo(retrievedValue2); + } + + @Test + void givenCustomWrapperKey_whenRetrievingFromMap_shouldRetrieveLastPutObject() { + // given + BytesKey key1 = new BytesKey(new byte[]{1, 2, 3}); + BytesKey key2 = new BytesKey(new byte[]{1, 2, 3}); + String value1 = "value1"; + String value2 = "value2"; + Map map = new HashMap<>(); + map.put(key1, value1); + map.put(key2, value2); + + // when + String retrievedValue1 = map.get(key1); + String retrievedValue2 = map.get(key2); + String retrievedValue3 = map.get(new BytesKey(new byte[]{1, 2, 3})); + + // then + assertThat(key1).isEqualTo(key2); + assertThat(retrievedValue1).isEqualTo(value2); + assertThat(retrievedValue2).isEqualTo(value2); + assertThat(retrievedValue1).isEqualTo(retrievedValue2); + assertThat(retrievedValue3).isEqualTo(value2); + + } +}