Java 为什么set.contains方法仅适用于某些对象?
我正在尝试看看set.contains方法是如何工作的。我为数组、列表、集合和类类型制作了4个不同的集合。但它不适用于数组和类类型Java 为什么set.contains方法仅适用于某些对象?,java,arrays,list,collections,set,Java,Arrays,List,Collections,Set,我正在尝试看看set.contains方法是如何工作的。我为数组、列表、集合和类类型制作了4个不同的集合。但它不适用于数组和类类型 class MainClass { public static void main(String[] args) { Set<int[]> arraySet = new HashSet<>(); Set<List<Integer>> listSet = new HashSet&l
class MainClass {
public static void main(String[] args) {
Set<int[]> arraySet = new HashSet<>();
Set<List<Integer>> listSet = new HashSet<>();
Set<Set<Integer>> setSet = new HashSet<>();
Set<Numbers> numSet = new HashSet<>();
int[] array1 = {1,2,3};
int[] array2 = {1,2,3};
List<Integer> list1 = Arrays.asList(1,2,3);
List<Integer> list2 = Arrays.asList(1,2,3);
Set<Integer> set1 = new HashSet<>(Arrays.asList(1,2,3));
Set<Integer> set2 = new HashSet<>(Arrays.asList(1,2,3));
Numbers num1 = new Numbers(1,2,3);
Numbers num2 = new Numbers(1,2,3);
arraySet.add(array1);
listSet.add(list1);
setSet.add(set1);
numSet.add(num1);
// false
System.out.println(arraySet.contains(array2));
// true
System.out.println(listSet.contains(list2));
// true
System.out.println(setSet.contains(set2));
// false
System.out.println(numSet.contains(num2));
}
}
class Numbers{
int num1;
int num2;
int num3;
public Numbers(int num1, int num2, int num3){
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}
}
class类main类{
公共静态void main(字符串[]args){
Set arraySet=new HashSet();
Set listSet=新的HashSet();
Set setSet=新的HashSet();
Set numSet=new HashSet();
int[]数组1={1,2,3};
int[]array2={1,2,3};
list1=Arrays.asList(1,2,3);
list2=Arrays.asList(1,2,3);
Set set1=新的HashSet(Arrays.asList(1,2,3));
Set set2=新的HashSet(Arrays.asList(1,2,3));
数字num1=新的数字(1,2,3);
数字num2=新数字(1,2,3);
arraySet.add(array1);
添加(list1);
setSet.add(set1);
numSet.add(num1);
//假的
System.out.println(arraySet.contains(array2));
//真的
System.out.println(listSet.contains(list2));
//真的
System.out.println(setSet.contains(set2));
//假的
System.out.println(numSet.contains(num2));
}
}
班级人数{
int num1;
int num2;
int num3;
公共编号(整数m1、整数m2、整数m3){
this.num1=num1;
this.num2=num2;
此值为0.num3=num3;
}
}
set.contains仅适用于集合对象的原因是什么?contains()
方法基于equals()
方法,因此第一种方法仅在第二种方法被覆盖时才有效
例如,数组不重写equals()
方法。因此,一个数组引用等于另一个数组引用当且仅当两个数组引用都指向内存中的同一个数组对象(array1==array2
),因此contains()
对它们不起作用
另一方面,ArrayList
class确实覆盖了equals()
方法,因此如果两个ArrayList
以相同的顺序包含相同的对象,则它们将是equals
(如果所包含的对象覆盖了equals()
方法,则也适用于)。因此,contains()
对它们有效。contains()
方法基于equals()
方法,因此第一个方法仅在第二个方法被覆盖时有效
例如,数组不重写equals()
方法。因此,一个数组引用等于另一个数组引用当且仅当两个数组引用都指向内存中的同一个数组对象(array1==array2
),因此contains()
对它们不起作用
另一方面,ArrayList
class确实覆盖了equals()
方法,因此如果两个ArrayList
以相同的顺序包含相同的对象,则它们将是equals
(如果所包含的对象覆盖了equals()
方法,则也适用于)。因此,<代码>容器(<)/代码> <强>对它们进行工作< /强>。对于包含对象的对象的哈希集, ,可以重写对象类中的均等()和HASCODE()方法,哈希集会认为两个不同的对象在保持相同内容时是相等的。
你可以用IDE。< P>自动生成重写方法,对于包含你所写类的对象的哈什集,你可以重写对象类中的均等()和HASCODE()方法,哈希集会认为两个不同的对象在保持相同内容时是相等的。
您可以使用IDE自动生成覆盖方法。尝试给出System.out.println(arraySet);System.out.println(列表集);系统输出打印LN(设置集);系统输出打印项次(numSet);在你离开之前。您将了解其中的区别。@Harmandepsingkalsi它与set.contains方法有关系吗?它与set.contains()方法没有直接关系,但这是您可以推断的方法,您的理解哪里出了问题。请尝试给出System.out.println(arraySet);System.out.println(列表集);系统输出打印LN(设置集);系统输出打印项次(numSet);在你离开之前。您将了解其中的区别。@Harmandepsingkalsi它与set.contains方法有关系吗?它与set.contains()方法没有直接关系,但这就是您可以推断的方法,您的理解哪里出错了。您还必须重写hashCode()
,要使其在集合中工作,还必须重写hashCode()
,才能使其在集合中工作。