java 两个set是否相等,java
我尝试检查两个集合,我需要两个结果都为TRUE,但事实并非如此。 我不知道为什么结果之一是返回false。 请帮助我,非常感谢。
类CollectionExample:
Set set1 = new HashSet<>();
set1.add(new String("A"));
set1.add(new String("B"));
Set set2 = new HashSet<>();
set2.add("A");
set2.add("B");
System.out.println("set1.equal(set2): "+ set1.equals(set2));
Set set3 = new HashSet<>();
set3.add(new Person1("A", "1"));
set3.add(new Person1("B", "1"));
Set set4 = new HashSet<>();
set4.add(new Person1("A", "1"));
set4.add(new Person1("B", "1"));
System.out.println("set3.equal(set4): "+ set3.equals(set4));
实体Person1:
public class Person1 {
String firstName;
String lastName;
public Person1(String firstName, String lastName) {
// TODO Auto-generated constructor stub
this.firstName = firstName;
this.lastName = lastName;
}
public String firstName() { return firstName;}
public String lastName() { return lastName;}
}
结果:
set1.equal(set2): true
set3.equal(set4): false
java 两个set是否相等,java
我尝试检查两个集合,我需要两个结果都为TRUE,但事实并非如此。 我不知道为什么结果之一是返回false。 请帮助我,非常感谢。
类CollectionExample:
Set set1 = new HashSet<>();
set1.add(new String("A"));
set1.add(new String("B"));
Set set2 = new HashSet<>();
set2.add("A");
set2.add("B");
System.out.println("set1.equal(set2): "+ set1.equals(set2));
Set set3 = new HashSet<>();
set3.add(new Person1("A", "1"));
set3.add(new Person1("B", "1"));
Set set4 = new HashSet<>();
set4.add(new Person1("A", "1"));
set4.add(new Person1("B", "1"));
System.out.println("set3.equal(set4): "+ set3.equals(set4));
实体Person1:
public class Person1 {
String firstName;
String lastName;
public Person1(String firstName, String lastName) {
// TODO Auto-generated constructor stub
this.firstName = firstName;
this.lastName = lastName;
}
public String firstName() { return firstName;}
public String lastName() { return lastName;}
}
结果:
set1.equal(set2): true
set3.equal(set4): false