I have a ArrayList with custom objects. I want to search inside this ArrayList for Strings.
The class for the objects look like this:
public class Datapoint implements Serializable {
private String stateBased;
private String name;
private String priority;
private String mainNumber;
private String groupadress;
private String dptID;
public Datapoint(){
}
public String getMainNumber() {
return mainNumber;
}
public void setMainNumber(String mainNumber) {
this.mainNumber = mainNumber;
}
public String getName() {
return name;
}
..and so on
I know how to search for a string in a ArrayList but how to do that in a ArrayList with my custom objects:
ArrayList<String> searchList = new ArrayList<String>();
String search = "a";
int searchListLength = searchList.size();
for (int i = 0; i < searchListLength; i++) {
if (searchList.get(i).contains(search)) {
//Do whatever you want here
}
}
So I want to have a function to search in my ArrayList with for example five objects for all "name" strings.
contains()
method just callsequals()
onArrayList
elements, so you can overload your class'sequals()
based on thename
class variable. Returntrue
fromequals()
ifname
is equal to the matchingString
. Hope this helps.The easy way is to make a
for
where you verify if the atrrtibutename
of the custom object have the desired stringI think this helps you.
UPDATE: Using Java 8 Syntax
If you don't mind using an external libaray - you can use Predicates from the Google Guava library as follows: