gpt4 book ai didi

java - LinkedHashMap containsKey 或 containsValue

转载 作者:行者123 更新时间:2023-12-02 22:32:43 25 4
gpt4 key购买 nike

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

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

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

最佳答案

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

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 执行浅哈希码计算,不适合嵌套数组。

关于java - LinkedHashMap containsKey 或 containsValue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14165830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com