• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic