• Post Reply Bookmark Topic Watch Topic
  • New Topic

TreeSet won't sort  RSS feed

 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,

I am creating a Treeset with a NameValuePair (inner class) as type.

the bean getter looks like this:



the NameValuePair class looks like this





now for some reason, upon adding values to the set, like this:




the set is not sorted once processed to output, and i have no clue why ..

thanks for help
 
Jason Cohen
Smart Bear Support
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sebastian,

It's hard to answer without knowing exactly how it's "not sorting" and an example of what you expected to have happen. However, I can still provide you with some ideas.

1. Make sure equals() and hashCode() are consistent with comparable(). I can't verify that without seeing your code.

2. You're comparing the value in your name/value pair, not the name. Is this on purpose? If not, that's the problem.

3. General note: Your method of comparing a string case-insensitive is actually incorrect! You cannot send both strings to lower case and then compare; rather you must use a.compareToIgnoreCase( b ). The reason is that with some languages (notably Turkish) there are capitalizations that compare equals when you use the correct form but are NOT the same when you send them both to lower case. Although the application in question might not need Turkish, this is a good general principal to know and use. Documentation is here: http://www.j2ee.me/j2se/1.4.2/docs/api/java/lang/String.html#compareToIgnoreCase(java.lang.String)
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey,

thank you for your reply ..

this is the full inner class code



now ...
what is returned by the db query is the following

Brand|Brand_No|Brand_1
Brand|1|ALFA ROMEO
Brand|76|ARO
Brand|85|ASIA
Brand|2|ASTON MARTIN
Brand|3|AUDI
Brand|5|BENTLEY
Brand|86|BERTONE
Brand|6|BMW
Brand|95|BRILLIANCE
Brand|96|BUGATTI
Brand|8|BUICK
Brand|9|CADILLAC
Brand|87|CATERHAM
Brand|10|CHEVROLET
Brand|11|CHRYSLER
Brand|12|CITROEN
Brand|69|DACIA
Brand|13|DAEWOO
Brand|15|DAIHATSU
Brand|77|DANGEL
Brand|17|DODGE
Brand|18|FERRARI
Brand|19|FIAT
Brand|20|FORD
Brand|22|HONDA
Brand|24|HUMMER
Brand|23|HYUNDAI
Brand|4|INFINITI
Brand|89|INNOCENTI
Brand|25|ISUZU
Brand|67|IVECO
Brand|26|JAGUAR
Brand|27|JEEP
Brand|28|KIA
Brand|29|LADA
Brand|30|LAMBORGHINI
Brand|31|LANCIA
Brand|32|LAND ROVER
Brand|78|LDV
Brand|33|LEXUS
Brand|35|LOTUS
Brand|79|MAHINDRA
Brand|36|MASERATI
Brand|80|MAYBACH
Brand|37|MAZDA
Brand|90|MEGA
Brand|38|MERCEDES-BENZ
Brand|39|MG
Brand|40|MINI
Brand|41|MITSUBISHI
Brand|42|MORGAN
Brand|43|NISSAN
Brand|44|OPEL
Brand|45|PEUGEOT
Brand|81|PGO
Brand|82|PIAGGIO
Brand|47|PONTIAC
Brand|48|PORSCHE
Brand|91|PORTARO
Brand|49|PROTON
Brand|50|RENAULT
Brand|51|ROLLS ROYCE
Brand|52|ROVER
Brand|53|SAAB
Brand|83|SANTANA
Brand|54|SEAT
Brand|55|SKODA
Brand|56|SMART
Brand|84|SOVAMAG
Brand|57|SSANG YONG
Brand|58|SUBARU
Brand|59|SUZUKI
Brand|92|TATA
Brand|61|TOYOTA
Brand|93|TVR
Brand|94|UMM
Brand|66|VOLVO
Brand|65|VW

now once this is put into a select box, it is sorted for the most part, except for the bottom entries (see attachment) ...
treeSetSort.gif
[Thumbnail for treeSetSort.gif]
 
Jason Cohen
Smart Bear Support
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, the problem is what I guessed in #2 above.

Specifically, your equals() and hashCode() are examining the KEY (or "name"), and your compareTo() is examining the VALUE.

You have to make them consistent! Then you should be good to go.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey,

ok cool thank you ...

now ... the thing is that i don't understand the mechanics behind it ...

i thought equals and hashcode are to determine equality between objects (in terms of a set a dublicate entry) ..
so. .what does that have to do with comparing ?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sebastian Janisch wrote:
i thought equals and hashcode are to determine equality between objects (in terms of a set a dublicate entry) ..
so. .what does that have to do with comparing ?


The java.util.TreeSet class uses the comparable and comparators to check for equality. It assumes that the equals() method is consistent. If it isn't, then it is not in compliance with the definition of set, as the Set interface implies that equality is done with the equals() method.

The JavaDoc (for TreeSet, linked above) specifically mentions this...

Henry
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!