使用 equals 方法比较虚构对象的问题

Problem with comparing made-up objects with equals method

我正在尝试制作一个程序,将对象存档与 Arraylist 存档进行比较,并且仅在不存在的情况下才将其添加到 arraylist 中,但我真的很挣扎。我试过修改 for 循环,添加一个新的布尔值来检查是否已经存在相同的存档,但似乎无法让它只添加唯一的存档。任何建议将不胜感激。

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        ArrayList<Archive> archives = new ArrayList<>();
        while (true) {
            System.out.println("Identifier? (empty will stop)");
            String identifier = scanner.nextLine();
            if (identifier.isEmpty()) {
                break;
            }
            System.out.println("Name? (empty will stop)");
            String name = scanner.nextLine();
            if (name.isEmpty()) {
                break;
            }
            Archive archive = new Archive(identifier, name);

            boolean contains = false;
            for (int i = 0; i < archives.size(); i++) {
                if (archive.equals(archives.get(i))) {
                    contains = true;
                    break;
                }
            }
            if (contains == false) {
                archives.add(archive);
            }
        }

        for (Archive i : archives) {
            System.out.println(i.getIdentifier() + ": " + i.getName());
        }
    }
}



public class Archive {

    private String identifier;
    private String name;

    public Archive(String identifier, String name) {
        this.identifier = identifier;
        this.name = name;
    }

    public String getIdentifier() {
        return this.identifier;
    }

    public String getName() {
        return this.name;
    }

    public boolean equals(Archive archive, Object compared) {

        if (compared == archive) {
            return true;
        }
        if (!(compared instanceof Archive)) {
            return false;
        }

        Archive compare = (Archive) compared;
        if (compare.getIdentifier().equals(archive.getIdentifier()) && compare.getName().equals(archive.getName())) {
            return true;
        }

        return false;
    }

}

//我也刚刚意识到,我可以做的不是整个带有布尔值的 for 循环包含的东西

if(archives.contains(archive){
contains=true;}

equals 方法的签名错误。 应该是

public boolean equals(Object compared) {
    if(compared==null || !(compared instanceof Archive)){
        return false;
    }

    Archive compare = (Archive) compared;
    if (compare.getIdentifier().equals(this.getIdentifier()) && compare.getName().equals(this.getName())) {
        return true;
    }

    return false;
}