Java 带有没有警告的比较器声明的通用 treeMap

Java Generic treeMap with comparator declaration without warning

接下来我可以采取任何方法来避免收到黄色警告/@SuppressWarnings("unchecked")

泛型对象:

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; 

    }
 }

产品Class:

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;
    }




}

谢谢!

从您发布的代码看来:

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;
}