map string list string in java
Map> myMaps = new HashMap >(); for (DataObject item : myList) { if (!myMaps.containsKey(item.getKey())) { myMaps.put(item.getKey(), new ArrayList ()); } myMaps.get(item.getKey()).add(item.getValue()); }
Here is what the above code is Doing:
1. Create a new HashMap
2. Loop through the list of DataObjects
3. If the HashMap doesn’t contain the key, add it
4. Add the value to the list of values for the key