带有比较器声明的Java Generic treeMap,没有警告

时间:2015-01-03 23:39:43

标签: java generics

我可以通过任何方式获取黄色警告/ @SuppressWarnings(“未选中”)

泛型对象:

P =要比较的产品,C =客户

public static myComparator<Product<P>> comparator= new myComparator<Product<P>>();

比较器声明在“insertIntoMap”方法之外, 我无法在“insertIntoMap”方法中使用Product

对象。

public static <P,C> TreeMap<P, C>  insertIntoMap(LinkedHashSet<P> set,C[] ac){

    @SuppressWarnings("unchecked")
    TreeMap<P,C> treeMap = new TreeMap<P,C>((Comparator<? super P>) comparator);  

    int itrIndex=0;
    Iterator<P> itr = set.iterator();

        while(itr.hasNext()){
            treeMap.put(itr.next(),   ac[itrIndex]);
            itrIndex++;
        }

    return (TreeMap<P, C>) treeMap;
}


public static class myComparator<E>  implements Comparator<Product<? super E>>{
    @Override
    public int compare(Product<? super E> o1, Product<? super E> o2) {
               if(o1.getName().length()>o2.getName().length())
                  return 1;
               else  return -1; 

    }
 }

产品类别:

public static class Product<E> implements Comparable<E>{

    private E serialNum;
    private String name;

    Product(E serialNum,String name){

        setSerialNum(serialNum);
        setName(name);
    }


    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result;
        result = prime * result + ((serialNum == null) ? 0 : serialNum.hashCode());
        return result;
    }


    @Override
    public boolean equals(Object obj) {
        Product<?> other = (Product<?>) obj;
        if (serialNum.equals(other.serialNum))return true;
        else return false;
    }



    public E getSerialNum() {
        return serialNum;
    }
    public void setSerialNum(E serialNum) {
        this.serialNum = serialNum;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public String toString() {
        return serialNum.toString()+": "+name;
    }


    @Override
    public int compareTo(E o) {
       if(this.hashCode()>o.hashCode())return 1;
       else if(this.hashCode()<o.hashCode()) return -1;
       else return -1;
    }




}

谢谢!

1 个答案:

答案 0 :(得分:0)

从您发布的代码看来:

myComparator不应该是通用的:

public class myComperator implements Comparator<Product<?>> {
    @Override
    public int compare(Product<?> o1, Product<?> o2) {
        if (o1.getName().length() > o2.getName().length())
            return 1;
        else if (o1.getName().length() < o2.getName().length())
            return -1;
        else
            return -1;
    }
}

insertIntoMap应仅适用于P扩展Product。否则你不能使用myComperator(比较Products):

public static <P extends Product<?>, C> TreeMap<P, C> insertIntoMap(
        LinkedHashSet<P> set, C[] ac) {

    TreeMap<P, C> treeMap = new TreeMap<P, C>(new myComperator());

    int itrIndex = 0;
    Iterator<P> itr = set.iterator();

    while (itr.hasNext()) {
        treeMap.put(itr.next(), ac[itrIndex]);
        itrIndex++;
    }

    return treeMap;
}