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 Da
UPDATE: Using Java 8 Syntax
List myList = new ArrayList<>();
//Fill up myList with your Data Points
List dataPointsCalledJohn =
myList
.stream()
.filter(p-> p.getName().equals(("john")))
.collect(Collectors.toList());
If you don't mind using an external libaray - you can use Predicates from the Google Guava library as follows:
class DataPoint {
String name;
String getName() { return name; }
}
Predicate nameEqualsTo(final String name) {
return new Predicate() {
public boolean apply(DataPoint dataPoint) {
return dataPoint.getName().equals(name);
}
};
}
public void main(String[] args) throws Exception {
List myList = new ArrayList();
//Fill up myList with your Data Points
Collection dataPointsCalledJohn =
Collections2.filter(myList, nameEqualsTo("john"));
}