简体   繁体   English

如何编写自定义比较器以使用 SimpleEntry 对树集进行排序?

[英]How to code a custom Comparator to sort a Treeset with SimpleEntry?

I'm currently working on an assignement which requires me to use a Treeset to sort pairs of value.我目前正在处理一个要求我使用 Treeset 对值对进行排序的作业。 I'm asked to use SimpleEntry.我被要求使用 SimpleEntry。 I'm storing them on a Treeset:我将它们存储在 Treeset 上:

    TreeSet treeSet = new TreeSet(new PairComparator());
    SimpleEntry pair = new SimpleEntry(weight,source);
    treeSet.add(pair);

Here is my custom Comparator:这是我的自定义比较器:

static class PairComparator implements Comparator<AbstractMap.SimpleEntry<Integer, Integer>> {

        @Override
        public int compare(AbstractMap.SimpleEntry<Integer,Integer> o1, AbstractMap.SimpleEntry<Integer,Integer> o2) {
            int key1 = o1.getKey() ;
            int key2 = o2.getKey();
            return key1 - key2;
        }
    }

I get an error I don't understand, which is:我收到一个我不明白的错误,即:

class java.util.HashMap cannot be cast to class java.lang.Integer class java.util.HashMap cannot be cast to class java.lang.Integer

Could you explain me what is happening?你能解释一下发生了什么吗? I'm not totally familiar with defining a custom comparator.我对定义自定义比较器并不完全熟悉。

Thanks for your attention !感谢您的关注 !

EDIT: I was adding a Hashmap as a key on my SimpleEntry and not an integer, which is now quite obvious...编辑:我在我的 SimpleEntry 上添加了 Hashmap 作为键,而不是 integer,这现在很明显......

Seems like somehow you have added a HashMap as a key to one of the SimpleEntries in your TreeSet.似乎您以某种方式添加了 HashMap 作为 TreeSet 中 SimpleEntries 之一的键。

Show a complete runnable example please.请显示一个完整的可运行示例。

This code works for me:这段代码对我有用:

import java.util.AbstractMap;
import java.util.AbstractMap.SimpleEntry;
import java.util.Comparator;
import java.util.TreeSet;

public class Main {

    static class PairComparator implements Comparator<SimpleEntry<Integer, Integer>> {

        @Override
        public int compare(SimpleEntry<Integer, Integer> o1, SimpleEntry<Integer, Integer> o2) {
            int key1 = o1.getKey();
            int key2 = o2.getKey();
            return key1 - key2;
        }
    }

    public static void main(String[] args) {
        TreeSet treeSet = new TreeSet(new PairComparator());
        treeSet.add(new SimpleEntry(42, 69));
        treeSet.add(new SimpleEntry(37, 65));
        treeSet.add(new SimpleEntry(23, 19));
        treeSet.add(new SimpleEntry(54, 12));
        System.out.println(treeSet);
    }
}

You didn't specify the types for the TreeSet and some other data structures but I did it here and it works just fine.您没有指定TreeSet和其他一些数据结构的类型,但我在这里做了,它工作得很好。 I did change your comparator.我确实改变了你的比较器。 Subtracting integer values to do the comparisons is not a good idea as it can cause problems for large numbers.减去 integer 值来进行比较不是一个好主意,因为它可能会导致大量问题。

        
TreeSet<SimpleEntry<Integer, Integer>> treeSet =
        new TreeSet<>(new PairComparator());

Random r = new Random(23);
for (int i = 0; i < 10; i++) {
    int weight = r.nextInt(100);
    int source = r.nextInt(100);
    
    SimpleEntry<Integer, Integer> pair =
            new SimpleEntry<>(weight, source);
    treeSet.add(pair);
}

treeSet.forEach(System.out::println);

Prints the following (duplicate keys ignored)打印以下内容(忽略重复键)

18=43
22=17
24=30
27=48
81=95
83=47
89=70
90=10
94=87

The comparator比较器

static class PairComparator implements
        Comparator<AbstractMap.SimpleEntry<Integer, Integer>> {
    public int compare(
            AbstractMap.SimpleEntry<Integer, Integer> o1,
            AbstractMap.SimpleEntry<Integer, Integer> o2) {
        int key1 = o1.getKey();
        int key2 = o2.getKey();
        return key1 < key2 ? -1 : key1 > key2 ? 1 : 0;
    }
}

You can also specify the following as a comparator.您还可以将以下内容指定为比较器。

Comparator<AbstractMap.SimpleEntry<Integer,Integer>> pairComparator =
     (se1,se2)-> Integer.compare(se1.getKey(),se2.getKey());
        

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM