Question

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;
    }

}
Était-ce utile?

La solution

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);

Autres conseils

Your list contains ProductType not integers. i.e. This would work: productTypes.contains( new ProductType( 1 ) );

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top