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.


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

Results 1 to 2 of 2

Thread: Comparator and "Equalator"

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Thanked 0 Times in 0 Posts

    Default Comparator and "Equalator"


    I was browsing through the Java docs online for information about TreeSet. And I came across the following:

    "Caution should be exercised when using a comparator capable of imposing an ordering inconsistent with equals to order a sorted set (or sorted map). Suppose a sorted set (or sorted map) with an explicit comparator c is used with elements (or keys) drawn from a set S. If the ordering imposed by c on S is inconsistent with equals, the sorted set (or sorted map) will behave "strangely." In particular the sorted set (or sorted map) will violate the general contract for set (or map), which is defined in terms of equals."

    So, to avoid any 'trouble', one should make sure that the Comparator<T>.compare( T a,b) returns 0 whenever a.equals(b) is true.

    Now, TreeSet() allows us to pass a Comparator in it's ctor(). Why doesn't it allow us to also pass in a 'Equaltor" in the constructor aswell? What if I had a set of Songs with fields for title, album, band, and year, and now I want to change the criteria for order and uniqueness in a set from any one of the Songs' fields?

    So I have a collectoin of Songs from my iTunes library. I want to make a set of titles, albums, and bands

    According to the quote above, I can't do it cleanly, because I would have to change equals() in each case in order to avoid the collection from acting "strangely". It seems like we should be able to pass an Equal-Object just like Comparator.

    Am I missing something? Thanks.

  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Tavistock, UK
    Thanked 103 Times in 93 Posts

    Default Re: Comparator and "Equalator"

    I think what you may be missing is that something like TreeSet or TreeMap is sorted *online*, so any sorting it does happens whenever you modify it (add or remove objects). What you're looking for is something that is sorted 'offline' or on demand, or externally. A list is an 'ordered collection' - one class that does offer to sort Lists on a specified Comparator is java.util.Collections - see the sort(List, Comparator) method.

Similar Threads

  1. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  2. Replies: 6
    Last Post: November 12th, 2010, 03:40 AM
  3. Java says:"Hello World". I say:"It works!"
    By Davidovic in forum Member Introductions
    Replies: 4
    Last Post: June 29th, 2010, 07:13 AM
  4. Replies: 1
    Last Post: March 31st, 2010, 09:42 PM
  5. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 07:35 AM