如何比较ArrayList中的Objects属性?

我是Java的新手,我已经用尽了所有现有的资源来寻找答案。 我想知道是否可以访问Objects first属性以查看它是否与特定整数匹配?

例如,我试图通过它的产品ID搜索它来获取我的数据库中的产品。 因此,如果我创建两个产品,例如, Product ipad = new Product(12345, "iPad", 125.0, DeptCode.COMPUTER);Product ipod = new Product(12356, "iPod", 125.0, DeptCode.ELECTRONICS); (我在下面包含了这个Product类),并将它们添加到Arraylist中,例如List products = new ArrayList(); 如何通过此ArrayList循环以通过其ID查找该产品? 这是我正在研究的方法:

 List products = new ArrayList(); @Override public Product getProduct(int productId) { // TODO Auto-generated method stub for(int i=0; i<products.size(); i++){ //if statement would go here //I was trying: if (Product.getId() == productId) { System.out.println(products.get(i)); } return null; }` 

我知道我可以在for循环中包含一个条件语句但是我无法弄清楚如何访问Product类中的getId()方法来比较productId参数?

  package productdb; public class Product { private Integer id; private String name; private double price; private DeptCode dept; public Product(String name, double price, DeptCode code) { this(null, name, price, code); } public Product(Integer id, String name, double price, DeptCode code) { this.id = id; this.name = name; this.price = price; this.dept = code; } public String getName() { return name; } public double getPrice() { return price; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public DeptCode getDept() { return dept; } public void setDept(DeptCode dept) { this.dept = dept; } public void setName(String name) { this.name = name; } public void setPrice(double price) { this.price = price; } @Override public String toString() { String info = String.format("Product [productId:%d, name: %s, dept: %s, price: %.2f", id, name, dept, price); return info; } } 

请告诉我

您已从以下语句中的List中获取了List

 System.out.println(products.get(i)); 

现在,你已经有了Product ,现在要获得它的id ,你可以调用它的getId()方法:

 if (product.get(i).getId() == productId) { // Return this product. } 

我还建议你使用增强的for-loop代替传统的循环,如下所示:

 for (Product product: products) { // Now you have the product. Just get the Id if (product.getId() == productId) { return product; } } 

此外,您应该将productId的类型从Integer更改为int 。 那里你不需要包装类型。

您是否考虑过使用HashMap(或LinkedHashMap)而不是数组。 这样你可以使用productId作为键,产品作为值吗?

这将使您无需遍历整个数组即可获取对象。

根据你的评论专栏//I was trying: if (Product.getId() == productId)我认为你在摔倒的地方是使用Product (大写P)。 你需要的是:

if (products.get(i).getId() == productId)

另外,你没有退回你找到的产品……

该表单的问题在于:a)你必须在列表中找到两次产品(一次在条件中,然后在打印结果时再次 – 如果你返回产品则第三次)和b)它将抛出一个空指针例如,您要查找的产品不在列表中。

这是一种更好,更紧凑的方式:

 @Override public Product getProduct(int productId) { for(Product product : products) { if (productId == product.getId()) { System.out.println(product.toString()); return product; } } return null; } 

为了比较ArrayList Objects,在CustomObject Class Like Employee中使覆盖相等的函数。

 ArrayList list; Employee emp; //suppose you have some number of items in that list and emp object contains some value. int size=list.size(); for(int i=0;i 

假设这是你的Employee类,在那个类中你需要覆盖相等的函数。

 class Employee{ int age; String name; public int getAge() { return this.age; } public String getName() { return this.name; } public void setAge(int emp_age) { this.age=emp_age; } public void setName(String emp_name) { this.name=emp_name; } @Override public boolean equal(Employee emp) { boolean isEqual=false; if(emp!=null && emp instanceof Employee) { isEqual=(this.age==emp.age); } return isEqual; } } 

我希望这个解决方案可以帮助您检查相等的值并比较值。

谢谢