[Solved] How to compare objects by property


I assume you have a name variable in your Product class. So what you would want to do is compare the name variable to the String. Something like this:

private String name;
Product  laptop = new Product(1, "Laptop", "Type", 1350.25, "black");
Product  mouse = new Product(2, "Mouse", "Type", 50.50, "black");

String check = new String("laptop");
if(check.equals(laptop.getName()) {
    System.out.println("Match!");
} else {
    System.out.println("No match...");
}

Here’s a good SO discussion about the proper use of instanceof:

What is the ‘instanceof’ operator used for in Java?

solved How to compare objects by property