Assuming I have a TreeMap<String,List<String> first;
and TreeMap<String,String> Origin;
And each key and each value in first has an equivalent key in origin. How can I substitute key and values of first with related value from Origin? For example
first {a = [b,c,d], e = [ f,g,h]}
origin {a=a1,b=b1,c=c1,d=d1,e = e1, g = g1, h=h1}
I need to get this TreeMap DesiredMap {[a1 =b1,c1,d1],[e1 = f1,g1,h1]}
You have to iterate over the entries on your first
TreeMap.
for(Map.Entry<String, List<String>> entry : first.entrySet()) {
For each entry
grab the oldKey
and the oldValues
.
String oldKey= entry.getKey();
List<String> oldValues= entry.getValue();
Create the newKey
String newKey = origin.get(oldKey);
Then iterate over each value s
in oldValues
to get the newValue from the origin
in order to create the List of newValues
List<String> newValues = new ArrayList<>();
for(String s : oldValues) {
newValues.add(origin.get(s));
}
Now that you have the newKey
and the newValues
put them in your result
TreeMap.
result.put(newKey, newValues);
Go to the next entry
and repeat!