Results 1 to 4 of 4
Thread: ArrayList of HashMaps
- 02-23-2012, 10:01 PM #1
Member
- Join Date
- May 2010
- Posts
- 24
- Rep Power
- 0
ArrayList of HashMaps
Hi
When I run this code, it seems that per each index[i] of the ArrayList I have maybe refference to just one and the same HashMap object hm.Java Code:ArrayList attCount = new ArrayList<HashMap>(); if (attCount.isEmpty()) {//populate for (int i = 0; i < is.set.length; i++) { HashMap hm = new HashMap<String, Integer>(); attCount.add(hm); }
I have a list of String[i], that I read in a loop.
---
For each String[i] I want to put a HashMap into ArrayList[i] with the corresponding value of String[i]
and a count integer of occurences of the value in all String[] in the list.
---
But at the end I have the SAME HashMap at each index of ArrayList[i], containing all the values for every index of String[i] and the corresponding correct cont number per distinct value..
---
Stuck on this for few hours. What is the problem with the HashMap?
At each loop repetition I declare a new hashmap:
HashMap hm = new HashMap<String, Integer>();
---
Then, when the ArrayList is full of empty HashMaps, in a similar loop I load them with values from the String[] arrays I read one by one
where into the loop I have
ArrayList[i] = String[i] (actually I get the HashMap and hashmap.put(String[i], integer))
But I dont get it right....
Finally I have the SAME hash map per each index [i]Last edited by emosms; 02-23-2012 at 10:04 PM.
- 02-23-2012, 10:31 PM #2
Re: ArrayList of HashMaps
Can you post a full test program the compiles and executes and shows the problem?
It sounds like you have multiple references to the same hashmap instead of references to separate hashmaps.
- 02-24-2012, 09:37 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: ArrayList of HashMaps
That code there is adding a new HashMap to the 'attCount' List.Java Code:for (int i = 0; i < is.set.length; i++) { HashMap hm = new HashMap<String, Integer>(); attCount.add(hm); }
They will each be different.
So there must be something other than the code you are showing here.Please do not ask for code as refusal often offends.
- 02-24-2012, 01:23 PM #4
Re: ArrayList of HashMaps
To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Maps and HashMaps
By moonah in forum Advanced JavaReplies: 1Last Post: 01-26-2012, 03:22 PM -
HashMaps containing JComponents
By chillin in forum AWT / SwingReplies: 3Last Post: 05-04-2011, 12:05 AM -
hashmaps
By santa in forum New To JavaReplies: 6Last Post: 05-03-2011, 11:07 PM -
Concerning HashMaps
By hjensen in forum New To JavaReplies: 7Last Post: 10-18-2010, 03:36 PM -
HashMaps get key for a specific value
By andre1011 in forum Advanced JavaReplies: 2Last Post: 03-11-2009, 02:30 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks