Search for object by keyword
I have an ArrayList of objects type Contact. Every Contact contains first name, last name, phone number and e-mail (all of them String).
I have to write a method which search for a keyword in the records and return true if this keyword has been found anywhere in it. The method I came up with is:
public boolean match(Object otherObject)
if (otherObject ==null)
else if (getClass() != otherObject.getClass())
Contact otherContact = (Contact)otherObject;
return (first.equals(otherContact.first) ||
and it works fine. The problem is that in this case it returns true only if the two values are exactly the same. I need to modify it in a way that even if the keyword is "ohn" records with name "John" to return true. It has to work with any keyword and to search for it in all object variables. Thanks
Re: Search for object by keyword
Use String.contains( ... ) instead of String.equals( ... ).