This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Comparator question

 
Graham VMead
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to sort a list of session beans by their string session keys so I implemented the following.



If I want to search this sorted list using Collections.binarySearch with the search key as a String and using the same comparator how do I know whether objA or objB will be the search String and which will be the bean.

Will I need to do the following



Hope this makes sense, I know I should be checking the bean class and throwing ClassCastExceptions etc but I've left it out for clarity.
[ July 07, 2006: Message edited by: Graham VMead ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no other obvious way to do it; what you've done makes perfect sense.

Regarding type-checking: it's generally considered OK to just cast obliviously in Comparators, without explicit checking. When you call sort() using such a Comparator, then it would make sense to catch ClassCastException.
 
Graham VMead
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Ernest: Thats what I'll do then
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic