在java中创建一组数组

时间:2021-08-12 21:20:54

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 Lists 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 ,那么AFAICT就没有简单而优雅的方法。问题是数组一方面不会覆盖equals()和hashCode()。另一方面,HashSet类不提供向其传递一些“策略”的可能性,该策略将在外部实现哈希码和相等计算(类似于比较器)。因此,您可以考虑使用自定义比较器创建TreeSet。不幸的是,我不知道阵列比较器的任何实现,所以很可能你需要编写自己的。

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 Lists 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 ,那么AFAICT就没有简单而优雅的方法。问题是数组一方面不会覆盖equals()和hashCode()。另一方面,HashSet类不提供向其传递一些“策略”的可能性,该策略将在外部实现哈希码和相等计算(类似于比较器)。因此,您可以考虑使用自定义比较器创建TreeSet。不幸的是,我不知道阵列比较器的任何实现,所以很可能你需要编写自己的。

If it's ok for you to have Set<List<String>>, you can consider advice in other answers.

如果您可以使用Set >,则可以在其他答案中考虑建议。