• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to get a highest (last) element in treeset when treeset is sorted with other attribute  RSS feed

 
Ags Karan
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Please help me to know how to solve this requirements.

I am using a treeset which contains employee class
TreeSet<Employee> ts = new TreeSet<Employee>(new ComparatorEmpId());

Requirements :

My treeset should be sorted by employee Id
But I want to get the highest salaried employee from treeset (using last) . how do i do that ?
[I tried to use comparator for sorting the employee Id , but for getting a highest salary I should have had the salary attribute sorted ]

ThanksInAdvance
 
Paweł Baczyński
Bartender
Posts: 2086
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

You can use Collections.max(Collection, Comparator) method for that.

In Java 8 you could also use streams for that:This assumes there is method public int getSalary() in Employee class.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again. I like Paweł's Stream solution, but you need to know it probably doesn't return an Employee object. It will return an Optional<Employee> so you need to check whether the result isPresent() before proceeding.
 
Stephan van Hulst
Saloon Keeper
Posts: 7992
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł's solution does return an Employee, but it with throw a NoSuchElementException if there's only one Employee in the tree.
 
Paweł Baczyński
Bartender
Posts: 2086
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Paweł's solution does return an Employee, but it with throw a NoSuchElementException if there's only one Employee in the tree.

No, it will throw NoSuchElementException only if the set is empty.

By the way, TreeSet.last() would also throw NoSuchElementException in such case so my solution is consistent with OP's question .
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, the Stream#max method returns an Optional and the get method returns the Employee or throws that Exception.

Yes, I was mistaken. Sorry. Thank you for correcting me, Stephan.

To get the Optional you have to stop one call earlier and use the is present method as I said earlier.
 
Stephan van Hulst
Saloon Keeper
Posts: 7992
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Er... and then Paweł corrected me
 
Ags Karan
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your responses and for welcoming me Have not known my doubt would be cleared this soon
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!