aspose file tools*
The moose likes Beginning Java and the fly likes Map Store only last value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Map Store only last value" Watch "Map Store only last value" New topic
Author

Map Store only last value

Sandy Fiasher
Greenhorn

Joined: Jul 14, 2009
Posts: 8
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Jul 08, 2003
Posts: 24184
    
  34

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!


[Jess in Action][AskingGoodQuestions]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

That's just how Maps work - one value for each key.

What you want is a MultiMap - check out http://commons.apache.org/collections/


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Sandy Fiasher
Greenhorn

Joined: Jul 14, 2009
Posts: 8
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

Joined: Jul 14, 2009
Posts: 8
I am sorry - Didn't realized the Naming convention - changed now
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Jul 14, 2009
Posts: 8
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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8



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

Joined: Jul 14, 2009
Posts: 8
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
Sheriff

Joined: Sep 28, 2004
Posts: 18842
    
  40



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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Sandy Fiasher
Greenhorn

Joined: Jul 14, 2009
Posts: 8
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

Joined: Mar 05, 2008
Posts: 3014
    
  10
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

Joined: Oct 27, 2005
Posts: 19696
    
  20

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

Joined: Mar 05, 2008
Posts: 3014
    
  10
Ah, I overlooked that it was a TreeMap, thanks. Substitute compareTo() for equals() in the above post, then.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Map Store only last value