Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Comparator interface question

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Comparator interface question

    I have to implement a class which implements the Comparator interface which does this: Given two Comparables of identical type, give the reverse of the "natural"
    ordering (from their built-in compareTo method) for those objects.

    I don't really understand what it means when it says given two Comparables of identical type, can someone help me please?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Comparator interface question

    See the API for Comparable: Comparable (Java Platform SE 6)

    When implementing the Comparator interface, sometimes (but definitely not always) one uses the compareTo method of Comparable ( see Object Ordering (The Java™ Tutorials > Collections > Interfaces) for examples). The above API for Comparable describes what is returned from the compareTo method, to reverse the order you can reverse the value of what is returned.

  3. The Following User Says Thank You to copeg For This Useful Post:

    brisingrbrom (February 24th, 2012)

  4. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Comparator interface question

    Ok I understand that but how do I then use two of these Comparables in a Comparator interface? Syntax in Java is really confusing me and thanks for the response

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Comparator interface question

    Many basic classes already implement the Comparable interface...for instance String, Integer, Float, Double, etc...in fact the link I provided to the API above lists all known implementing classes in the standard java library.

  6. The Following User Says Thank You to copeg For This Useful Post:

    brisingrbrom (February 24th, 2012)

Similar Threads

  1. When to use Comparator and Comparable Interface in java
    By diyaots in forum Java Theory & Questions
    Replies: 7
    Last Post: October 26th, 2012, 09:35 AM
  2. Help needed with Comparator objects and Collections
    By iceyd in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 5th, 2011, 02:41 PM
  3. Replies: 0
    Last Post: October 2nd, 2009, 10:51 PM
  4. [SOLVED] help with sorting...(comparator)
    By mdstrauss in forum Collections and Generics
    Replies: 2
    Last Post: July 26th, 2009, 06:25 AM
  5. Getting an error while altering a source code
    By marksquall in forum Collections and Generics
    Replies: 3
    Last Post: June 8th, 2009, 02:49 AM

Tags for this Thread