I’m trying to copy the keys and values from one map, map1, into another map, map2. The values in map 1 are stored in a set and map 2 should store map1’s values in a list. The keys in each should map the same in both maps.
I could loop through the keys in map1 then add those keys to map 2. And have another inner for loop to add each set’s elements to the list in map2 but I’m not sure if this is a right way to go about it, or even correct.
public static <K, V> void changeSetToList (Map<K, Set<V>> map1, Map<K, List<V>> map2) {
for (Map.entry<K, Set<V>> entry : m1.keys())
for (List<V> l : m1.values())
m2.put(entry.getKey(), l.getValue());
}
I haven’t compiled or tested it yet though. No access to computer.
You can iterate through the
Mapand use new ArrayList(Collections) constructor to create a List out of theSetstored in original Map.. And put it into new Map..And if you want to use a Generic method.. You need to change your method to this: –