File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes retrieving data from a TreeMap collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "retrieving data from a TreeMap collection" Watch "retrieving data from a TreeMap collection" New topic
Author

retrieving data from a TreeMap collection

Bob Bob
Greenhorn

Joined: Mar 20, 2003
Posts: 8
This code is supposed to get the name of the first employee object in a TreeMap collection and put it in a textbox. Anybody know why it displays the last object in the collection?
Ta Ri Ki Sun
Ranch Hand

Joined: Mar 26, 2002
Posts: 442
what are the keys ?
maybe the code adding the elements to the TreeMap will answer this question, or lead to an answer anyway
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
An obvious possibility is that the Comparator (or Comparable implementation) sorts the wrong way around...
- Peter
Bob Bob
Greenhorn

Joined: Mar 20, 2003
Posts: 8
Here is the complete code:
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
So which of the two is being retrieved? Rose?
- Peter
Bob Bob
Greenhorn

Joined: Mar 20, 2003
Posts: 8

should return rose, but its returning chris
Bob Bob
Greenhorn

Joined: Mar 20, 2003
Posts: 8
ANYBODY? ANY IDEAS AT ALL?
Garrett Smith
Ranch Hand

Joined: Jun 27, 2002
Posts: 401
Like Peter said, an obvious possibility is that the Comparator (or Comparable implementation) sorts the wrong way around

You haven't defined any comparator for the TreeMap. You don't any way to compare Employees.
You could implement interface Comparable in Employee.
First off, you have name and payRate as static. static means related to the class. You want instance variables, so remove static modifier (You knew that, right?)
Ok. Now You need to make a comparator or implement comparable interface for Employee.

The Comparator interface will allow you to make different types of comparisons (pay, first name, last name). Implementing Comparable interface allows just one type of comparison.

As a final note, it would be wise to declare numberEmployees as volatile.

For more information on thread safety, see Josh Bloch's Effective Java chapter on Synchronization.
[ March 26, 2003: Message edited by: Garrett Smith ]

comp.lang.javascript FAQ: http://jibbering.com/faq/
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Looks like there's no need for a Comparator here - the keys being used are Integers, which implement Complarator:

Based on this, yes it looks like Rose should be associated with the first key. I can't tell from your code where the problem actually is. I can't compile and run it myself since other classes are missing. I'd suggest inserting somethiing like this for debugging:

You can insert this several different places. Try it immediately after empObjects has been initialized, and then also after the code that attempts to display the results in a text field. This way you can see if the problem is really in the map, or in the code that attempts to take the map values and put them on screen. Good luck...
[ March 27, 2003: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
man!
there is a bug in Employee class...
all class members are "static" so even if everytime u create a new object all the values are overwritten hence only the last value is retained..
make Employee fields and method non-static and then see...it should work..
regards
maulin.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: retrieving data from a TreeMap collection