|
-
July 30th, 2009, 11:02 PM
#1
LinkedHashMap vs ArrayList.clear()
Hi All, I need a help on my code.
I have put an arraylist as a value in to a map.But when I cleared the arraylist the value of map also removed. and after clear the arraylist when I put same arraylist with new entry and put on map for different key it behave unexpectedly.
please suggest me on this.
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
public class fullTest1
{
public static void main(String[] args)
{
LinkedHashMap<String, List> hashMap = new LinkedHashMap<String, List>();
ArrayList<String> arrlist1 = new ArrayList<String>();
arrlist1.add("a");
arrlist1.add("b");
arrlist1.add("c");
arrlist1.add("d");
hashMap.put("1", arrlist1);
System.out.println("Map Before ArrayList clear:"+hashMap);
arrlist1.clear();
System.out.println("Map After ArrayList clear:"+hashMap);
arrlist1.add("a");
hashMap.put("2", arrlist1);
System.out.println("Map with new Key:"+hashMap);
}
}
-----------
Out Put is:
Map Before ArrayList clear:{1=[a, b, c, d]}
Map After ArrayList clear:{1=[]}
Map with new Key:{1=[a], 2=[a]}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|