It is bit easy to add multimap in Java 8 with lamda expression with different values as expected. Here is the sample code. You can give a try.
public class TestMultiMap<K, V> {
private final Map<K, Set<V>> multiValueMap = new HashMap<>();
public void put(K key, V value) {
this.multiValueMap.computeIfAbsent(key, k -> new HashSet<>()).add(value);
}
public Set<V> get(K key) {
return this.multiValueMap.getOrDefault(key, Collections.emptySet());
}
}
The computeIfAbsent() method takes A key and a lambda. If that key does not exist, computeIfAbsent() runs and create new HashMap.
The getOrDefault() method returns the value or returns the empty set if key found to be null.
To use this
TestMultiMap<String,Object> testMultiMap = new TestMultiMap<>();
testMultiMap.put("Key 1", new Object());
testMultiMap.put("Key 2", "String 1");
testMultiMap.put("Key 3", 1);