diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/util/CollectionUtils.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/util/CollectionUtils.java index d93a69016ca..691d6adf199 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/util/CollectionUtils.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/util/CollectionUtils.java @@ -154,17 +154,30 @@ public final class CollectionUtils { } /** - * Filter the elements of a collection down to just the ones - * that match the given predicate. + * Filter the elements of a collection down to just the ones that match the given predicate. * @since 5.6 */ public static Collection filter(Collection collection, IUnaryPredicate predicate) { if (collection.isEmpty()) return collection; - Collection result = new ArrayList(); - for (T t : collection) - if (predicate.apply(t)) - result.add(t); - return result; + Collection result = null; + int n = 0; + for (T t : collection) { + if (predicate.apply(t)) { + if (result != null) { + result.add(t); + } else { + ++n; + } + } else if (result == null) { + result = new ArrayList(collection.size() - 1); + for (T u : collection) { + if (--n < 0) + break; + result.add(u); + } + } + } + return result == null ? collection : result; } }