2011-09-12 32 views
1

我有產品列表。該列表包含複選框& qty在Text-box.IF中,用戶選擇的特定產品需要將該對象保存在HashSet中。HashSet中的問題 - 保存對象

CheckBox Name Price Qty 
[]   Pen 21 TextBox 
[]   aaa 11 TextBox 
[]   bbb 25 TextBox 

當用戶選中複選框後,將該對象保存到HashSet中。

Set<Product> s = new HashSet<Product>(); 
Product product = new Product(); 
product.setName("Pen"); 
product.setPrice(21.00); 
product.setName(10); 
//s.add(product); 
if(!s.add(product)) 
    System.out.println("Duplicate detected : " + product); 
} 

問題是:

我選擇了一個特定的product.After一段時間,我改變了數量救產品。 我們如何做到這一點:

如何把保存的對象&改變一些屬性&保存回來。

請幫我...

在此先感謝..

回答

2

你可以一對夫婦的方法添加到您的Product類:

  • public int getQuantity()
  • void setQuantity(int quantity)

但是,你不應該修改HashSet中的對象:刪除它並添加一個新對象會更安全。事實上,如果您修改了一個HashSet中的對象,您將修改其哈希值!

下面是一些代碼:

public class Main { 

    public static void main(String[] args) { 
     new Main().go(); 
    } 

    public void go() { 
     Product p1 = new Product(); 
     p1.setName("P1"); 
     p1.setPrice(2.5); 
     p1.setQuantity(1); 

     Product p2 = new Product(); 
     p2.setName("P2"); 
     p2.setPrice(5.3); 
     p2.setQuantity(1); 

     Set<Product> products = new HashSet<Product>(); 
     products.add(p1); 
     products.add(p2); 

     System.out.println(products); 

     // now let's assume that you want to change quantity of P1 

     Product newp1 = new Product(); 
     newp1.setName("P1"); 
     newp1.setPrice(2.5); 
     newp1.setQuantity(2); 

     if (products.contains(newp1)) { 
      products.remove(newp1); 
     } 
     products.add(newp1); 

     System.out.println("after update:"); 
     System.out.println(products); 
    } 

} 

這裏是Product類:

public class Product { 

    String name; 
    double price; 
    int quantity; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public double getPrice() { 
     return price; 
    } 

    public void setPrice(double price) { 
     this.price = price; 
    } 

    public int getQuantity() { 
     return quantity; 
    } 

    public void setQuantity(int quantity) { 
     this.quantity = quantity; 
    } 

    @Override 
    public String toString() { 
     return "Product [name=" + name + ", price=" + price + ", quantity=" 
       + quantity + "]"; 
    } 

    @Override 
    public int hashCode() { 
     final int prime = 31; 
     int result = 1; 
     result = prime * result + ((name == null) ? 0 : name.hashCode()); 
     long temp; 
     temp = Double.doubleToLongBits(price); 
     result = prime * result + (int) (temp^(temp >>> 32)); 
     return result; 
    } 

    @Override 
    public boolean equals(Object obj) { 
     if (this == obj) 
      return true; 
     if (obj == null) 
      return false; 
     if (getClass() != obj.getClass()) 
      return false; 
     Product other = (Product) obj; 
     if (name == null) { 
      if (other.name != null) 
       return false; 
     } else if (!name.equals(other.name)) 
      return false; 
     if (Double.doubleToLongBits(price) != Double 
       .doubleToLongBits(other.price)) 
      return false; 
     return true; 
    } 

} 

請注意,我已經覆蓋hashCodeequals不考慮外地quantity。更好的選擇可以有兩個類別:Product只有namepriceProductOrder有兩個字段:Productquantity

+0

我添加了這些setter&getter.eg在設置product.setQty(10.00)setInstance之前。 ADDD(產品);之後,我想要拍攝該對象並更改並再次保存...如何操作? – Piraba

+0

不要這樣做:從集合中刪除舊對象並添加新對象。這更安全,因爲如果你改變了一個對象的集合,你可以改變它的哈希碼! – MarcoS

1

只需從您的集合中獲取對象的引用,使用對象公開的方法修改其值,然後下一次檢索將爲您提供更新的對象。 如果您覆蓋了產品類,您是否也可以發佈產品類的equals()和hashcode()方法?

所以是這樣的:

//set.add(savedProduct); 
//savedProduct.setQuantity(newQuantity); 
//now the product saved in the set has the new quantity. 

所以你不需要重新使用參考保存任何東西,只是獲得參考對象和修改的對象。

+0

是的,它可以做到這一點。但如何獲得保存對象的引用... – Piraba

+0

所以,如果你有一個對象,你想修改;檢查它是否存在於集合中並對其進行修改,並且不需要將其保存回來。或者,如果您沒有對象引用,則需要遍歷集合,在每個元素上應用搜索參數(如果iteratedProduct.getProductName()=「SearchingName」和iteratedProduct.getProductName()= oldQuantity,則iteratedProduct.setQuantity(newQuantity);) – Scorpion

0

由於HashSet沒有任何getter方法,您必須獲取迭代器,然後迭代您的集合,直到找到所需的集合,然後對其進行更改並添加。

因此,走出去,然後迭代器:

Product p; 
while(itr.hasNext()){ 
    p = itr.next(); 
    if(p.equals(name) { 
     itr.remove(); 
     //do stuff to it; 
     s.add(p); 
     break; 
    } 
} 

做的情況下,一些清理你有沒有發現它等等。

0

雖然使用設置界面中,直接的方式就是寫一搜索方法遍歷集合中的所有元素並搜索給定的產品名稱說'筆'。然後您可以在搜索到的產品上設置數量。這樣您就不必將元素放回設置。

1

難道使用MAP更容易嗎?您可以使用產品名稱作爲關鍵字,只需從集合中按名稱檢索Product對象。

Map<String, Product> products = new HashMap<String, Product>(); 

public void addProduct(Product product){ 
    if(products.get(product.getName())!=null){ 
    products.get(product.getName()).addQuantity(product.getQuantity()); 
    }else{ 
     products.put(product.getName(), product); 
    } 
    } 

請注意,對於此解決方案,產品名稱必須是唯一的。如果2個產品具有相同的名稱,則不能使用此名稱,除非您強制使用唯一名稱或將字段添加到具有唯一性的產品