Results 1 to 3 of 3
  1. #1
    trixter is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default SortedSet and my own Integer comparator

    Hello,

    This is my Integer comparator:

    Java Code:
    class cmp implements Comparator
    	{
    		public int compare(Object OA, Object OB) 
    	    {
    			int A=(Integer)OA,B=(Integer)OB;
    			if (distR[A]!=distR[B]) return (int) (distR[A]-distR[B]);
    			return A-B;
    	    }
    	}
    I need it here:

    Java Code:
    SortedSet<Integer> S = new TreeSet<Integer>(new cmp());
    In C++ I've done it several times in a similiar way and everything worked fine.
    Here, it compiles, but sometimes when I write for example:

    Java Code:
    int x=700;
    S.add(x);
    x is not added to the SortedSet, although it's different to any other value in the set. Why? :confused:

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    The API for TreeSet states:

    Note that the ordering maintained by a set (whether or not an explicit comparator is provided) must be consistent with equals if it is to correctly implement the Set interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Set interface is defined in terms of the equals operation, but a TreeSet instance performs all element comparisons using its compareTo (or compare) method, so two elements that are deemed equal by this method are, from the standpoint of the set, equal. The behavior of a set is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Set interface.
    I would not use Integer here but rather a wrapper object that overrides equals and has a Comparator or implements Comparable in a similar way.

  3. #3
    trixter is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    The API for TreeSet states:



    I would not use Integer here but rather a wrapper object that overrides equals and has a Comparator or implements Comparable in a similar way.
    Thank you for your answer. Yes, I know that way of doing that, I just prefer not to create new classes if not necessary.

    However, I found my mistake :) distR was the array of doubles and the values often were different, but the difference was about 0.0001, so - after casting to int - it was 0. This is why SortedSet "thaught" it was equal.

    The topic now can be closed.

Similar Threads

  1. SortedSet Interface
    By Lil_Aziz1 in forum New To Java
    Replies: 14
    Last Post: 08-03-2010, 03:15 PM
  2. how to use comparator
    By KidneyinaCooler in forum Advanced Java
    Replies: 2
    Last Post: 07-18-2010, 10:25 AM
  3. convert unsigned integer to signed integer in java?
    By diskhub in forum New To Java
    Replies: 6
    Last Post: 05-17-2010, 12:50 AM
  4. SortedSet and Collections.binarySearch
    By ninoid in forum New To Java
    Replies: 4
    Last Post: 03-22-2010, 03:28 PM
  5. Use different comparator for SortedSet
    By linus_k in forum New To Java
    Replies: 0
    Last Post: 11-21-2008, 02:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •