2011-10-26 29 views
0

我想要繼承ConcurrentSkipListMap並將它的比較器設爲沒有任何鎖定。 這是我有:如何繼承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>()); 

正如你可以看到我加3層構造到隊列類。無論我在主類中改變了什麼,其他構造函數都會產生錯誤。什麼是正確的方式來設置它的權利? 謝謝

回答

3

第二個構造函數是垃圾。去掉它。

而您的代碼不會編譯,因爲您使用MYCLASS作爲鍵和Integer作爲值構建隊列,但提供了一個比較器,它將Integer實例而不是MYCLASS實例排序。

我想你想要的是整數作爲關鍵。如果是這樣,那麼隊列的類型應該是Queue。

或者你可以尊重放置鑰匙第一和後的值的慣例,並更改隊列聲明

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

注意,它通常是一個壞主意,子類集合。將一個集合封裝在一個對象中通常會更好。