How merge list when combine two hashMap objects in Java

martinixs

I have two HashMaps defined like so:

HashMap<String, List<Incident>> map1 = new HashMap<String, List<Incident>>();
HashMap<String, List<Incident>> map2 = new HashMap<String, List<Incident>>();

Also, I have a 3rd HashMap Object:

HashMap<String, List<Incident>> map3;

and the merge list when combine both.

Chase

In short, you can't. map3 doesn't have the correct types to merge map1 and map2 into it.

However if it was also a HashMap<String, List<Incident>>. You could use the putAll method.

map3 = new HashMap<String, List<Incident>>();
map3.putAll(map1);
map3.putAll(map2);

If you wanted to merge the lists inside the HashMap. You could instead do this.

map3 = new HashMap<String, List<Incident>>();
map3.putAll(map1);
for(String key : map2.keySet()) {
    List<Incident> list2 = map2.get(key);
    List<Incident> list3 = map3.get(key);
    if(list3 != null) {
        list3.addAll(list2);
    } else {
        map3.put(key,list2);
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Merge/combine two objects

From Dev

Java 8: Merge two list of objects by id

From Dev

How do I combine two objects in Java?

From Dev

How to combine/merge/intersect two arrays of objects in one loop?

From Dev

Is there a way to compare two objects in a List and combine their values that is most optimal?(Java)

From Dev

how sort when hashmap value is list of objects by multiple properties java 8

From Dev

how to merge two different List objects into one List and sort them

From Dev

How to combine / merge two regexp?

From Dev

How to combine or merge two sparse vectors in Spark using Java?

From Dev

How to merge List of two objects based on unique id?

From Dev

How to combine two SwiftyJSON objects

From Dev

Combine or merge recursively two list in R

From Dev

How to merge two list of POJOS using stream JAVA 8 modifying some values when necessary

From Dev

How to merge two json objects?

From Dev

how to return a list of objects from arraylist when passed two dates in java?

From Dev

How to merge two java object list at once in java

From Dev

how to Link/Merge/Combine two contacts in android?

From Dev

Java Hashmap two identical objects stored seperate

From Dev

How combine two list into list of list at python?

From Dev

How to merge generic list of objects?

From Dev

Merge two list (java 7)

From Dev

lodash merge and combine objects

From Java

Java Stream: List of objects to HashMap without duplicates

From Dev

Java Stream: List of objects to HashMap without duplicates

From Dev

Filter list of objects without using hashmap java

From Dev

JAVA 8 : HOW TO MERGE TWO LIST USING 2 STREAM

From Dev

How to combine two list in to one with two columns

From Dev

How to combine two javascript FormData objects

From Dev

Java: How to merge two hashmaps with the same Key saving the values of the first hashmap

Related Related

  1. 1

    Merge/combine two objects

  2. 2

    Java 8: Merge two list of objects by id

  3. 3

    How do I combine two objects in Java?

  4. 4

    How to combine/merge/intersect two arrays of objects in one loop?

  5. 5

    Is there a way to compare two objects in a List and combine their values that is most optimal?(Java)

  6. 6

    how sort when hashmap value is list of objects by multiple properties java 8

  7. 7

    how to merge two different List objects into one List and sort them

  8. 8

    How to combine / merge two regexp?

  9. 9

    How to combine or merge two sparse vectors in Spark using Java?

  10. 10

    How to merge List of two objects based on unique id?

  11. 11

    How to combine two SwiftyJSON objects

  12. 12

    Combine or merge recursively two list in R

  13. 13

    How to merge two list of POJOS using stream JAVA 8 modifying some values when necessary

  14. 14

    How to merge two json objects?

  15. 15

    how to return a list of objects from arraylist when passed two dates in java?

  16. 16

    How to merge two java object list at once in java

  17. 17

    how to Link/Merge/Combine two contacts in android?

  18. 18

    Java Hashmap two identical objects stored seperate

  19. 19

    How combine two list into list of list at python?

  20. 20

    How to merge generic list of objects?

  21. 21

    Merge two list (java 7)

  22. 22

    lodash merge and combine objects

  23. 23

    Java Stream: List of objects to HashMap without duplicates

  24. 24

    Java Stream: List of objects to HashMap without duplicates

  25. 25

    Filter list of objects without using hashmap java

  26. 26

    JAVA 8 : HOW TO MERGE TWO LIST USING 2 STREAM

  27. 27

    How to combine two list in to one with two columns

  28. 28

    How to combine two javascript FormData objects

  29. 29

    Java: How to merge two hashmaps with the same Key saving the values of the first hashmap

HotTag

Archive