0

Hellow everyone,

I have a populated LinkedHashMap, and I need to insert another key/value to it, but it appears that it replaces it. How can I append it?
Here's the example:

    Map <String, String>invalid = new LinkedHashMap<String, String>();
    invalid.put("111111111111111111111", "Invalid Message # 1");

    Map <String, String>valid = new LinkedHashMap<String, String>();
    valid.put("222222222222222222222", "Valid Message # 2");

    Map <String, String>invalid3 = new LinkedHashMap<String, String>();
    invalid.put("33333333333333333333", "Invalid Message # 3");

    Map <String, Map<String, String>>results = new LinkedHashMap<String, Map<String, String>>();
    results.put("valid", valid);
    results.put("invalid", invalid);
    ...
    // Do some other stuff...
    // now update the invalid Map, but the following appears to replace my existing "invalid" Map
    results.get("invalid").putAll(invalid3);

    System.out.println("size=" + results.size());

    //================ RESULT ================
    size=2

    It should have 3 entries.

Can someone please let me know how can I add an entry to my "invalid" Map into my LinkedHashMap? Thanks.

2
Contributors
1
Reply
8
Views
7 Months
Discussion Span
Last Post by JamesCherrill
0

results has just two entries (see lines 11,12). You can add stuff to the maps that they contain, but there are still only two entries in resultsitself

Try printing the contents of all your maps to see what's going on - at first sight I think your code is doing what you wanted.

Edited by JamesCherrill

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.