• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

retrieving data from a TreeMap collection

 
Bob Bob
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An obvious possibility is that the Comparator (or Comparable implementation) sorts the wrong way around...
- Peter
 
Bob Bob
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the complete code:
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So which of the two is being retrieved? Rose?
- Peter
 
Bob Bob
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

should return rose, but its returning chris
 
Bob Bob
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ANYBODY? ANY IDEAS AT ALL?
 
Garrett Smith
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic