This PR is for BAEL-6788 (#14563)

This PR aims to add java classes for the HashMap multi values operations with same key
This commit is contained in:
MohamedHelmyKassab 2023-08-12 10:00:00 +03:00 committed by GitHub
parent bb8bb76538
commit 5874d1fd15
2 changed files with 82 additions and 0 deletions

View File

@ -0,0 +1,29 @@
package com.baeldung.hashmapmultivalues;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class MultiValueHashMap<K, V> {
private final HashMap<K, ArrayList<V>> map;
// Constructor
public MultiValueHashMap() {
map = new HashMap<>();
}
public void put(K key, V value) {
map.computeIfAbsent(key, k -> new ArrayList<>()).add(value);
}
public List<V> get(K key) {
return map.getOrDefault(key, new ArrayList<>());
}
public void remove(K key, V value) {
map.computeIfPresent(key, (k, v) -> {
v.remove(value);
return v;
});
}
}

View File

@ -0,0 +1,53 @@
package com.baeldung.hashmapmultivalues;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
public class MultiValueHashMapUnitTest {
@Test
public void given_MultiValueHashMap_whenPuttingAndGettingSingleValue_thenValueIsRetrieved() {
MultiValueHashMap<String, Integer> map = new MultiValueHashMap<>();
map.put("key1", 10);
assertEquals(List.of(10), map.get("key1"));
}
@Test
public void given_MultiValueHashMap_whenPuttingAndGettingMultipleValues_thenAllValuesAreRetrieved() {
MultiValueHashMap<String, String> map = new MultiValueHashMap<>();
map.put("key2", "value1");
map.put("key2", "value2");
map.put("key2", "value3");
assertEquals(List.of("value1", "value2", "value3"), map.get("key2"));
}
@Test
public void given_MultiValueHashMap_whenGettingNonExistentKey_thenEmptyListIsReturned() {
MultiValueHashMap<String, Double> map = new MultiValueHashMap<>();
assertTrue(map.get("nonexistent").isEmpty());
}
@Test
public void given_MultiValueHashMap_whenRemovingValue_thenValueIsSuccessfullyRemoved() {
MultiValueHashMap<Integer, String> map = new MultiValueHashMap<>();
map.put(1, "one");
map.put(1, "uno");
map.put(1, "eins");
map.remove(1, "uno");
assertEquals(List.of("one", "eins"), map.get(1));
}
@Test
public void testRemoveNonExistentValue() {
MultiValueHashMap<Integer, String> map = new MultiValueHashMap<>();
map.put(1, "one");
map.remove(1, "nonexistent");
assertEquals(List.of("one"), map.get(1));
}
}