Results 1 to 4 of 4
  1. #1
    emosms is offline Member
    Join Date
    May 2010
    Rep Power

    Default ArrayList of HashMaps

    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>();
    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.
    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 11:04 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power

    Default 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.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power

    Default Re: ArrayList of HashMaps

    Java Code:
                for (int i = 0; i < is.set.length; i++) {
                    HashMap hm = new HashMap<String, Integer>();
    That code there is adding a new HashMap to the 'attCount' List.
    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.

    ** This space for rent **

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

    Default Re: ArrayList of HashMaps

    To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Maps and HashMaps
    By moonah in forum Advanced Java
    Replies: 1
    Last Post: 01-26-2012, 04:22 PM
  2. HashMaps containing JComponents
    By chillin in forum AWT / Swing
    Replies: 3
    Last Post: 05-04-2011, 12:05 AM
  3. hashmaps
    By santa in forum New To Java
    Replies: 6
    Last Post: 05-03-2011, 11:07 PM
  4. Concerning HashMaps
    By hjensen in forum New To Java
    Replies: 7
    Last Post: 10-18-2010, 03:36 PM
  5. HashMaps get key for a specific value
    By andre1011 in forum Advanced Java
    Replies: 2
    Last Post: 03-11-2009, 03:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts