2011-09-12 35 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个产品具有相同的名称,则不能使用此名称,除非您强制使用唯一名称或将字段添加到具有唯一性的产品