I have data like below:
Key value
----- ------
car toyota
car bmw
car honda
fruit apple
fruit banana
computer acer
computer asus
computer ibm
...
(Each row of above data is an object with fields "key" and "value", all in one List List<DataObject>
)
I would like to construct the data to a Map<String, List<String>>
like following:
"car" : ["toyota", "bmw", "honda"]
"fruit" : ["apple","banana"]
"computer" : ["acer","asus","ibm"]
How to achieve above Map
structure from the data objects?
******Besides******
I am more interested in using pure JDK provided classes or interfaces to achieve the result instead of using external library. Any help?
Map<String, List<String>> myMaps = new HashMap<String, List<String>>();
for (DataObject item : myList) {
if (!myMaps.containsKey(item.getKey())) {
myMaps.put(item.getKey(), new ArrayList<String>());
}
myMaps.get(item.getKey()).add(item.getValue());
}