0

I have a List<Map> which should be of the below syntax:

[{clientName=abcd}, {clientName=defg}]

Previously I had List<Bean> which I want to replace with List<Map>.

Here is my code:

List<Map> clientList=new ArrayList<Map>();
Map<String,String> clientNameMap = new HashMap<String,String>();

    clientNameMap.put("clientName","abcd");
    clientList.add(clientNameMap);

    clientNameMap.put("clientName","defg");
    clientList.add(clientNameMap);

What happens with this code is, I am getting [{clientName=defg}, {clientName=defg}] as the output where, clientName=abcd is replaced by the 2nd value defg. How can I get the expected result which is [{clientName=abcd}, {clientName=defg}]?

Thanks

3 Answers 3

3

You have to re-initialize your Map<> again before adding to List<> because you are changing previous reference for Map<> object and on same key that will change previous object also.

You code should be :

List<Map> clientList=new ArrayList<Map>();
Map<String,String> clientNameMap = new HashMap<String,String>();

clientNameMap.put("clientName","abcd");
clientList.add(clientNameMap);

clientNameMap = new HashMap<String,String>();  //Initialize it again.
clientNameMap.put("clientName","defg");
clientList.add(clientNameMap);  
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks.. :) I will be adding it in a for loop.
re-initializing your Map<> creates new map object so i think instead of re initializing adding different keys for same map as clientName-1,clientName-2 will be good..
@mady your are right, but user requirement is adding it on same key. She also mention that Previously I had List<Bean> which I want to replace with List<Map> It means that key name will same for different object.
1

First read up on Map and List. When you add a Map object (or any other object) to a List object, all you're doing is adding a reference to that object in the List.

It means that if you change the Map contents after adding it to the List object, that will be reflected in the List.

In a Map, moreover, the key has to be unique.

So here you need to create a new Map object before you can add the new value and add that new Map object to the list.

See this other post for details:copying a java hashmap

1 Comment

Ok..so creating the map within a for loop and adding values to it, does the job for me.. Thnx
1

Try below code, Map key should be unique so when you put value to same key n times the value is just replaced for the key, In your code you are replacing the value for same key(clientName) and adding it to list so it is printing the same value which you put in last to map.

List<Map> clientList=new ArrayList<Map>();
Map<String,String> clientNameMap = new HashMap<String,String>();

clientNameMap.put("clientName-1","abcd");
clientList.add(clientNameMap);

clientNameMap.put("clientName-2","defg");
clientList.add(clientNameMap);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.