Problem in HashSet - Saving object
I have list of product.That list contain check-box & qty in Text-box.IF the user selected particular product need to save that object in HashSet.
CheckBox Name Price Qty
[] Pen 21 TextBox
[] aaa 11 TextBox
[] bbb 25 TextBox
When the user select the checkbox then save that object into 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);
}
Problem is :
I selected one particular product.After some time I am changing qty for that saved product. How we can do it:
How to take saved object & change some property & saved back it.
Please help me...
Thanks in advance..
You could add a couple of methods to your Product
class:
public int getQuantity()
void setQuantity(int quantity)
However, you should not modify an object that is in a HashSet
: it's safer to remove it and add a new one. In fact, if you modify an object that is in a HashSet
you will modify its hash value!
Here is some code:
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);
}
}
and here is the Product
class:
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;
}
}
Note that I've overridden hashCode
and equals
without considering the field quantity
. A better option could be having two classes: Product
with only name
and price
, and ProductOrder
with two fields: a Product
and a quantity
.
Just obtain a reference to the object from your set, modify its value using the methods exposed by the object and the next retrieval will give you the updated object. can you also post the equals() and hashcode() method of your product class, if you have overriden them?
so something like this :
//set.add(savedProduct);
//savedProduct.setQuantity(newQuantity);
//now the product saved in the set has the new quantity.
So you don't need to save anything back in, just obtain the reference to the object and modify the object using the reference.
Isn't it easier to use a MAP
for this? You can use the product names as keys and just retrieve the Product object by name from your collection.
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);
}
}
Note that for this solution product names have to be unique. If 2 products have the same name, you cannot use this unless you either enforce unique names or add a field to products that has is unique
While using the Set interface, the straight forward way is to write a search method that iterates through all the elements of the set and searches for the given product Name say 'Pen'. Then you can set the quantity on the searched product. This way you don't have to put the element back to the set.
As HashSet
doesn't have any getter method you'd have to get the iterator then iterate over your set until you find the one you're looking for, change it then add.
So get out the iterator then:
Product p;
while(itr.hasNext()){
p = itr.next();
if(p.equals(name) {
itr.remove();
//do stuff to it;
s.add(p);
break;
}
}
Do some cleanup in case you haven't found it etc..
精彩评论