在 ArrayDeque containsAll(Collection c)、contains(Object o)、equals(Object o) 中给出预期但 equals(Collection c) 给出意外结果
In ArrayDeque containsAll(Collection c), contains(Object o), equals(Object o) give expected but equals(Collection c) gives unexpected result
我有以下“人物”class
-
import java.util.Objects;
public class Person implements Comparable<Person> {
private int id;
private String name;
Person(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
@Override
public int compareTo(Person person) {
int myReturn = 0;
int minLength = 0;
boolean equal = false;
int id1 = id;
int id2 = person.id;
String name1 = name;
String name2 = person.name;
if (name1.length() > name2.length()) {
minLength = name2.length();
} else if (name1.length() < name2.length()) {
minLength = name1.length();
} else {
minLength = name1.length();
equal = true;
}
if (id1 > id2) {
myReturn = 1;
} else if (id1 < id2) {
myReturn = -1;
} else {
int i = 0;
for (i = 0; i < minLength; i++) {
if (name1.charAt(i) > name2.charAt(i)) {
myReturn = 1;
break;
} else if (name1.charAt(i) < name2.charAt(i)) {
myReturn = -1;
break;
} else {
continue;
}
}
if(i == minLength) {
if (equal) {
myReturn = 0;
} else if (name1.length() > minLength) {
myReturn = 1;
} else {
myReturn = -1;
}
}
}
return myReturn;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null || getClass() != obj.getClass()) {
return false;
}
Person person = (Person) obj;
return id == person.id && Objects.equals(name, person.name);
}
@Override
public int hashCode() {
return Objects.hash(id, name);
}
}
现在以下代码的行为符合预期 -
代码 1:
/* equals(Object object) - method */
Person person3 = new Person(2, "Neha");
Person person4 = new Person(2, "Neha");
Person person5 = new Person(2, "Sneha");
System.out.println(person3.equals(person4));
System.out.println(person3.equals(person5));
输出-
true
false
代码 2:
/* contains(Object object) - method */
ArrayDeque<Person> arrayDeque13 = new ArrayDeque<>(List.of(
new Person(99, "Needhi"),
new Person(12, "Papori"),
new Person(78, "Pallavi")
));
Person person1 = new Person(12, "Papori");
Person person2 = new Person(13, "Puja");
System.out.println(arrayDeque13.contains(person1));
System.out.println(arrayDeque13.contains(person2));
输出-
true
false
代码 3:
/* containsAll(Collection collection) - method */
ArrayDeque<Person> arrayDeque14 = new ArrayDeque<>(List.of(
new Person(3, "Namita"),
new Person(2, "Mridu"),
new Person(82, "Priyadarshini"),
new Person(89, "Tina"),
new Person(13, "Kajal")
));
ArrayDeque<Person> arrayDeque15 = new ArrayDeque<>(List.of(
new Person(3, "Namita"),
new Person(2, "Mridu"),
new Person(13, "Kajal")
));
ArrayDeque<Person> arrayDeque16 = new ArrayDeque<>(List.of(
new Person(3, "Namita"),
new Person(2, "Mridu"),
new Person(13, "Neha")
));
System.out.println(arrayDeque14.containsAll(arrayDeque15));
System.out.println(arrayDeque14.containsAll(arrayDeque16));
输出-
true
false
但是下面的代码没有按预期运行 -
代码 4:
/* equals(Collection collection) - method */
ArrayDeque<Person> arrayDeque17 = new ArrayDeque<>(List.of(
new Person(56, "Leena"),
new Person(66, "Nidhi"),
new Person(76, "Payal")
));
ArrayDeque<Person> arrayDeque18 = new ArrayDeque<>(List.of(
new Person(56, "Leena"),
new Person(66, "Nidhi"),
new Person(76, "Payal")
));
ArrayDeque<Person> arrayDeque19 = new ArrayDeque<>(List.of(
new Person(56, "Leena"),
new Person(66, "Nidhi"),
new Person(76, "Payel")
));
System.out.println(arrayDeque17.equals(arrayDeque18));
System.out.println(arrayDeque17.equals(arrayDeque19));
输出-
false
false
在这里,我希望第一个输出是 true
但它是 false
如 -
https://docs.oracle.com/javase/7/docs/api/java/util/ArrayDeque.html
Methods inherited from interface java.util.Collection
addAll, containsAll, equals, hashCode, removeAll, retainAll
那么,我哪里错了?
正如评论者已经提到的,ArrayDeque
没有实现 equals()
。如果不确定,请查看 javadoc 并搜索“等于”。如果它在“从 class java.lang.Object 继承的方法”下,那么您知道它正在使用 Object
的 equals
实现:
接口不能有equals()
方法实现,因为Java不允许default
等值方法。如果 class 实现了多个接口,每个接口都包含一个默认的“equals”方法,应该使用哪一个?所以它是从 Collection
接口继承的有点无关紧要。 Javadoc 在那里列出它是因为 Collection
接口明确定义了 equals()
和 collection-specific javadoc。
我有以下“人物”class
-
import java.util.Objects;
public class Person implements Comparable<Person> {
private int id;
private String name;
Person(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
@Override
public int compareTo(Person person) {
int myReturn = 0;
int minLength = 0;
boolean equal = false;
int id1 = id;
int id2 = person.id;
String name1 = name;
String name2 = person.name;
if (name1.length() > name2.length()) {
minLength = name2.length();
} else if (name1.length() < name2.length()) {
minLength = name1.length();
} else {
minLength = name1.length();
equal = true;
}
if (id1 > id2) {
myReturn = 1;
} else if (id1 < id2) {
myReturn = -1;
} else {
int i = 0;
for (i = 0; i < minLength; i++) {
if (name1.charAt(i) > name2.charAt(i)) {
myReturn = 1;
break;
} else if (name1.charAt(i) < name2.charAt(i)) {
myReturn = -1;
break;
} else {
continue;
}
}
if(i == minLength) {
if (equal) {
myReturn = 0;
} else if (name1.length() > minLength) {
myReturn = 1;
} else {
myReturn = -1;
}
}
}
return myReturn;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null || getClass() != obj.getClass()) {
return false;
}
Person person = (Person) obj;
return id == person.id && Objects.equals(name, person.name);
}
@Override
public int hashCode() {
return Objects.hash(id, name);
}
}
现在以下代码的行为符合预期 -
代码 1:
/* equals(Object object) - method */
Person person3 = new Person(2, "Neha");
Person person4 = new Person(2, "Neha");
Person person5 = new Person(2, "Sneha");
System.out.println(person3.equals(person4));
System.out.println(person3.equals(person5));
输出-
true
false
代码 2:
/* contains(Object object) - method */
ArrayDeque<Person> arrayDeque13 = new ArrayDeque<>(List.of(
new Person(99, "Needhi"),
new Person(12, "Papori"),
new Person(78, "Pallavi")
));
Person person1 = new Person(12, "Papori");
Person person2 = new Person(13, "Puja");
System.out.println(arrayDeque13.contains(person1));
System.out.println(arrayDeque13.contains(person2));
输出-
true
false
代码 3:
/* containsAll(Collection collection) - method */
ArrayDeque<Person> arrayDeque14 = new ArrayDeque<>(List.of(
new Person(3, "Namita"),
new Person(2, "Mridu"),
new Person(82, "Priyadarshini"),
new Person(89, "Tina"),
new Person(13, "Kajal")
));
ArrayDeque<Person> arrayDeque15 = new ArrayDeque<>(List.of(
new Person(3, "Namita"),
new Person(2, "Mridu"),
new Person(13, "Kajal")
));
ArrayDeque<Person> arrayDeque16 = new ArrayDeque<>(List.of(
new Person(3, "Namita"),
new Person(2, "Mridu"),
new Person(13, "Neha")
));
System.out.println(arrayDeque14.containsAll(arrayDeque15));
System.out.println(arrayDeque14.containsAll(arrayDeque16));
输出-
true
false
但是下面的代码没有按预期运行 -
代码 4:
/* equals(Collection collection) - method */
ArrayDeque<Person> arrayDeque17 = new ArrayDeque<>(List.of(
new Person(56, "Leena"),
new Person(66, "Nidhi"),
new Person(76, "Payal")
));
ArrayDeque<Person> arrayDeque18 = new ArrayDeque<>(List.of(
new Person(56, "Leena"),
new Person(66, "Nidhi"),
new Person(76, "Payal")
));
ArrayDeque<Person> arrayDeque19 = new ArrayDeque<>(List.of(
new Person(56, "Leena"),
new Person(66, "Nidhi"),
new Person(76, "Payel")
));
System.out.println(arrayDeque17.equals(arrayDeque18));
System.out.println(arrayDeque17.equals(arrayDeque19));
输出-
false
false
在这里,我希望第一个输出是 true
但它是 false
如 -
https://docs.oracle.com/javase/7/docs/api/java/util/ArrayDeque.html
Methods inherited from interface java.util.Collection
addAll, containsAll, equals, hashCode, removeAll, retainAll
那么,我哪里错了?
正如评论者已经提到的,ArrayDeque
没有实现 equals()
。如果不确定,请查看 javadoc 并搜索“等于”。如果它在“从 class java.lang.Object 继承的方法”下,那么您知道它正在使用 Object
的 equals
实现:
接口不能有equals()
方法实现,因为Java不允许default
等值方法。如果 class 实现了多个接口,每个接口都包含一个默认的“equals”方法,应该使用哪一个?所以它是从 Collection
接口继承的有点无关紧要。 Javadoc 在那里列出它是因为 Collection
接口明确定义了 equals()
和 collection-specific javadoc。