如何子类化ConcurrentSkipListMap并设置其Comparator?
我正在尝试子类 ConcurrentSkipListMap 并设置它的比较器而不带任何锁定。 这就是我所拥有的:
// 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>());
如您所见,我向 Queue 类添加了 3 个构造函数。无论我在主类中进行什么更改,其他构造函数都会产生错误。正确的设置方法是什么? 谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第二个构造函数是垃圾。将其删除。
并且您的代码无法编译,因为您构建了一个以 MYCLASS 作为键、以 Integer 作为值的队列,但提供了一个对 Integer 实例而不是 MYCLASS 实例进行排序的比较器。
我猜你想要的是整数作为关键。如果是这样,那么队列的类型应该是Queue。
或者,您可以遵循将键放在前面、将值放在后面的约定,并将 Queue 声明更改为“
注意,对集合进行子类化通常是一个坏主意”。通常最好将集合封装在对象中。
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
Note that it's generally a bad idea to subclass collections. It's generally better to encapsulate a collection in an object.