Results 1 to 9 of 9
  1. #1
    galxion123 is offline Member
    Join Date
    Aug 2012
    Posts
    11
    Rep Power
    0

    Default [Solved]Java - HashMap - problem with values..

    Hi,
    I am trying to read number values from a large text file and put it into a hashmap.. (It is basically a graph..)
    The values are first read into a ArrayList<String[]>, now I need to group the nodes(edges).Displaying the arraylist gives me an output of:
    Java Code:
    1 1 
    1 2 
    1 5 
    2 13019 
    2 47648
    I am using a HashMap to map each edges to their vertices..

    Code:
    Java Code:
    public static void group(List<String[]> p) {
            HashMap<Integer, ArrayList<Integer>> lst = new HashMap<Integer, ArrayList<Integer>>();
            ArrayList<Integer> row = new ArrayList<Integer>();
            
            for (int i = 1; i <= p.size(); i++) {
                
                if (lst.containsKey(getElement(p, i, 1))) {
                    row.add(getElement(p, i, 2));
                } else if (!lst.containsKey(getElement(p, i, 1))) {
                    row.clear();
                    row.add(getElement(p, i, 2));
                }
                //System.out.println(row);
                lst.put(getElement(p, i, 1), row);
            }
    The output statement at the end of the loop gives the correct list i.e
    [a]
    [a,b]
    [a,b,c]

    Where the last list one is to be added to the HashMap..

    But the final output of the hashmap has all values as the value of the last key...

    What am I doing wrong here?..

    Thanks in advance..
    Last edited by galxion123; 07-25-2013 at 02:59 PM.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,580
    Rep Power
    5

    Default Re: Java - HashMap - problem with values..

    Before you ask for help, try sprinkling your code with System.out.println() statements to print out what your code is doing. Hopefully that way you'll figure it out yourself and you'll have learned ten times more than when someone debugs it for you.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    mkarthik90 is offline Member
    Join Date
    Feb 2012
    Posts
    62
    Rep Power
    0

    Default Re: Java - HashMap - problem with values..

    Is it that your order is missing in HashMap?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Java - HashMap - problem with values..

    Quote Originally Posted by mkarthik90 View Post
    Is it that your order is missing in HashMap?
    Order?

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

  5. #5
    galxion123 is offline Member
    Join Date
    Aug 2012
    Posts
    11
    Rep Power
    0

    Default Re: Java - HashMap - problem with values..

    Quote Originally Posted by gimbal2 View Post
    Before you ask for help, try sprinkling your code with System.out.println() statements to print out what your code is doing. Hopefully that way you'll figure it out yourself and you'll have learned ten times more than when someone debugs it for you.
    Well, before I edited and posted here, there was about 6 output statements in that method telling me everything, but unfortunately since I was testing it on a txt file with 5 million rows, it bugged me out..
    So now I am trying it on a simpler graph..
    Updated code with more output statements:
    Java Code:
        public static void groupVertices(List<String[]> p) {
            HashMap<Integer, ArrayList<Integer>> lst = new HashMap<Integer, ArrayList<Integer>>();
            ArrayList<Integer> row = new ArrayList<Integer>();
            
            for (int i = 1; i <= p.size(); i++) {
                
                if (lst.containsKey(getElement(p, i, 1))) {      
                    row.add(getElement(p, i, 2));
                
                } else if (!lst.containsKey(getElement(p, i, 1))) {   
                    row.clear();
                    row.add(getElement(p, i, 2));
                
                }
                System.out.println("Adding Row to key: "+getElement(p, i, 1)+" Row: "+row);
                lst.put(getElement(p, i, 1), row);
    
            }
            dispHash(lst);
            System.out.println("Value for the key '2': "+lst.get(2));
        }
    the file that I am testing it on contains the following:
    Java Code:
    1 4
    2 8
    3 6
    4 7
    5 2
    6 9
    7 1
    8 5
    8 6
    9 7
    9 3
    The output I get:

    Java Code:
    length of array:11
    Adding Row to key: 1 Row: [4]
    Adding Row to key: 2 Row: [8]
    Adding Row to key: 3 Row: [6]
    Adding Row to key: 4 Row: [7]
    Adding Row to key: 5 Row: [2]
    Adding Row to key: 6 Row: [9]
    Adding Row to key: 7 Row: [1]
    Adding Row to key: 8 Row: [5]
    Adding Row to key: 8 Row: [5, 6]
    Adding Row to key: 9 Row: [7]
    Adding Row to key: 9 Row: [7, 3]
    key: 1 value: 
    7
    3
     
    key: 2 value: 
    7
    3
     
    key: 3 value: 
    7
    3
     
    key: 4 value: 
    7
    3
     
    key: 5 value: 
    7
    3
     
    key: 6 value: 
    7
    3
     
    key: 7 value: 
    7
    3
     
    key: 8 value: 
    7
    3
     
    key: 9 value: 
    7
    3
     
    Value for the key '2': [7, 3]
    The
    Java Code:
    System.out.println("Adding Row to key: "+getElement(p, i, 1)+" Row: "+row);
    gives me a good picture of what is added to the hashMap, but the output I get is completely different...
    The last line of the output is to verify that my display method is correct...

    My doubt is that,am I adding the key,values it to the hashMap correctly?..
    (I am new to the concept of hashMap..)

    Quote Originally Posted by mkarthik90 View Post
    Is it that your order is missing in HashMap?
    Order? I don't think I have mentioned order anywhere in the code....

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,580
    Rep Power
    5

    Default Re: Java - HashMap - problem with values..

    You keep re-using the same ArrayList object for the row. So any index of your hashmap, it contains a reference to the exact same row which you clear and fill with different objects. I can't believe that is what you want, you want a new unique ArrayList object in each index of the Map.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    galxion123 is offline Member
    Join Date
    Aug 2012
    Posts
    11
    Rep Power
    0

    Default Re: Java - HashMap - problem with values..

    Quote Originally Posted by gimbal2 View Post
    You keep re-using the same ArrayList object for the row. So any index of your hashmap, it contains a reference to the exact same row which you clear and fill with different objects. I can't believe that is what you want, you want a new unique ArrayList object in each index of the Map.
    It works!
    I added a
    Java Code:
    row = new ArrayList<Integer>();
    instead of row.clear().
    Now I don't know what the purpose of .clear() is... doesn't it remove/clear any element of the list?
    Or maybe.. Each time I add it to the hashMap, I need a new instance of, but not a cleared arraylist?..

    Thank You!

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,580
    Rep Power
    5

    Default Re: Java - HashMap - problem with values..

    You kept adding the exact same list to the hashmap. So when you call clear on it, all indexes all of a sudden have an empty list.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    galxion123 is offline Member
    Join Date
    Aug 2012
    Posts
    11
    Rep Power
    0

    Default Re: Java - HashMap - problem with values..

    Quote Originally Posted by gimbal2 View Post
    You kept adding the exact same list to the hashmap. So when you call clear on it, all indexes all of a sudden have an empty list.
    Ok, got it!
    Thanks again!..

Similar Threads

  1. displaying values from HashMap
    By Debra in forum New To Java
    Replies: 6
    Last Post: 05-13-2012, 06:30 AM
  2. HashMap help? Values keep overriding..?
    By sim18 in forum New To Java
    Replies: 2
    Last Post: 05-06-2012, 12:59 PM
  3. how to get the values from hashmap
    By baktha.thalapathy in forum New To Java
    Replies: 5
    Last Post: 05-25-2010, 02:12 PM
  4. HashMap contains all values but doesn't show all values
    By xcallmejudasx in forum New To Java
    Replies: 3
    Last Post: 05-10-2009, 11:35 PM
  5. how to return values from hashmap
    By oregon in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 04:56 PM

Posting Permissions

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