如何使Java Hashtable.containsKey适用于Array?
发布时间:2020-12-15 00:36:38 所属栏目:Java 来源:网络整理
导读:很抱歉问这个问题,但我是 Java的新手. Hashtablebyte[],byte[] map = new Hashtablebyte[],byte[]();byte[] temp = {1,-1,0};map.put(temp,temp);byte[] temp2 = {1,0};;System.err.println(map.containsKey(temp2)); 不适用于.containsKey(因为打印结果为“
很抱歉问这个问题,但我是
Java的新手.
Hashtable<byte[],byte[]> map = new Hashtable<byte[],byte[]>(); byte[] temp = {1,-1,0}; map.put(temp,temp); byte[] temp2 = {1,0};; System.err.println(map.containsKey(temp2)); 不适用于.containsKey(因为打印结果为“False”) Hashtable<Integer,Integer> mapint = new Hashtable<Integer,Integer>(); int i = 5; mapint.put(i,i); int j = 5; System.err.println(mapint.containsKey(j)); 工作(打印结果为“True”) 我知道它与对象引用有关,但在搜索后无法达到任何解决方案… 无论如何我可以使用Hashtable来查找具有Array类型的键吗?我只是想测试一个特定的数组是否在Hashtable中作为键… 任何点击都会很棒.谢谢!!! 解决方法
你不能在HashTable / HashMap中使用数组作为键,因为它们不会覆盖Object的equals的默认实现,这意味着temp.equals(temp2)当且仅当temp == temp2时,这在你的情况下是不正确的案件.
您可以使用Set< Byte>或列表<字节>而不是键的字节[]. 例如 : Hashtable<List<Byte>,Byte[]> map = new Hashtable<List<Byte>,Byte[]>(); Byte[] temp = {1,0}; map.put(Arrays.asList(temp),temp); Byte[] temp2 = {1,0};; System.err.println(map.containsKey(Arrays.asList(temp2))); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |