1

I am using java.util.concurrent.ConcurrentSkipListSet. I define a class People whose objects are held in a ConcurrentSkipListSet<People>, a Comparator<People> comparing Age of People, and that ConcurrentSkipListSet<People> object is constructed via this Comparator<People>.

If I have two People objects having the same Age, e.g. a.Age.equals(b.Age), what happens? Both a and b in the ConcurrentSkipListSet? Or only one?

It's required that such a Comparator must be consistent with equals, but in my case, a.Age.equals(b.Age) definitely doesn't mean a.equals(b). I just want to sort People objects by Age.

Now my comparator is implemented as follows:

    class ComparatorHSD implements Comparator<People> {  
        public int compare(People s0, People s1) {  
        int rv = 0;  
        if (s0.Age > s1.Age) {
            rv = 1;
        } else if (s0.Age < s1.Age) rv = -1;
        return rv;
    }  

If I don't define rv = 0, the program seems stuck somewhere and will not terminate. So I have define it? Why?

象嘉道
  • 3,657
  • 5
  • 33
  • 49
  • @gurung why did you italicize the code samples? And why some but not all? – corsiKa Dec 15 '11 at 20:05
  • @toto2 Big minus 1. Do not take the shortcut of subtraction in comparisons. http://stackoverflow.com/questions/2728793/java-integer-what-is-faster-comparison-or-subtraction – corsiKa Dec 15 '11 at 22:17
  • Actually, it is not working as what @glowcoder thought:`ConcurrentSkipListSet` is based on totally ordered `Set`, so **no** repeated elements can be added. – 象嘉道 Jan 11 '12 at 23:36
  • yes and no. It depends on what you mean by repeated. The key is that in a set, there will be no two non-null elements `a` and `b` such that `set.contains(a) && set.contains(b) && a.equals(b)` is true. This may or may not depend on your `Comparator` having its `compareTo` method consistent with `equals`. It would appear that in your case, being that it says so in the documentation, it requires them to be equal. – corsiKa Jan 12 '12 at 03:03
  • Simply put, you don't want to use this data structure to sort. Use an `ArrayList` or something. :) – corsiKa Jan 12 '12 at 03:05

2 Answers2

2

Consistent with equals simply means that a.equals(b) (not ==...) implies that a.compareTo(b) == 0. That's it.

In terms of sorting, this means that you have an undefined order within a range of equal values.

I should also note that, according to the javadoc of java.util.Comparator,

It is generally the case, but not strictly required that (compare(x, y)==0) == (x.equals(y)).

Although it can be generally be assumed that it is the case unless it is documented otherwise. (How much business risk you put on that assumption is up to you.)

corsiKa
  • 81,495
  • 25
  • 153
  • 204
  • So, regarding my example, both `a` and `b` are in the `ConcurrentSkipListSet`, but the *order* of them is undefined. Right? – 象嘉道 Dec 15 '11 at 21:54
  • If you have a,b,c,d,e with ages, in order, of 20,20,19,21,19, then you have 4 possible outcomes from the comparator: `c,e,a,b,d` `e,c,a,b,d` `c,e,b,a,d` `e,c,b,a,d` - the `c` and `e` might be before or after the other, but they will be before all the others. `a` and `b` will be between the 19s and 21, and 21 will be on the end. Make sense? – corsiKa Dec 15 '11 at 22:14
  • 1
    One follow up question - What do I have to do to get a set like a,c,d ... basically I don't want duplicate elements in the my set. – crucifix94 Jul 25 '19 at 13:13
  • @crucifix94 You can't get duplicate elements in a set. That's why you use a set. If you do have duplicate elements, you either aren't using a set, have a bug in your set, or have a bug in your equals (and or hashCode) implementation. – corsiKa Jul 25 '19 at 18:25
0

I think this is a moot point since you can't use Comparator<Age> with a ConcurrentSkipListSet<People>. See the javadoc, which shows that Age would have to be a supertype of People and I imagine it is not.

Sean Owen
  • 66,182
  • 23
  • 141
  • 173