Reputation: 159
In my Java application, i need some sort of multiple-selection from a list,is there any way except a self implemented funktion like this:
private List<T> list = someListClass(); //Contains the data
public List<T> getByKey(Key key){
List<T> returnList = someListClass();
for(Element e : list){
if(e.qualifiedBy(key)) returnList.add(e);
}
return returnList;
}
Upvotes: 3
Views: 209
Reputation: 691755
Your method is fine.
With Java8 lambdas, the above will be easier to write:
public List<T> getByKey(Key key){
return list.stream().filter(e -> e.qualifiedBy(key)).into(someListClass());
}
But before that, what you have is the simplest thing.
Upvotes: 2