I would like to use a set of pairs in Java, but when I call contains()
to see if it already contains a specific pair then I always get a ClassCastException
. Is there a way to avoid this kind of behaviour?
It is instantiated like this:
private static final Set<Pair<String, String>> BLACKLIST = new TreeSet<>();
BLACKLIST.add(new Pair<String, String>("anytext", "anytext"));
Calling contains() here leads to following 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)
I use java.util classes instead of my own. Is there an elegant way to avoid to implement a new class pair which overloads common.util.Pair?