LinkedHashMap包含Key或containsValue

时间:2013-01-04 22:12:05

标签: java linkedhashmap

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

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

这似乎有效。我想我必须做一些额外的检查,以避免重复的重复。

2 个答案:

答案 0 :(得分: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进行浅层哈希码计算,这不适合嵌套数组。

答案 1 :(得分:3)

containsKeycontainsValue功能可能适用于equals()和/或hashCode()。对于String[],正如您正确指出的那样,这些可能就参考而言。除非您可以将比较器与containsKey()containsValue()一起传递,否则您的选项就是创建自己的类来执行String[]所做的操作,除了它可以计算equals()和{ {1}}使得具有相同值的对象相等。