I keep coming across techniques like the code below where i need to filter out a type of enumeration from a list.
Are there any more efficent ways to do this?
private List<TestResult> removeInfo(List<TestResult> testResults) {
List<TestResult> tmpT = new ArrayList<TestResult>();
for(TestResult t : testResults) {
if(!t.getSeverity().equals(Severity.INFO)) {
tmpT.add(t);
}
}
return tmpT;
}
Collections would be my first thought but not sure.
Cheers
D
You can filter the list in-place using an iterator:
This is an example of Marvo’s comment. It’s more memory efficient but just time efficient as your code. Again, as Marvo said you could some improvement by using a LinkedList. However, unless you’re dealing with huge lists of data I don’t think you’ll notice a difference.