如何在 java 中使用 .contains() 方法查找完全相同的对象?
How to look up for exact same objects with .contains() method in java?
这个想法很简单我不需要添加已经在列表中的对象。我想问题是它创建了一个全新的对象,并且找不到与列表中已经存在的其他相同对象的匹配项(我可能完全错了)。
import java.util.Scanner;
import java.util.ArrayList;
public class archive {
private String indentifier;
private String name;
public archive(String indentifier,String name) {
this.name = name;
this.indentifier = indentifier;
}
public String toString() {
return this.indentifier + ": "+this.name;
}
public static void main(String[] args) {
ArrayList<archive> arhivs = new ArrayList<>();
Scanner scan = new Scanner(System.in);
String indentifier, name;
while(true) {
System.out.println("Indentifier? (empty will stop)");
indentifier = scan.nextLine();
if(indentifier.isEmpty()) {
break;
}
System.out.println("Name?(empty will stop)");
name = scan.nextLine();
if(name.isEmpty()) {
break;
}
archive newAr = new archive(indentifier,name);
if(arhivs.contains(newAr)) {
System.out.println("Item is on the list");
}else {
arhivs.add(newAr);
}
}
System.out.println("==Items==");
for(archive arhivi:arhivs) {
System.out.println(arhivi);
}
scan.close();
}
}
覆盖 equals
(&hashCode
)
您忽略了提供对继承的 Object#equals
and Object#hashCode
方法的覆盖。
equals
的默认实现使用标识进行比较,即“被比较的两个对象引用是否共享相同的隐式内存地址?”。
相反,您想比较两个对象的 内容。因此,您需要在自己的 equals
方法中编写内容比较代码。每当覆盖 equals
时,通常最好使用相同的逻辑覆盖 hashCode
。
你的IDE will assist you in writing that code. For example, in IntelliJ, see this documentation and this discussion.
此问题已在 Stack Overflow 上多次提及。所以 search to learn more.
记录
顺便说一句,您可以更简单地将 class 定义为 record。
record Archive ( String identifier , String name ) {}
当您的 class 的主要目的是透明且不可变地传达数据时,记录是合适的。
编译器隐式创建构造函数、getter、equals
& hashCode
和 toString
方法。
因此,假设您想比较 class 上的每个成员字段,那么使用 记录会使您的问题变得毫无实际意义 。 equals
和 hashCode
覆盖已为您提供。
这个想法很简单我不需要添加已经在列表中的对象。我想问题是它创建了一个全新的对象,并且找不到与列表中已经存在的其他相同对象的匹配项(我可能完全错了)。
import java.util.Scanner;
import java.util.ArrayList;
public class archive {
private String indentifier;
private String name;
public archive(String indentifier,String name) {
this.name = name;
this.indentifier = indentifier;
}
public String toString() {
return this.indentifier + ": "+this.name;
}
public static void main(String[] args) {
ArrayList<archive> arhivs = new ArrayList<>();
Scanner scan = new Scanner(System.in);
String indentifier, name;
while(true) {
System.out.println("Indentifier? (empty will stop)");
indentifier = scan.nextLine();
if(indentifier.isEmpty()) {
break;
}
System.out.println("Name?(empty will stop)");
name = scan.nextLine();
if(name.isEmpty()) {
break;
}
archive newAr = new archive(indentifier,name);
if(arhivs.contains(newAr)) {
System.out.println("Item is on the list");
}else {
arhivs.add(newAr);
}
}
System.out.println("==Items==");
for(archive arhivi:arhivs) {
System.out.println(arhivi);
}
scan.close();
}
}
覆盖 equals
(&hashCode
)
您忽略了提供对继承的 Object#equals
and Object#hashCode
方法的覆盖。
equals
的默认实现使用标识进行比较,即“被比较的两个对象引用是否共享相同的隐式内存地址?”。
相反,您想比较两个对象的 内容。因此,您需要在自己的 equals
方法中编写内容比较代码。每当覆盖 equals
时,通常最好使用相同的逻辑覆盖 hashCode
。
你的IDE will assist you in writing that code. For example, in IntelliJ, see this documentation and this discussion.
此问题已在 Stack Overflow 上多次提及。所以 search to learn more.
记录
顺便说一句,您可以更简单地将 class 定义为 record。
record Archive ( String identifier , String name ) {}
当您的 class 的主要目的是透明且不可变地传达数据时,记录是合适的。
编译器隐式创建构造函数、getter、equals
& hashCode
和 toString
方法。
因此,假设您想比较 class 上的每个成员字段,那么使用 记录会使您的问题变得毫无实际意义 。 equals
和 hashCode
覆盖已为您提供。