-2

i have 3 maps like that

map1 ={(1,"kamal"),(2,"amal")(3,"nimal")};
map2 ={(1,"gold fish"),(2,"carft")(3,"angel")};
map3 ={(1,"cat"),(2,"dog")(3,"rabit")};

I need add those to one newMap

then i tried

 newMap.puttAll(map1);
 newMap.puttAll(map2);
 newMap.puttAll(map3);

out put is {(1,"cat"),(2,"dog")(3,"rabit")}

map1 and map 2 is replaced by map 3. i need add these 3 maps to newMap.

wanted out put is {(1,"kamal"),(2,"amal")(3,"nimal"),(4,"gold fish"),(5,"carft")(6,"angel"),(7,"cat"),(8,"dog")(9,"rabit")}

Thing is my map has same key how can I solve this

Zcon
  • 153
  • 6
  • 18

4 Answers4

4

Quoting first line of javadoc for Map:

An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.

Quoting javadoc for put():

If the map previously contained a mapping for the key, the old value is replaced by the specified value.

You're getting exactly the result as documented. Last entry for each key wins.


Update

Since you've now said you want result to be:

{(1,"kamal"),(2,"amal")(3,"nimal"),(4,"gold fish"),(5,"carft")(6,"angel"),(7,"cat"),(8,"dog")(9,"rabit")}

You don't seem to care about the key, just the values, so add all values with new incrementing key:

int newKey = 0;
for (String value : map1.values())
    newMap.put(++newKey, value);
for (String value : map2.values())
    newMap.put(++newKey, value);
for (String value : map3.values())
    newMap.put(++newKey, value);
Andreas
  • 154,647
  • 11
  • 152
  • 247
0

Without more information it is hard to give the best answer, but if the keys are 1, 2, 3,..., 9, it would make more sense to use List rather than Map. Then you can use addAll to add all elements from one List to another.

Remember that List indices start at 0 so you would get the first item by doing list.get(0);.

Paul Boddington
  • 37,127
  • 10
  • 65
  • 116
0
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.collections4.Transformer;

@SafeVarargs
private static Map<Integer, String> putAllValuesToMapStartingWithKey(int startKey, Map<Integer, String>... sourceMaps) {
    final Map<Integer, String> destinationMap = new TreeMap<>();
    final AtomicInteger counter = new AtomicInteger(startKey);
    for (Map<Integer, String> sourceMap : sourceMaps) {
        MapUtils.populateMap(destinationMap, sourceMap.values(), new Transformer<String, Integer>() {
            @Override
            public Integer transform(final String input) {
                return counter.getAndIncrement();
            }
        });
    }
    return destinationMap;
}

Main class for testing:

public static void main(String[] args) {
    Map<Integer, String> map1 = new HashMap<Integer, String>() {{
        put(1, "kamal");
        put(2, "amal");
        put(3, "nimal");
    }};
    Map<Integer, String> map2 = new HashMap<Integer, String>() {{
        put(1, "gold fish");
        put(2, "carft");
        put(3, "angel");
    }};
    Map<Integer, String> map3 = new HashMap<Integer, String>() {{
        put(1, "cat");
        put(2, "dog");
        put(3, "rabit");
    }};
    Map<Integer, String> map = putAllValuesToMapStartingWithKey(1, map1, map2, map3);
    System.out.println(map);
}
jctim
  • 260
  • 1
  • 4
-1

What you can do is create a Map of type Map<Integer, List<String>> and add manually.

/*
 *@param HashMap<Integer, ArrayList<String>> The map to be generated 
 *@param HashMap<Integer, String> The map to add
 *@returns HashMap<Integer, ArrayList<String>> The final map after adding the given hashmap to it. 
*/

public HashMap<Integer, ArrayList<String>> addToMap(HashMap<Integer, ArrayList<String>>map, HashMap<Integer, String> mp)
 {
 Iterator it = mp.entrySet().iterator();
 while (it.hasNext()) {
  Map.Entry pair = (Map.Entry)it.next();
 List<String> list;
  if(map.containsKey(pair.getKey())
    list = map.get(pair.getKey());
  else
     List<String> list = new ArrayList();
   list.add(pair.getValue());
   map.put(pair.getKey, list);
 }
return map;
}

If you want the ouput to be with a incremental key, it becomes simple.

Iterate, maintain an iterating key

ArrayList<HashMap<Integer, String>> maps = new ArrayList();
maps.add(map1);
maps.add(map2);

for(int i = 0, index = 1; i < maps.size(); ++i)
{
 Iterator it = maps[i].entrySet().iterator();
 while (it.hasNext()) {
  Map.Entry pair = (Map.Entry)it.next();
  HashMap.put(index , mp.getValue());
  index ++;
  }
}
Uma Kanth
  • 5,659
  • 2
  • 20
  • 41