MultiValueMap in Java
Filed in: Java
When you use a Normal Map interface in Java or its implementations there is a restriction that each Key must be unique which implies you can only have 1 Key Value pair stored but what if you have multiple values for each key? In that case you need a MultiValueMap.
There are many public implementations of MultiMap apis you can get and use but I thought it would be fun to write a simple version myself that I control and customize.
To start you need the Basics a class with a get and a put function.
Here is the class the K and V can be set up later when you instantiate the class but represent the Key and Value of a map. Note we have a List here because we have many values for each key. The private Map is set up and hidden from the caller
Next we look at the get which is trivially simple but we don't want the list touched after you return it.
Now lets look at put(key, value) this is straightforward but some explanation follows.
If the item is in the Map we get the trfList or transferList List and update it with an add and put in Map
if it is not in the list then we simply create a newList and add the item and put into Map
To prove this all works here is some code that drives it all the dump() method is a convenience to show it works
The out put is this
And the whole class that you can use as you wish and modify for your own needs is here - you might want to add other methods and Initial Capacity etc…and take out the main method
Java Interview Code Examples
Naive Bayes classification AI algorithm
K-Means Clustering AI algorithm
Equity Derivatives tutorial
Fixed Income tutorial
Java
python
Scala
Investment Banking tutorials
HOME

There are many public implementations of MultiMap apis you can get and use but I thought it would be fun to write a simple version myself that I control and customize.
To start you need the Basics a class with a get and a put function.
Here is the class the K and V can be set up later when you instantiate the class but represent the Key and Value of a map. Note we have a List
public class GenericMultiValueMap {
private Map> map = new TreeMap<>();
Next we look at the get which is trivially simple but we don't want the list touched after you return it.
public List get(K k){
return Collections.unmodifiableList(get(k));
}
Now lets look at put(key, value) this is straightforward but some explanation follows.
If the item is in the Map we get the trfList or transferList List
if it is not in the list then we simply create a newList and add the item and put into Map
public void put(K k,V v) {
List trfList = map.get(k);
if(trfList!=null){
trfList.add(v);
map.put(k, trfList);
}else {
List newList = new ArrayList<>();
newList.add(v);
map.put(k, newList);
}
}
To prove this all works here is some code that drives it all the dump() method is a convenience to show it works
public void dump() {
for(K k : map.keySet()) {
System.out.println(k+ " -> "+ map.get(k)); //Stdout but change to your Logger
}
}
public static void main(String[] args) {
int one = 1;
int two = 2;
String val1 = "1";
String val2 = "2";
GenericMultiValueMap mvMap = new GenericMultiValueMap<>();
mvMap.put(one, val1);
mvMap.put(one, val1);
mvMap.put(two, val2);
mvMap.dump();
}
The out put is this
1 -> [1, 1]
2 -> [2]
And the whole class that you can use as you wish and modify for your own needs is here - you might want to add other methods and Initial Capacity etc…and take out the main method
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class GenericMultiValueMap {
private Map> map = new TreeMap<>();
public void put(K k,V v) {
List trfList = map.get(k);
if(trfList!=null){
trfList.add(v);
map.put(k, trfList);
}else {
List newList = new ArrayList<>();
newList.add(v);
map.put(k, newList);
}
}
public List get(K k){
return Collections.unmodifiableList(get(k));
}
public void dump() {
for(K k : map.keySet()) {
System.out.println(k+ " -> "+ map.get(k)); //Stdout but change to your Logger
}
}
public static void main(String[] args) {
int one = 1;
int two = 2;
String val1 = "1";
String val2 = "2";
GenericMultiValueMap mvMap = new GenericMultiValueMap<>();
mvMap.put(one, val1);
mvMap.put(one, val1);
mvMap.put(two, val2);
mvMap.dump();
}
}
People who enjoyed this article also enjoyed the following:
Java Interview Code Examples
Naive Bayes classification AI algorithm
K-Means Clustering AI algorithm
Equity Derivatives tutorial
Fixed Income tutorial
And the following Trails:
C++Java
python
Scala
Investment Banking tutorials
HOME
