[英]How can I check if an ArrayList contains an Object with a specific field value?
[英]How can I remove a specific object from an arrayList and how can I check if it contains this object?
我有一個arrayList類型的購物車。 當我向購物車中添加新商品時,我將首先檢查購物車中是否已有該商品。 但是cart.contains(item)方法不起作用,即使購物車中有相同的商品,它也會返回false。 第二個問題是我無法從購物車arrayList中刪除此項目對象。 我的代碼如下所示:
@Controller
@RequestMapping("/addTo.htm")
public class AddToController{
@SuppressWarnings("unchecked")
@RequestMapping(method=RequestMethod.GET)
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
String action = request.getParameter("action");
System.out.println(action);
ModelAndView mv = new ModelAndView();
ArrayList<Item> cart;
if(session.getAttribute("cart") != null) {
cart = (ArrayList<Item>) session.getAttribute("cart");
}else {
cart = new ArrayList<Item>();
}
if(action.equals("addToCart")) {
long itemId = Long.parseLong(request.getParameter("itemId"));
ItemDAO itemDao = new ItemDAO();
Item item = itemDao.get(itemId);
System.out.println("111"+cart.contains(item));
if (!cart.contains(item)) {
cart.add(item);
}
double total = 0;
int count = 0;
for (Item i : cart) {
total = total + i.getPrice();
count += 1;
}
session.setAttribute("cart", cart);
mv.addObject("total", total);
mv.addObject("count", count);
mv.setViewName("User/viewCart");
}
if(action.equals("remove")){
System.out.println("cart size is" + cart.size());
Long itemId = Long.parseLong(request.getParameter("item"));
ItemDAO itemDao= new ItemDAO();
Item item = itemDao.get(itemId);
System.out.println(cart.contains(item));
session.setAttribute("cart", cart);
System.out.println(cart.size());
}
return mv;
}
}
誰能幫我解決這個問題? 謝謝!!
您將需要向Item類添加.equals
方法,以便ArrayLists知道如何將兩個不同的對象一起比較。 在此過程中,我們還應該添加一個hashCode
方法。 這主要對Sets有用,但是在需要時最好將其作為備份。
我們可以使用.indexOf(Item)方法獲取對象在列表中的位置。 如果數字返回-1。 那么它不在列表中。 如果它是0或更大,那么它就在里面,我們可以使用索引來刪除該項目。
public class Item{
private String type;
public Item(String type){
this.type = type;
}
public String getType(){
return type;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((type == null) ? 0 : type.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Item))
return false;
Item other = (Item) obj;
if (type == null) {
if (other.type != null)
return false;
} else if (!type.equals(other.type))
return false;
return true;
}
}
現在我們有了一個.equals和hashcode。 現在,我們可以在ArrayList中對其進行比較。
ArrayList<Item> itemList = new ArrayList<Item>();
// Fill the list
itemList.add(new Item("Banana"));
itemList.add(new Item("Toaster"));
itemList.add(new Item("Screw Driver"));
Item item = new Item("Hand Grenade");
itemList.add(item);
int index = itemList.indexOf(item);
if( index != -1 ){
System.out.println("The item is in index " + index);
// Remove the item and store it in a variable
Item removedItem = itemList.remove(index);
System.out.println("We removed " + removedItem.getType() + " from the list.");
}
您必須重寫.equals
項的.equals
方法。默認情況下,Java通常將比較對象引用,而不是對象的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.