user63898
user63898

Reputation: 30895

How to subclass ConcurrentSkipListMap and set its Comparator?

I'm trying to subclass ConcurrentSkipListMap and set it's Comparator without any lock. Here's what I have :

// the subclass 
public class Queue<V, K> extends ConcurrentSkipListMap<K, V> {

    public Queue(Comparator<? super K> queueComparator) {
        // TODO Auto-generated constructor stub
        super(queueComparator);
    }

    public Queue(QueueComparator<Integer> queueComparator) {
        // TODO Auto-generated constructor stub
        super((Comparator<? super K>) queueComparator);
    }

}
//the comparator (QueueComparator)
public class QueueComparator<T> implements Comparator<T> {

    @Override
    public int compare(T o1, T o2) {
        // TODO Auto-generated method stub
        return 0;
    }

}

// main class init the subclass

Queue queue= new Queue<Integer,MYCLASS>(new QueueComparator<Integer>());

As you can see I added 3 constructors to the Queue class. No matter what I change in the main class, the other constructors yield error. What is the right way to set it right ? thanks

Upvotes: 0

Views: 300

Answers (1)

JB Nizet
JB Nizet

Reputation: 691725

The second constructor is rubbish. Remove it.

And your code doesn't compile because you build a queue with MYCLASS as key and Integer as value, but provide a comparator which sorts Integer instances instead of MYCLASS instances.

I guess that what you want is Integer as key. If so, then the queue's type should be Queue.

Or you could respect the convention of placing the key first and the value after, and change the Queue declaration to

public class Queue<K, V> extends ConcurrentSkipListMap<K, V> {

Note that it's generally a bad idea to subclass collections. It's generally better to encapsulate a collection in an object.

Upvotes: 3

Related Questions