简体   繁体   English

TreeMap没有返回所有键

[英]TreeMap not returning all keys

I have a requirement like I need to sort the keys inside a Map according to some sequence which is defined on a property file entry.So that output should come according to sequence which user defining on the property entry.For this I tried using TreeMap and Comparator . 我有一个要求,就像我需要根据在属性文件条目上定义的一些序列对Map内的键进行排序。因此输出应该根据用户在属性条目上定义的顺序来进行。为此我尝试使用TreeMapComparator

My property file entry is 我的属性文件条目是

seq=People,Object,Environment,Message,Service

Following is my code: 以下是我的代码:

Properties prop=new Properties();
prop.load(new FileInputStream("D:\\vignesh\\sample.properties"));
final String sequence=prop.getProperty("seq");
// Display elements
     final String sequence=prop.getProperty("seq");
     System.out.println("sequence got here is "+sequence); 
      //Defined Comparator
      Comparator<String> comparator = new Comparator<String>() {
          @Override
          public int compare(String key1, String key2) {
              return sequence.indexOf(key1) - sequence.indexOf(key2);
          }
      };
SortedMap<String,String> lhm = new TreeMap<String,String>(comparator);
       // Put elements to the map
          lhm.put("Object", "biu");
          lhm.put("Message", "nuios");
          lhm.put("Service", "sdfe");
          lhm.put("People", "dfdfh");
          lhm.put("Environment", "qwe");
          lhm.put("Other", "names");
          lhm.put("Elements", "ioup");          //Not showing in output
          lhm.put("Rand", "uiy");               //Not showing in output
//Iterating Map
      for(Entry<String, String> entry : lhm.entrySet()) {
            System.out.println(entry.getKey());
        }

Output 产量

sequence got here is People,Object,Environment,Message,Service
Other
People
Object
Environment
Message
Service

Now I have some problems with this code. 现在我对这段代码有些问题。

  • I have nearly 8 elements inside my map.But output showing only six elements.Why last two elements didn't come? 我的地图里面有近8个元素。但是输出只显示了6个元素。为什么最后两个元素没有来?

  • The values which are not matching with sequence are coming at top 与序列不匹配的值位于顶部
    now.I would like to get those at bottom.Is there a way? 现在。我想把那些放在最底层。有办法吗?

  • Here I have declared the string which is reading from properties file as final so that I can't change the property every time.When I 在这里,我已经将从属性文件中读取的字符串声明为final这样我每次都无法更改属性。当我
    remove final Identifier it is showing error in my IDE. 删除最终标识符,它在我的IDE中显示错误。 How can I 我怎样才能
    avoid that? 避免那样?

  • My keys inside HashMap may not fully equals with the property entry 我在HashMap中的键可能与属性条目不完全相等
    sequence.So I need to check whether that sequence are contains in my HashMap key.Do I need to change my Comparator for that? 所以我需要检查我的HashMap密钥中是否包含该序列。我需要更改我的Comparator吗?

It's not that your elements are not printed, it's that they are not added in the map ! 这并不是说你的元素没有打印,而是它们没有被添加到地图中!

In a sorted Map you have : 在有序地图中,您有:

A sorted map performs all key comparisons using its compareTo (or compare) method, so two keys that are deemed equal by this method are, from the standpoint of the sorted map, equal. 有序映射使用compareTo(或compare)方法执行所有键比较,因此从排序映射的角度来看,通过此方法认为相等的两个键是相等的。

But a map cannot have two equal keys : 但是地图不能有两个相同的键:

A map cannot contain duplicate keys; 地图不能包含重复的键; each key can map to at most one value. 每个键最多可以映射一个值。

But when comparing Element with Other your comparator returns zero. 但是当比较ElementOther时,比较器返回零。 It considers those two strings as equals so the latest is not added to the map. 它将这两个字符串视为等号,因此最新的字符串不会添加到地图中。

As suggested in the javadoc of SortedMap : 正如SortedMap的javadoc中所建议的那样:

ote that the ordering maintained by a sorted map (whether or not an explicit comparator is provided) must be consistent with equals if the sorted map is to correctly implement the Map interface. 如果有序映射要正确实现Map接口,则由有序映射维护的排序(无论是否提供显式比较器)必须与equals一致。

So I would advise you to make sure that when your comparator returns 0 it is because the two elements compared are indeed equals. 所以我建议你确保当比较器返回0时,这是因为比较的两个元素确实是等于。 A very "naive" way to do that would be : 一种非常“天真”的方式是:

Comparator<String> comparator = new Comparator<String>() {
      @Override
      public int compare(String key1, String key2) {
          int returned = sequence.indexOf(key1) - sequence.indexOf(key2);

          if (returned == 0 && !key1.equals(key2))
              returned = -1;

          return returned;

      }
  };

First of all, indexOf returns -1 for any substrings it cannot locate in a String . 首先, indexOf为它在String找不到的任何子字符串返回-1

When you are inserting "Other" , indexOf("Other") returns -1 and indexOf("People") returns 0 , which adds "Other" to the map in the first position. 当您插入"Other"indexOf("Other")返回-1并且indexOf("People")返回0 ,这会在第一个位置向地图添加"Other"

However, when adding other strings, they are tested against "Other" , which returns -1 . 但是,在添加其他字符串时,会针对"Other"对其进行测试,返回-1 Unfortunately, all other elements are also missing in the property value string and thus return -1 . 不幸的是,属性值字符串中也缺少所有其他元素,因此返回-1 Which means sequence.indexOf(key1) - sequence.indexOf(key2) is 0 and they all are assumed equal to "Other" . 这意味着sequence.indexOf(key1) - sequence.indexOf(key2)0并且假设它们都等于"Other"

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

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