I have an ArrayList containing Car objects and I want to get from that ArrayList all the unique names as String or just the Car objects that meet that criteria.
EG:
public class Car {
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
Now I have carArray that contains a lot of Car objects.
To get the unique names I am doing something like this:
Set<String> setOfNames = new HashSet<String>();
for (Car car : carArray) {
setOfNames.add(car.getName());
}
for (String name : setOfNames) {
System.out.println(name);
}
Is there a better/faster way to filter an ArrayList by its elements properties?
Thank you!
I guess you are talking about some properties like features (available in other languages), but not available in java, so the best approach is to use setters and getters.
So my suggestion is to stick with your code since is working pretty fast 🙂
Good luck,
Arkde