Java 字节数组集
我有一个 byte[]
的哈希集,我想测试该集合中是否有新的 byte[]
。问题在于,Java 似乎是在测试 byte[] 实例是否相同,而不是测试字节数组中的实际值是否相同。
换句话说,请考虑以下代码:
public class Test
{
public static void main(String[] args)
{
java.util.HashSet<byte[]> set=new java.util.HashSet<byte[]>();
set.add(new String("abc").getBytes());
System.out.println(set.contains(new String("abc").getBytes()));
}
}
此代码打印出 false
,我希望它打印出 true
。我该怎么做呢?
I have a HashSet of byte[]
s and I would like to test whether a new byte[]
is in that set. The problem is that Java seems to be testing whether the byte[]
instances are the same rather than testing whether the actual values in the byte arrays are the same.
In other words, consider the following code:
public class Test
{
public static void main(String[] args)
{
java.util.HashSet<byte[]> set=new java.util.HashSet<byte[]>();
set.add(new String("abc").getBytes());
System.out.println(set.contains(new String("abc").getBytes()));
}
}
This code prints out false
and I would like it to print out true
. How should I go about doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用 ByteBuffer.wrap 包装每个字节数组,这将为您提供正确的 equals 和 hashCode 行为。请注意在 ByteBuffer 上调用的方法(不要修改数组或提前其指针)。
You can wrap each byte array using
ByteBuffer.wrap
, which will provide the right equals and hashCode behavior for you. Just be careful what methods you call on theByteBuffer
(that you don't modify the array or advance its pointer).您可以创建一个 ByteArray 类来包装字节数组并按照您想要的方式测试相等性。然后你就会有一个
Set
。You could create a
ByteArray
class that wraps the byte arrays and tests for equality the way you want. Then you'd have aSet<ByteArray>
.您可以定义自己的包装类,但最简单的方法可能是将数组“包装”到 ArrayList 中并使用
HashSet
。You could define your own wrapper class, but probably the easiest thing to do is to "wrap" the arrays into ArrayLists and use a
HashSet<ArrayList>
.现代(截至目前的解决方案)
注意:
您不应该在不提供
Charset< 的情况下将
String
转换为byte[]
/code> 结果变得依赖于默认的Charset
运行时,这通常不是一个好的字符集并且可以更改。.asReadOnlyBuffer()
很重要!Modern ( as of right now solution )
NOTES:
You should never convert a
String
to abyte[]
without providing aCharset
the results become runtime dependant based on the defaultCharset
which is usually not a good one and can change..asReadOnlyBuffer()
is important!您可以避免包装器和愚蠢的 hashCode 问题(嘿,像 byte[] 这样的标准东西没有 hashCode 对吧?):
使用 TreeSet 而不是 HashSet 并在实例化时提供 byte[] 比较器:
如果您得到一个字节[] HashSet b 来自其他地方,将变量 a 初始化为 TreeSet,然后使用 a.addAll(b);这样,即使 b 包含重复项,a 也不会。
You can avoid wrappers and the stupid hashCode problem (hey, a standard thing like a byte[] doesn't have hashCode right?):
Use TreeSet instead of HashSet and provide a byte[] comparator at instantiation time:
If you get a byte[] HashSet b from somewhere else, initialize your variable a before as TreeSet and then use a.addAll(b); This way, even if b contained duplicates, a does not.
实施亚当·克鲁姆的想法。
Implementing Adam Crume's idea.