我不想把相等的向量加在一起(或者不加列表(。当我在一个集合中加两个相等的数字时,只加一个,但当我加两个相同的向量时,两个相加。有人知道怎么解决吗?我的代码:
int[] a = new int[] {0,1,2,3,4,5,6};
int[] b = new int[] {0,1,2,3,4,5,6};
Set setExample = new HashSet();
setExample.add(a);
setExample.add(b);
集合"setExample"有一个&b.
数组不使用值相等,因此集合不会将两个类似的数组视为重复数组。您可以使用列表。List
的标准实现覆盖equals
和hashCode
,使得具有相同内容的两个列表被视为相等。
List<Integer> a = Arrays.asList(1,2,3);
List<Integer> b = Arrays.asList(1,2,3);
Set<List<Integer>> set = new HashSet<>();
set.add(a);
set.add(b);
// set will only contain one list