how to merge the list of values to the same Key in hashmap?
if I use the above logic I'm getting below result as an output {Adam=[[Subject, ComputerScience], [Subject, ComputerScience]]}
But I have to merge like the below result, is it possible to append the list of values to the same key?
{Adam=[Subject, ComputerScience,Subject, ComputerScience]}
public class DemoMap {
public static void main(String[] args) {
HashMap<String, ArrayList<String>> tmeMap = new HashMap<>();
HashMap<String, ArrayList<Object>> mngrMap = new HashMap<>();
ArrayList<Object> emailcontent = new ArrayList<>();
ArrayList<String> mngrList1 = new ArrayList<>();
mngrList1.add("Jay");
mngrList1.add("Aaron");
tmeMap.put("Adam", mngrList1);
//Adam is Senior Manager who has the list of managers under him
emailcontent.add("Subject");
emailcontent.add("ComputerScience");
mngrMap.put("Jay", emailcontent);
mngrMap.put("Aaron", emailcontent);
//Each manager will have the email content
ArrayList<Object> collectionOfManagerContent = new ArrayList<>();
for (Map.Entry<String,ArrayList<Object>> emailEntry : mngrMap.entrySet()) {
collectionOfManagerContent.add(emailEntry.getValue());
}
//our Target is to show the manager's content to Senior Project manager
HashMap<String, ArrayList<Object>> tmeEmailMap1 = new HashMap<>();
for (Map.Entry<String,ArrayList<String>> emailEntry : tmeMap.entrySet()) {
emailEntry.getValue();
tmeEmailMap1.put(emailEntry.getKey(), collectionOfManagerContent);
}
System.out.println(tmeEmailMap1.toString());
}
}
Use addAll() to add all elements of ArrayList into another ArrayList
for (Map.Entry<String,ArrayList<Object>> emailEntry : mngrMap.entrySet()) {
collectionOfManagerContent.addAll(emailEntry.getValue());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.