简体   繁体   English

Java中使用比较器错误的TreeMap构造函数

[英]TreeMap Constructor using comparator error in Java

I want to make a TreeMap, so that each time an entry is inserted in the TreeMap - the entry is sorted based on value on the run. 我想制作一个TreeMap,以便每次在TreeMap中插入一个条目时-该条目将根据运行时的值进行排序。 (needs O(logN) time.) So, I define a TreeMap with its constructor like below :: I dont understand where is the problem... i am confused. (需要O(logN)时间。)因此,我定义了一个TreeMap及其构造函数,如下所示:::我不明白问题出在哪里……我很困惑。 Can anyone please explain me the error/problem ? 谁能解释这个错误/问题吗?

Code :: 代码::

Map<String,Integer> tm = 
  new TreeMap<String,Integer>(new Comparator<Map.Entry<String,Integer>>(){

        @Override
        public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
            throw new UnsupportedOperationException("Not supported yet."); // implement logic here
        }
    });

The comparator is for the key 比较器是关键

Map<String,Integer> tm =
                    new TreeMap<String,Integer>(new Comparator<String>(){
                        @Override
                        public int compare(String o1, String o2) {
                            throw new UnsupportedOperationException("Not supported yet."); // implement logic here
                        }
                    });

You should sort entries based on the entry's key. 您应该根据条目的键对条目进行排序。 So comparator should compare two strings here. 因此,比较器应在此处比较两个字符串。 (new Comparator(){...}) as suggested in other answer here. (new Comparator(){...}),如此处其他答案所建议。

Take a look at constructor documentation: http://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html#TreeMap(java.util.Comparator) 看看构造函数文档: http : //docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html#TreeMap(java.util.Comparator)

If you really need to sort by the value, you should create a new object for the key, also containing your value: 如果确实需要按值排序,则应为键创建一个新对象,其中也包含您的值:

class MyKey {
    String str;
    Integer i;
    ...
}
Map<MyKey,Integer> tm = new TreeMap<MyKey, Integer>(new Comparator<MyKey>(){
    @Override
    public int compare(MyKey o1, MyKey o2) {
         String str1 = o1.str;
         String str2 = o2.str;
         // implement logic here
    }
};

You need to provide comparator, which compares keys, rather then full map entries. 您需要提供比较器,它比较键,而不是完整的映射条目。 So to create TreeMap<String,X> ( X is any type) you need to provide Comparator<String> . 因此,要创建TreeMap<String,X>X是任何类型),您需要提供Comparator<String> This can be done like this: 可以这样完成:

Map<String,Integer> tm = new TreeMap<String,Integer>(new Comparator<String>(){
                        @Override
                        public int compare(String o1, String o2) {
                            // implement logic here
                            // sample implementation below 
                            return o1.comparateTo(o2)
                        }
                    });

If natural ordering of strings (see: javadoc for String#compareTo) is fine for you, you can just write: 如果对字符串进行自然排序(请参阅: javadoc中的String#compareTo)适合您,则可以编写:

Map<String,Integer> tm = new TreeMap<String,Integer>();

Notice that in both cases, ordering of keys is the same. 请注意,在两种情况下,键的顺序都是相同的。 In both cases String#compareTo method is used to compare keys. 在这两种情况下,都使用String#compareTo方法比较键。 Only in the first example we are providing comparator explicitly and in the second one we using the fact that String implements Comparable . 仅在第一个示例中,我们显式提供了比较器,而在第二个示例中,我们使用String实现Comparable的事实。

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

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