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:

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

07-25-2013, 12:02 PM

gimbal2

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.

07-25-2013, 12:05 PM

mkarthik90

Re: Java - HashMap - problem with values..

Is it that your order is missing in HashMap?

07-25-2013, 12:17 PM

DarrylBurke

Re: Java - HashMap - problem with values..

Quote:

Originally Posted by mkarthik90

Is it that your order is missing in HashMap?

Order?

db

07-25-2013, 01:00 PM

galxion123

Re: Java - HashMap - problem with values..

Quote:

Originally Posted by gimbal2

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:

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

Is it that your order is missing in HashMap?

Order? I don't think I have mentioned order anywhere in the code....

07-25-2013, 01:17 PM

gimbal2

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.

07-25-2013, 02:37 PM

galxion123

Re: Java - HashMap - problem with values..

Quote:

Originally Posted by gimbal2

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

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!

07-25-2013, 02:50 PM

gimbal2

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.

07-25-2013, 03:05 PM

galxion123

Re: Java - HashMap - problem with values..

Quote:

Originally Posted by gimbal2

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.