Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Treeset with both comparable and comparator

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why does the line: TreeSet<Question14> t2 = new TreeSet<>(s1);
have different output compare to when it is TreeSet<Question14> t2 = new TreeSet<>();

 
Rancher
Posts: 3441
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try debugging the code to see what it is doing.  Add print statements in the compare and compareTo methods that print out the values being compared and the value that is returned.
The print out will show you why you are getting the results.
 
Bartender
Posts: 3517
150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OP added a printout. So that will not make him much wiser.

OP must have a look at the API of a TreeSet what the difference is between:

and
 
Norm Radder
Rancher
Posts: 3441
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

printout. So that will not make him much wiser.


The print out from the print statements that I suggested would show what method is called and when it is called and what the results of the call is.
Given that if the OP did not understand what was happening, he could ask.
 
Master Rancher
Posts: 260
12
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet's response is right on. In addition to that, also think about what the following would do:
 
It means our mission is in jeapordy! Quick, read this tiny ad!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!