2

为什么会发生这种情况,我该如何避免?我假设它只检查引用,但这不是很方便。

import java.util.LinkedHashMap;

public class CheckingLinkedHashMap {

    public static void main(String[] args) {
        LinkedHashMap<String[], String[]> linkedh = new LinkedHashMap<>();
        String[] a = "A".split(",");
        String[] ab = "A,B".split(",");
        linkedh.put(a, ab);
        String[] aa = "A".split(",");
        String[] aabb = "A,B".split(",");
        System.out.println("Contains key: " + linkedh.containsKey(aa));
        System.out.println("Contains value: " + linkedh.containsValue(aabb));

        System.out.println("Contains key: " + linkedh.containsKey(a));
        System.out.println("Contains value: " + linkedh.containsValue(ab));
    }
}

--

Contains key: false
Contains value: false
Contains key: true
Contains value: true

更新

import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class CheckingLinkedHashMap {

    public static void main(String[] args) {

        LinkedHashMap< List<String>, List<String>> linkedh = new LinkedHashMap<>();

        String[] a = "A".split(",");
        String[] ab = "A,B".split(",");
        List<String> al = Arrays.asList(a);
        List<String> abl = Arrays.asList(ab);
        linkedh.put(al, abl);

        String[] aa = "A".split(",");
        String[] aabb = "A,B".split(",");
        List<String> aal = Arrays.asList(aa);
        List<String> aabbl = Arrays.asList(aabb);
        linkedh.put(aal, aabbl);

        String[] aaa = "A,B".split(",");
        String[] aaabbb = "A,B".split(",");
        List<String> aaal = Arrays.asList(aaa);
        List<String> aaabbbl = Arrays.asList(aaabbb);
        linkedh.put(aaal, aaabbbl);

        String[] aaaa = "B,A".split(",");
        String[] aaaabbbb = "A,B".split(",");
        List<String> aaaal = Arrays.asList(aaaa);
        List<String> aaaabbbbl = Arrays.asList(aaaabbbb);
        linkedh.put(aaaal, aaaabbbbl);

        Iterator it = linkedh.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry) it.next();
            System.out.println("Key : " + pairs.getKey() + " Value : " + pairs.getValue());
        }

        System.out.println("Contains key: " + linkedh.containsKey(aal));
        System.out.println("Contains value: " + linkedh.containsValue(aabbl));

        System.out.println("Contains key: " + linkedh.containsKey(al));
        System.out.println("Contains value: " + linkedh.containsValue(abl));
    }
}

--

Key : [A] Value : [A, B]
Key : [A, B] Value : [A, B]
Key : [B, A] Value : [A, B]
Contains key: true
Contains value: true
Contains key: true
Contains value: true

这似乎可行。我想我必须做一些额外的检查以避免密钥重复。

4

2 回答 2

4

问题是您使用的是默认情况下通过引用比较相等的数组。要解决您的问题,您应该将它们封装到一个类中并覆盖适当的方法:

class StringBundle {
  String[] strings;

  @Override
  public boolean equals(Object o) {
    // various checks
    return Array.deepEquals(this.strings, o.strings);
  }

  @Override
  public int hashCode() {
    return Arrays.hashCode(strings);
  }
}

您需要一种方法来提供您自己的版本,hashCode()否则您将无法在散列容器中成功使用此类。注意进行Arrays.hashCode不适合嵌套数组的浅哈希码计算。

于 2013-01-04T22:19:34.373 回答
3

containsKeycontainsValue功能可能在和equals()/或方面起作用hashCode()。因为String[],正如您正确指出的那样,这些可能是在参考方面。除非您可以将比较器与containsKey()and一起传递,否则您containsValue()的选择变成创建自己的类来执行该String[]操作,除了它可以计算equals()并且hashCode()以具有相同值的对象相等的方式。

于 2013-01-04T22:17:44.703 回答