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

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 ]
 
Everybody's invited. Except this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic