使用 "Boolean equals()" 比较保存在 ArrayList 中的对象
Comparing Objects saved in ArrayList using "Boolean equals()"
我有一个 Class
叫 Products
。这个 class 有一个 name
和一个 price
。
我可以通过以下方式创建产品:
Product book = new Product();
book.setName("book");
book.setPrice(3);
之后我应该检查这个产品是否已经存在于 ArrayList
我应该将它保存到,如果不存在,那么我就把它放在那里。我应该使用以下方法执行此操作:
public boolean equals(Object obj){
}
问题是,如果我应该保存产品的 ArrayList
在 public static void main
中创建并初始化,而这个 [=21] =] 是在 ArrayList
之前创建的?
我应该像这样将 Class
本身设为 ArrayList
吗?
public class ArrayList<Product>{
}
不用担心它的存在。由于您在产品 class 中重写了 equals 方法,您可以在下面尝试这样做
if(yourArayList.contains(book)){
// it existed in the list
}else{
yourArayList.add(book);
}
当您调用 contains 方法时,它会在内部调用 Product 方法的 equals 方法,而不是传递给它的对象。
this boolean
is created before the ArrayList
even exists?
您误解了 equals
的工作方式。它不会 "create" a boolean
直到你用某个对象作为参数调用它,并且它 returns a boolean
基于你传递的对象的属性。
当您定义 equals
方法时,您提供了决定相等性的代码,但此时您没有决定任何事情:
@Override
public boolean equals(Object obj){
if (obj == this) return true
if (!(obj instanceof Product)) return false;
Product other = (Product)obj;
if (!other.getName().equals(getName())) return false;
if (!other.getPrice() == getPrice()) return false;
return true;
}
@Override
public int hashCode() {
return 31*getName().hashCode() + getPrice();
}
现在您可以使用 equals
来判断列表中是否有您的 Product
,方法有以下两种:
- 使用
contains
- 此方法调用 equals
来检查包含,或
- 迭代所有对象,并手动调用
equals
。
您不需要创建自己的 ArrayList class。
如果你实现了你等于权利,当你执行 myList.contains(book)
时,它会被调用来检查你是否在列表中
实际上有一种结构可以让您跳过在代码中执行检查。你可以使用java.util.HashSet
。它确保不能添加重复项。此外,它 returns 表示是否添加了元素的布尔值。例如
Set<Product> mySet = new HashSet<>();
boolean added = mySet.add(book);
请不要忘记易于遵循的规则 - 在定义 equals 时,也要定义 hashCode。如果您使用 IDE,您可以轻松地生成它们。
此布尔值不是在您的 ArrayList 存在之前创建的。
您需要像这样覆盖 Product 的 equals() 方法:
@Override
public boolean equals(Object obj) {
if(obj == this) return true; // Both objects have the same reference -> the objects are equal
if((obj == null) || (obj.getClass() != this.getClass())) return false; // Classes are different -> objects are different
Product p = (Product) obj; // Cast obj into Product
if( (this.getPrice() == p.getPrice()) && (this.getName().equals(p.getName())) ) return true; // Price and name are the same -> both products are the same
return false; // At this point the two objects can't be equal
}
这是创建 ArrayList 的方式:
ArrayList<Product> products = new ArrayList<Product>();
这就是当产品不在列表中时添加产品的方法:
if(!products.contains(yourProduct)){ // Checks if yourProduct is not contained in products
products.add(yourProduct); // adds yourProduct to products
}
我有一个 Class
叫 Products
。这个 class 有一个 name
和一个 price
。
我可以通过以下方式创建产品:
Product book = new Product();
book.setName("book");
book.setPrice(3);
之后我应该检查这个产品是否已经存在于 ArrayList
我应该将它保存到,如果不存在,那么我就把它放在那里。我应该使用以下方法执行此操作:
public boolean equals(Object obj){
}
问题是,如果我应该保存产品的 ArrayList
在 public static void main
中创建并初始化,而这个 [=21] =] 是在 ArrayList
之前创建的?
我应该像这样将 Class
本身设为 ArrayList
吗?
public class ArrayList<Product>{
}
不用担心它的存在。由于您在产品 class 中重写了 equals 方法,您可以在下面尝试这样做
if(yourArayList.contains(book)){
// it existed in the list
}else{
yourArayList.add(book);
}
当您调用 contains 方法时,它会在内部调用 Product 方法的 equals 方法,而不是传递给它的对象。
this
boolean
is created before theArrayList
even exists?
您误解了 equals
的工作方式。它不会 "create" a boolean
直到你用某个对象作为参数调用它,并且它 returns a boolean
基于你传递的对象的属性。
当您定义 equals
方法时,您提供了决定相等性的代码,但此时您没有决定任何事情:
@Override
public boolean equals(Object obj){
if (obj == this) return true
if (!(obj instanceof Product)) return false;
Product other = (Product)obj;
if (!other.getName().equals(getName())) return false;
if (!other.getPrice() == getPrice()) return false;
return true;
}
@Override
public int hashCode() {
return 31*getName().hashCode() + getPrice();
}
现在您可以使用 equals
来判断列表中是否有您的 Product
,方法有以下两种:
- 使用
contains
- 此方法调用equals
来检查包含,或 - 迭代所有对象,并手动调用
equals
。
您不需要创建自己的 ArrayList class。
如果你实现了你等于权利,当你执行 myList.contains(book)
实际上有一种结构可以让您跳过在代码中执行检查。你可以使用java.util.HashSet
。它确保不能添加重复项。此外,它 returns 表示是否添加了元素的布尔值。例如
Set<Product> mySet = new HashSet<>();
boolean added = mySet.add(book);
请不要忘记易于遵循的规则 - 在定义 equals 时,也要定义 hashCode。如果您使用 IDE,您可以轻松地生成它们。
此布尔值不是在您的 ArrayList 存在之前创建的。
您需要像这样覆盖 Product 的 equals() 方法:
@Override
public boolean equals(Object obj) {
if(obj == this) return true; // Both objects have the same reference -> the objects are equal
if((obj == null) || (obj.getClass() != this.getClass())) return false; // Classes are different -> objects are different
Product p = (Product) obj; // Cast obj into Product
if( (this.getPrice() == p.getPrice()) && (this.getName().equals(p.getName())) ) return true; // Price and name are the same -> both products are the same
return false; // At this point the two objects can't be equal
}
这是创建 ArrayList 的方式:
ArrayList<Product> products = new ArrayList<Product>();
这就是当产品不在列表中时添加产品的方法:
if(!products.contains(yourProduct)){ // Checks if yourProduct is not contained in products
products.add(yourProduct); // adds yourProduct to products
}