• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Map Store only last value

 
Sandy Fiasher
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All

I have a simple java code that calls another function and I set a TreeMap with Key, values

However, the TreeMap is storing only the last value. I am passing 2 values to store in the Map

I am not sure what am I doing wrong.

This is a code snippet

class StoreMap{

private TreeMap tMap = null;

void setUp(){

tMap = new TeeMap();
setUpMap(tMap)
// check tMap value - It is always 1 and shows only 1 value when iterated
System.out.println("tMap.size() is "+tMap.size());

}

void setUpMap(tMap){
//this method gets unique key and value
tMap.put(key,value)

}

}

Thanks for your feedback
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags. What's a TeeMap? Where's the code that actually stores something? I only see code putting a single value into the map.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post real code when asking for help. Since you think your code should work, your summary is going to show code that probably works. You're not going to put in the wrong part!
 
Rob Spoor
Sheriff
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's just how Maps work - one value for each key.

What you want is a MultiMap - check out http://commons.apache.org/collections/
 
Sandy Fiasher
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The objective is to store unique key and value. The code is just part of the full program. This is actually a threaded application and the program generated new key and new value.

I am trying to save the key and value in the Map. The reason is simple - So that I can retrieve the saved Map at later point

I appologize for not showing the entire code - but the gist of it is shown in the snippet

I am confused on why the TreeMap is not able to store the value and saved only the last one.
 
Sandy Fiasher
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry - Didn't realized the Naming convention - changed now
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. The code you posted does this:

(1) Create a new TreeMap.

(2) Add an entry to it.

So after step (1) the map is empty and after step (2) it has one entry in it. If you want something else to happen then don't do those two steps like that.
 
Sandy Fiasher
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry for this Paul, I would have explained much there, but as I mentioned before - The setUpMap(tMap) gets different values of key and value - In this particular case - There are 2 values of separate key and value gets there.

So, i am confused on if setUpMap(tMap) gets separate values - Why does it shows the length as 1 and when iterated gets the last one
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


What are you talking about? You have one line of code there. It adds one entry to the map. That's all it does.

Perhaps you didn't post the code you meant to ask about? I notice you didn't use copy-and-paste to make your post; either that or you posted code which doesn't compile yet.
 
Sandy Fiasher
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and what i said Paul is that one line is called twice (for now)

I cannot show the entire code as it is a threaded application and i am showing the area where the issue is
 
Henry Wong
author
Marshal
Pie
Posts: 21492
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Without a working example, we can't tell you what is wrong -- we can only speculate. But possible speculations are...

1. The threads are storing to different tree maps.
2. The threads are storing the same key to the tree map.

Henry
 
Sandy Fiasher
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well that's fine Henry - All i want to ask that I am not doing something stupid and is missing something basic out there that Java didn't like - I have proof that I get distinct key and value pairs.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have proof that I get distinct key and value pairs.

Well, maybe. But are both these key/value pairs getting put into the same map?

So, i am confused on if setUpMap(tMap) gets separate values - Why does it shows the length as 1 and when iterated gets the last one

This suggests that the keys, at least, may not be distinct. Perhaps the keys are from a custom class, and the equals() method is not working correctly? So that two keys that should be distinct are seen as equal, according to equals()? Or perhaps something else in the program is removing entries from the map?

Aside from checking equals(), try printing the complete contents of the map immediately before you do an insert, and immediately after. That should give a few clues.

Another possibility is that the multiple threads you mentioned are doing something unsafe. Are you using synchronization or some other mechanism to enforce thread safety? Do the updates to the map come from two different threads? Does the behavior you describe occur erratically, or reliably?
 
Rob Spoor
Sheriff
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:This suggests that the keys, at least, may not be distinct. Perhaps the keys are from a custom class, and the equals() method is not working correctly? So that two keys that should be distinct are seen as equal, according to equals()? Or perhaps something else in the program is removing entries from the map?

TreeMap uses compareTo (or compare when a Comparator is provided) instead of hashCode / equals.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, I overlooked that it was a TreeMap, thanks. Substitute compareTo() for equals() in the above post, then.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic