Reputation: 16050
productTypes
_id = [1, 2, 3]
p.getId()
_id = 1
Why does this function always return false
? For instance, if p.getId() = 1
and productTypes = [1, 2, 3]
, it should return true, but it doesn't.
List<ProductType> productTypes = new ArrayList<ProductType>();
boolean result = productTypes.contains(p.getId()));
public class ProductType
{
private int _id;
private String _name;
public ProductType(int id)
{
this._id = id;
}
public ProductType(int id,String name)
{
this._id = id;
this._name = name;
}
public int getId()
{
return _id;
}
public void setId(int _id)
{
this._id = _id;
}
public String getName()
{
return _name;
}
public void setName(String _name)
{
this._name = _name;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final ProductType other = (ProductType) obj;
if (this._id != other._id)
return false;
return true;
}
}
Upvotes: 0
Views: 85
Reputation: 1561
Your list contains ProductType
not integers.
i.e. This would work:
productTypes.contains( new ProductType( 1 ) );
Upvotes: 0
Reputation: 85779
Because you're checking if it contains the id
, which will be autoboxed to Integer
:
productTypes.contains(p.getId()));
You should send the ProductType
instead:
productTypes.contains(p);
Upvotes: 3