I want to do something like
我想做点什么
Set <String[]> strSet = new HashSet <String[]> ();
Is there an easy way to make a Set of arrays in Java, or do I have to code my own implementation? Adding an object to a Set checks the Object using equals(), which does not work for arrays.
有一种简单的方法可以用Java创建一组数组,还是我必须编写自己的实现代码?将对象添加到Set使用equals()检查Object,这对数组不起作用。
3 个解决方案
#1
12
Arrays don't override equals
and hashCode
, and so the HashSet
will compare them based on reference equality only. Consider using List
s instead:
数组不会覆盖equals和hashCode,因此HashSet将仅基于引用相等性来比较它们。请考虑使用列表:
Set<List<String>> strSet = new HashSet<List<String>>();
From the List.equals
documentation:
从List.equals文档:
Returns
true
if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal.当且仅当指定的对象也是列表时,返回true,两个列表具有相同的大小,并且两个列表中的所有对应元素对都相等。
#2
6
Use a Set<List<String>>
. You can use Arrays.asList
and List.toArray
for conversions, as necessary.
使用Set
>。您可以根据需要使用Arrays.asList和List.toArray进行转换。
#3
0
If you really need Set<String[]>
, there is no easy and elegant way for that, AFAICT. The problem is that arrays do not not override equals()
and hashCode()
, on the one hand. On the other hand HashSet
class do not provide a possibility to pass some "strategy" to it that would implement hash code and equality computation externally (something like Comparator
). So you might consider creating TreeSet
with a custom comparator. Unfortunately, I don't know of any implementation of array comparator, so most likely you will need to write your own.
如果你真的需要Set
If it's ok for you to have Set<List<String>>
, you can consider advice in other answers.
如果您可以使用Set
>,则可以在其他答案中考虑建议。
#1
12
Arrays don't override equals
and hashCode
, and so the HashSet
will compare them based on reference equality only. Consider using List
s instead:
数组不会覆盖equals和hashCode,因此HashSet将仅基于引用相等性来比较它们。请考虑使用列表:
Set<List<String>> strSet = new HashSet<List<String>>();
From the List.equals
documentation:
从List.equals文档:
Returns
true
if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal.当且仅当指定的对象也是列表时,返回true,两个列表具有相同的大小,并且两个列表中的所有对应元素对都相等。
#2
6
Use a Set<List<String>>
. You can use Arrays.asList
and List.toArray
for conversions, as necessary.
使用Set
>。您可以根据需要使用Arrays.asList和List.toArray进行转换。
#3
0
If you really need Set<String[]>
, there is no easy and elegant way for that, AFAICT. The problem is that arrays do not not override equals()
and hashCode()
, on the one hand. On the other hand HashSet
class do not provide a possibility to pass some "strategy" to it that would implement hash code and equality computation externally (something like Comparator
). So you might consider creating TreeSet
with a custom comparator. Unfortunately, I don't know of any implementation of array comparator, so most likely you will need to write your own.
如果你真的需要Set
If it's ok for you to have Set<List<String>>
, you can consider advice in other answers.
如果您可以使用Set
>,则可以在其他答案中考虑建议。