Win a copy of Head First Android this week in the Android 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Collections and comparisons

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone please explain to me the difference between the Comparator interface and the Comparable interface. When would I need to override each?
[ February 21, 2002: Message edited by: Rajinder Yadav ]
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Java API gives a good explanation, look for the classes Comparable and Comparator.
Erik Dark
[ February 21, 2002: Message edited by: Erik Dark ]
 
Rajinder Yadav
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Erik thanks for the tip!
I looked them up in the API and also looked up Collections.sort() under java.utils which helped clear it up for me.
[ February 21, 2002: Message edited by: Rajinder Yadav ]
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone please tell me why Collections.sort() only takes List as its arguments? What about Set, Map or Vector?
Thanks,
Jenny
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Collections.sort() takes a List as argument so that all classes implementing it (AbstractList, ArrayList, LinkedList, Vector) can be given as argument, and thus, sorted.
[ February 21, 2002: Message edited by: Valentin Crettaz ]
 
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
also, notice that HashSet and TreeSet are ALWAYS sorted, so u dont need to sort them.
 
Rajinder Yadav
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jenny, along with what Roy said, for Maps there are two methods values() and keySet()
Map.values() returns a Collection containing all the values, which could be sorted using Collections.sort()
Likewise Map.keySet() return a set of all possible keys and again you could sort them using Collections.sort()
So if you had some special sorting need this is one way it could be handled with a Map interface.
[ February 22, 2002: Message edited by: Rajinder Yadav ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic