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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.