简体   繁体   English

Java中的TreeMap

[英]TreeMap in Java

import java.util.Comparator;
import java.util.TreeMap;
public class Manager {

     public static void main(String[] args) {

          TreeMap tmap1 = new TreeMap(new C.D());
          tmap1.put(new C(9, 1), 9);
          tmap1.put(new C(0, 10), 19);
          tmap1.put(new C(1, 8), 92);
          tmap1.put(new C(8, 0), 91);
          tmap1.put(new C(5, 4), 39);
          tmap1.put(new C(5,4), 73);//this line

          System.out.println(tmap1);
        }
}

  class C {
      int i, j;

         C(int i, int j) {
             this.i = i;
             this.j = j;
           }

        public String toString() {
           return "(i=" + i + ",j=" + j + ")";
         }

       static class D implements Comparator {
             public int compare(Object obj1, Object obj2) {
               return ((C)obj1).i - ((C)obj2).i;
             }
      }

  }

//As we know TreeMap sorts based on the key and here I am passing it a comparator and its sorting based on variable i.When I tried to enter new entry in treemap1 as I mentioned it in comment as //this line , its adding this new entry while deleting previous same entry. ///我们知道TreeMap是基于键排序的,在这里我将基于变量i的比较器及其排序传递给它。当我尝试在treemap1中输入新条目时,正如我在注释中提到的那样//this line ,它的添加此新条目,同时删除以前的相同条目。 Well Class C does not override hashcode() and equals() method so the new entry is completedly different from it previous entry as they have different hashcode() based on Object class, so both entries should retain in map but only new one is added and previous one deleted. 好吧,C类不会覆盖hashcode()和equals()方法,因此新条目与以前的条目完全不同,因为它们基于Object类具有不同的hashcode(),因此两个条目都应保留在map中,但仅添加一个新条目和上一个被删除。

In case of line 在线路的情况下

tmap1.put(new C(5,4), 73);

your comparator returns 0 so it is duplicate for JVM and it overrides it. 您的比较器返回0,因此它对于JVM是重复的,并且将其覆盖。

Put one debug statement like below and re-run your program you will understand the fact.. 如下所示放置一条调试语句,然后重新运行程序,您将了解事实。

     public int compare(Object obj1, Object obj2) {
       System.out.println("in compare difference is : "+ (((C)obj1).i - ((C)obj2).i));
       return ((C)obj1).i - ((C)obj2).i;
     }

you will get below output..see the zero(0) at last 您将获得以下输出..最后看到零(0)

in compare : -9
in compare : -8
in compare : 1
in compare : 7
in compare : -1
in compare : 4
in compare : -4
in compare : -3
in compare : 4
in compare : -3
in compare : 0

In your case the problem is because if you use TreeMap and provide Comparator you should also provide correct equals method. 就您而言,问题是因为如果您使用TreeMap并提供Comparator那么您还应该提供正确的equals方法。 It's in JavaDoc of TreeMap: 在TreeMap的JavaDoc中:

Note that the ordering maintained by a tree map, like any sorted map, and whether or not an explicit comparator is provided, must be consistent with equals if this sorted map is to correctly implement the Map interface 请注意,如果树排序图要正确实现Map接口,则树映射所维护的排序(与任何排序的映射一样)以及是否提供显式比较器必须与equals一致。

Your equals method is inherited from Object and it's not consistent with your Comparator. equals方法是从Object继承的,它与Comparator不一致。

Have a loot at TreeMap put method and compare it with HashMap put method. TreeMap put方法上有一个战利品,并将其与HashMap put方法进行比较。 TreeMap's version use compareTo method only instead of equals like in HashMap's put. TreeMap的版本仅使用compareTo方法,而不是HashMap的put中的equals

Btw you shouldn't use raw types since Java 1.5 顺便说一句,从Java 1.5开始,您不应该使用原始类型

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

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