如何防止用户在ArrayList Java中输入重复的对象?

如何防止用户在ArrayList Java中输入重复的对象?,java,Java,因此,我试图阻止用户在检查他们输入的第一个元素的基础上输入重复的条目(对象)。我试过几种方法,但它一直在我身上游走。有没有关于如何阻止这种情况的建议?另外,请注意此处的缩进没有正确表示。程序本身并非如此 import java.util.Scanner; import java.util.ArrayList; public class Inventory { private ArrayList<Book> inventory = new ArrayList<>();

因此,我试图阻止用户在检查他们输入的第一个元素的基础上输入重复的条目(对象)。我试过几种方法,但它一直在我身上游走。有没有关于如何阻止这种情况的建议?另外,请注意此处的缩进没有正确表示。程序本身并非如此

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


public class Inventory {
private ArrayList<Book> inventory = new ArrayList<>();
private Scanner scan = new Scanner(System.in);

public void addBook() { //open method addBook
    System.out.print("Enter the book id number: ");
    int id = scan.nextInt();
    if (inventory.contains(id) == true){
        System.out.print("This book is already in the inventory!");
        id--;
    } 
    else if (inventory.contains(id) == false) {
        
        System.out.print("Enter book title: ");
        scan.next();
        String title = scan.nextLine();
        System.out.print("Enter book price: ");
        double price = scan.nextDouble();
        inventory.add(new Book(id, title, price));
    }
} //Close method addBook
import java.util.Scanner;
导入java.util.ArrayList;
公共类目录{
私有ArrayList清单=新ArrayList();
专用扫描仪扫描=新扫描仪(System.in);
public void addBook(){//open方法addBook
System.out.print(“输入图书id号:”);
int id=scan.nextInt();
if(inventory.contains(id)==true){
System.out.print(“这本书已经在库存中了!”);
id--;
} 
else if(inventory.contains(id)=false){
系统输出打印(“输入书名:”);
scan.next();
String title=scan.nextLine();
系统输出打印(“输入图书价格:”);
double price=scan.nextDouble();
存货.增加(新书(编号、名称、价格));
}
}//关闭方法addBook
设置
为了防止重复,可以使用而不是
ArrayList
。如果此集合尚未包含指定的元素,则该方法将返回
true

如果您还想保留元素的顺序,您可能需要签出
LinkedHashSet


如果您不愿意使用
Set
,则必须循环遍历ArrayList的元素以检查它是否包含书籍。这是因为您的ArraList包含的
book
不是其id的
int
。因此条件
inventory.contains(id)
将始终返回false

您可以这样做:

        int id = scan.nextInt();
        for (Book book : inventory) {
            if(book.id == id){
                //print duplicate and return
            }
        }
        //add the book and return

作为其他答案的替代方案:

if (inventory.stream().anyMatch(b -> b.getId() == id))
    ...

使用Shivansh Narayan提供的LinkedHashSet的方法似乎恰到好处。您可以使用的另一种方法是使用HashSet检查ID并使用ArrayList存储书籍。 此外,else if条件是冗余的

public class Inventory {
private ArrayList<Book> inventory = new ArrayList<>();
private Scanner scan = new Scanner(System.in);
private HashSet<Integer> bookIDs = new HashSet<Integer>(); // a set to store all unique book IDs
public void addBook() { //open method addBook
  System.out.print("Enter the book id number: ");
  int id = scan.nextInt();
  if (bookIDs.contains(id)){
      System.out.print("This book is already in the inventory!");
      id--;
  } 
  else{
    
    System.out.print("Enter book title: ");
    scan.next();
    String title = scan.nextLine();
    System.out.print("Enter book price: ");
    double price = scan.nextDouble();
    inventory.add(new Book(id, title, price));
    // don't forget to add the id to the set
    bookIDs.add(id);
 }
} //Close method addBook
公共类目录{
私有ArrayList清单=新ArrayList();
专用扫描仪扫描=新扫描仪(System.in);
private HashSet bookIDs=new HashSet();//存储所有唯一图书ID的集合
public void addBook(){//open方法addBook
System.out.print(“输入图书id号:”);
int id=scan.nextInt();
if(bookIDs.contains(id)){
System.out.print(“这本书已经在库存中了!”);
id--;
} 
否则{
系统输出打印(“输入书名:”);
scan.next();
String title=scan.nextLine();
系统输出打印(“输入图书价格:”);
double price=scan.nextDouble();
存货.增加(新书(编号、名称、价格));
//别忘了将id添加到集合中
bookIDs.add(id);
}
}//关闭方法addBook

使用
Set
而不是
ArrayList
来防止重复。我认为哈希表或映射更好,如果键已经存在,你可以检查它。现在你的代码不起作用,因为检查id将不等于实际对象。哦,你也不需要检查两次,在第一个if之后,else部分是guar在没有重复项的情况下运行(假设您已经移动到哈希表或映射)@Martheen它可以使用
Set
实现,只需要在
Book
类中实现
equals
hashCode
方法。之后就不需要外部检查了。太好了!我确实在尝试解决hash Set选项。这不是因为我不喜欢HashSet,而是我正在努力让它工作没有它,这工作很好。