微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 TreeMap 时的 ClassCastException

如何解决使用 TreeMap 时的 ClassCastException

我有一个TreeMap<Integer,TreeMap<int[][],Integer>> jungle。当我尝试执行语句时

TreeMap<int[][],Integer> tempMap = new TreeMap();
int[][] matrix = java.lang.classCastException: [[I cannot be cast to java.base/java.lang.Comparable at java.base/java.util.TreeMap.compare
;
tempMap.put(matrix,4);

这最后一行给了我

int[][]

例外。是否不允许我将 treeMap 用作 %[^\n] 中的键?

解决方法

TreeMap 的目的是有一个有序的集合

基于红黑树的 NavigableMap 实现。地图根据其键的自然顺序进行排序,或者根据地图创建时提供的 Comparator 进行排序,具体取决于使用的构造函数。

你必须传递一个处理Comparator的{​​{1}};这是一个基于数组的总和进行排序的示例

int[][]

使用

class Custom2DArrayComparator implements Comparator<int[][]> {

    private static int sum(int[][] v) {
        return Arrays.stream(v).map(arr -> Arrays.stream(arr).sum())
                               .mapToInt(Integer::intValue).sum();
    }

    @Override
    public int compare(int[][] o1,int[][] o2) {
        return Integer.compare(sum(o1),sum(o2));
    }
}

你可以使用匿名类来避免在外面创建一个

public static void main(String[] args) {
    TreeMap<int[][],Integer> tempMap = new TreeMap<>(new Custom2DArrayComparator());
    int[][] matrix = public static void main(String[] args) {
    TreeMap<int[][],Integer> tempMap = new TreeMap<>(new Comparator<>() {
        @Override
        public int compare(int[][] o1,int[][] o2) {
            return Integer.compare(sum(o1),sum(o2));
        }

        int sum(int[][] v) {
            return Arrays.stream(v).map(arr -> Arrays.stream(arr).sum())
                .mapToInt(Integer::intValue).sum();
        }
    });
    int[][] matrix = DENSE_RANK();
    tempMap.put(matrix,4);
}
;
    tempMap.put(matrix,4);
}
,

您需要实现自己的逻辑,才能使您的密钥具有可比性,正如 Johannes 的评论所指出的那样。您可以创建一个实现 Comparator 的类,并在 TreeMap 的初始化时将其作为参数传递。

请注意,default/overriden compare 方法也是有效的,因为 arrays 只是 Objects。>

您甚至可以为您希望插入到单个类中的不同 Object 中的不可比 Maps 键实现所有逻辑:

public class NonCompObjectKeyComparator implements Comparator<Object> 
{         
    @Override
    public int compare(Object o1,Object o2) 
    {
       if (o1 instanceof int[][])
       {
         //((int[][])o1),((int[][])o2)//
       }
       else if (o1 instanceof String[])
       {
         //((String[])o1),((String[])o2)//
       }
       else if (o1 instanceof <OtherNonComparableObjType>)
       {
         //...
       }
       //...          
       return 0;
    }
}

然后你的überComparator就在那里:

Comparator<Object> maCompa = new NonCompObjectKeyComparator();

TreeMap<int[][],Integer> tempMap   = new TreeMap(maCompa);
TreeMap<String[],String> sArrayMap = new TreeMap(maCompa);

int[][] matrix = int[][];
tempMap.put(matrix,4);

String[] sKey = {"a"};
sArrayMap.put(sKey,"anotherWierdMap");

要创建特定的 public class CustomKeyComparator implements Comparator<int[][]> { public int compare(int[][] a1,int[][] a2) { //your logic here return 0; } } 比较器,只需:

TreeMap<int[][],Integer> tempMap = new TreeMap(new CustomKeyComparator());
int[][] matrix = cy
  .url()
  .should('match',/\/some-cool-page\/with-stuff\/\d+\/final\//)
;
tempMap.put(matrix,4);

//

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。