"how to compare objects attributes in an arraylist?" Code Answer

1

you have already got the product out of the list<product> in the following statement:

system.out.println(products.get(i));

now, that you have got product, now to get it's id, you can just call it's getid() method:

if (product.get(i).getid() == productid) {
    // return this product.
}

i would also suggest you to use enhanced for-loop instead of the traditional loop like this:

for (product product: products) {
    // now you have the product. just get the id
    if (product.getid() == productid) {
        return product;
    }
}

also, you should change the type of productid from integer to int. you don't need a wrapper type there.

By Alak on October 13 2022

Answers related to “how to compare objects attributes in an arraylist?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.