0

I have this class:

public abstract class Orderable<ID, T extends Comparable, V extends Orderable> implements Comparator<V>{
public abstract ID getId();
public abstract T getOrderParam();

public int compare(V o1, V o2) {
    int compared = o1.getRankParam().compareTo(o2.getRankParam());
    if (compared == 0) return o2.getId().toString().compareTo(o1.getId().toString());
    return compared;
}

Now I'd like to create a ConcurrentSkipListMap passing as Comparator what I defined in Orderable Class. The goal is to keep ConcurrentSkipListMap ordered with comparator of V, where V is a generic class that extend Orderable.

    private volatile ConcurrentSkipListMap<ID, V> concurrentSkipListMap;
private volatile AtomicInteger size;

public LeaderBoardImpl() {
    concurrentSkipListMap = new ConcurrentSkipListMap<ID, V>(**** HERE I NEED TO PASS COMPARATOR****);
    size = new AtomicInteger(0);
}

I'm not sure how to pass a Comparator of a generic Class. Thanks Albert

Gianx
  • 61
  • 7
  • 2
    `Comparable` is a *raw* generic. Do not use *raw* generics. Perhaps you meant `T extends Comparable`? --- The same goes for `Orderable` – Andreas Jun 18 '18 at 07:37
  • [What is a raw type and why shouldn't we use it?](https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it) – Andy Turner Jun 18 '18 at 07:40
  • Sorry, my class Orderable implements Comparator. – Gianx Jun 18 '18 at 07:41
  • 1
    @Albert `compareTo` isn't a member of `Comparator`: `compare` is; `compareTo` is a member of `Comparator`; – Andy Turner Jun 18 '18 at 07:42
  • @Andreas that's works fine in a ConcurrentSkipListSet, which use default Comparable of object inside. But for ConcurrentSkipListMap I need to specify one – Gianx Jun 18 '18 at 07:44
  • @Albert it's unclear what you are intending to do with all those type variables. As far as I can see, `T` is unused, and `V` is raw. If you change it to `V extends Orderable`, does it work as you expect? – Andy Turner Jun 18 '18 at 07:44
  • 1
    @Michael typo: the second `Comparator` should be `Comparable`. – Andy Turner Jun 18 '18 at 07:49
  • I have another class that extends Orderable that in my generic concurrentSkipListMap is V – Gianx Jun 18 '18 at 07:50
  • It is concurrently both Comparable and Comparator, I guess, I will skip this one :) – gagan singh Jun 18 '18 at 07:53
  • @AndyTurner at the beginning I had Orderable implements Comparable. And everything goes well if i use a ConcurrentSkipListSet (of my class that extend Ordearble items). But now I want to use ConcurrentSkipListMap, and I must specify a Comparator. So I changed the implements of Orderable to Comparator. – Gianx Jun 18 '18 at 07:54
  • A map is ordered by the key, not value. So in this case you can order by ID not by V. – gagan singh Jun 18 '18 at 08:05
  • @gagansingh you're right for a regular Map. But with ConcurrentSkipListMap you can pass a Comparator and keep the Map ordered with comparator defined for V – Gianx Jun 18 '18 at 08:09
  • @Albert are you talking about [this constructor](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListMap.html#ConcurrentSkipListMap-java.util.Comparator-) ? The comparator over here is for the key, not value. – gagan singh Jun 18 '18 at 14:17

0 Answers0