diff --git a/primitiveCollections/src/main/java/org/lucares/collections/IntList.java b/primitiveCollections/src/main/java/org/lucares/collections/IntList.java index adffe47..33f8d22 100644 --- a/primitiveCollections/src/main/java/org/lucares/collections/IntList.java +++ b/primitiveCollections/src/main/java/org/lucares/collections/IntList.java @@ -342,6 +342,11 @@ public final class IntList implements Serializable, Cloneable { *

* This method does not release any memory. Call {@link #trim()} to free unused * memory. + *

+ * If {@code retain} is sorted, then the algorithm has a complexity of + * O(n*log(m)), where n is the length of {@code this} and m the length of + * {@code retain}. If {@code retain} is not sorted, then the complexity is + * O(n*m). * * @param remove * the elements to remove @@ -350,19 +355,7 @@ public final class IntList implements Serializable, Cloneable { * @see #trim() */ public void removeAll(final IntList remove) { - - int insertPosition = 0; - for (int i = 0; i < size; i++) { - final int current = data[i]; - if (remove.indexOf(current) < 0) { - // keep current element - data[insertPosition] = current; - insertPosition++; - } - } - size = insertPosition; - - sorted = size() <= 1 ? true : sorted; // lists of size 1 or smaller are always sorted + removeIf((val, index) -> remove.indexOf(val) >= 0); } /** @@ -403,8 +396,10 @@ public final class IntList implements Serializable, Cloneable { * For a method that computes the intersection of two lists and also removes * duplicate values, see {@link #intersection(IntList, IntList)}. *

- * The algorithm has a complexity of O(n*log(m)), where n is the length of - * {@code this} and m the length of {@code retain}. + * If {@code retain} is sorted, then the algorithm has a complexity of + * O(n*log(m)), where n is the length of {@code this} and m the length of + * {@code retain}. If {@code retain} is not sorted, then the complexity is + * O(n*m). * * @param retain * the elements to retain @@ -414,17 +409,7 @@ public final class IntList implements Serializable, Cloneable { * @see #intersection(IntList, IntList) */ public void retainAll(final IntList retain) { - - int insertPosition = 0; - for (int i = 0; i < size; i++) { - final int current = data[i]; - if (retain.indexOf(current) >= 0) { - // keep current element - data[insertPosition] = current; - insertPosition++; - } - } - size = insertPosition; + removeIf((val, index) -> retain.indexOf(val) < 0); } /**