Java 使用一组对时的ClassCastException

Java 使用一组对时的ClassCastException,java,set,treeset,Java,Set,Treeset,我想在Java中使用一组对,但是当我调用contains()查看它是否已经包含一个特定的对时,我总是得到一个ClassCastException。有没有办法避免这种行为 它是这样实例化的: private static final Set<Pair<String, String>> BLACKLIST = new TreeSet<>(); BLACKLIST.add(new Pair<String, String>("anytext", "any

我想在Java中使用一组对,但是当我调用
contains()
查看它是否已经包含一个特定的对时,我总是得到一个
ClassCastException
。有没有办法避免这种行为

它是这样实例化的:

private static final Set<Pair<String, String>> BLACKLIST = new TreeSet<>();

BLACKLIST.add(new Pair<String, String>("anytext", "anytext"));
private static final Set BLACKLIST=new TreeSet();
添加(新对(“任意文本”、“任意文本”));
在此处调用contains()会导致以下ClassCastException:

if (!blacklist.contains(new Pair<String, String>(localizedFile.getName(), key)))

java.lang.ClassCastException: .common.util.Pair cannot be cast to java.lang.Comparable
        at java.util.TreeMap.compare(TreeMap.java:1294)
        at java.util.TreeMap.put(TreeMap.java:538)
        at java.util.TreeSet.add(TreeSet.java:255)
if(!blacklist.contains(新对(localizedFile.getName(),key)))
java.lang.ClassCastException:.common.util.Pair不能强制转换为java.lang.Compariable
比较(TreeMap.java:1294)
位于java.util.TreeMap.put(TreeMap.java:538)
在java.util.TreeSet.add处(TreeSet.java:255)

我使用java.util类而不是我自己的类。有没有一种优雅的方法可以避免实现一个重载common.util.pair的新类对?

TreeSet
使用其元素的自然顺序,并且似乎要转换为
Comparable

要使此代码正常工作,您可以将
树集
指定给您自己的
比较器
,以便它可以使用该比较器,而不是自然排序

Set<Pair<String, String>> blacklist = new TreeSet<>(
    Comparator.comparing(Pair::getFirst)
              .thenComparing(Pair::getSecond));
设置黑名单=新建树集(
Comparator.comparing(对::getFirst)
.然后比较(Pair::getSecond));

它将首先排序,然后是
对的第二项
树集合使用其元素的自然排序
除非在创建集合时将任何显式比较器传递给构造函数

合同上说,所有插入到 集合必须实现可比较接口。此外,所有此类元素必须相互可比较
e1。compareTo(e2)
不得抛出 集合中任何元素e1和e2的ClassCastException

你的代码是

  private static final Set<Pair<String, String>> BLACKLIST = new TreeSet<>();
BLACKLIST.add(new Pair<String, String>("anytext", "anytext"));
private static final Set BLACKLIST=new TreeSet();
添加(新对(“任意文本”、“任意文本”));
由于您没有将显式比较器传递给构造函数,并且您的元素(对)都没有实现良好的可比性,因此在向集合中添加元素时会出现类强制转换异常

我的建议是 (i) 传递一个显式比较器

或(ii)使成对机具具有以下可比性

public class Pair<T1 extends Comparable<T1>, T2 extends Comparable<T2>> implements Comparable<Pair<T1, T2>> {
    T1 firstName;
    T2 secondName;

    public Pair(T1 firstName, T2 secondName) {
        this.firstName = firstName;
        this.secondName = secondName;
    }

    public T1 getFirstName() {
        return firstName;
    }

    public T2 getSecondName() {
        return secondName;
    }

    // elements ordered with first parameter .you can make a better
    // comparable as well
    @Override
    public int compareTo(Pair<T1, T2> o) {
        // TODO Auto-generated method stub
        return this.firstName.compareTo(o.firstName);
    }

}
公共类对实现了可比较的{
T1名;
T2第二名;
公共对(T1第一名,T2第二名){
this.firstName=firstName;
this.secondName=secondName;
}
公共T1 getFirstName(){
返回名字;
}
公共T2 getSecondName(){
返回secondName;
}
//元素按第一个参数排序。您可以制作更好的
//同样可比
@凌驾
公共整数比较(o对){
//TODO自动生成的方法存根
返回this.firstName.compareTo(o.firstName);
}
}

Pair是否实现了Compariable?你能分享你的
Pair
类吗?重复的可能重复项不要使用
Set
,但原因是一样的,解决方案也是一样的。重复项:我不能在Java中使用Pair::getFirst。@pink\u panda确切的方法名称当然取决于你的
Pair
类。另外,在您使用的Java版本上;我假设Java8或更高版本。