Collections.removeIf spec says nothing whether predicate may refer to collection

Tagir Valeev amaembo at gmail.com
Mon Sep 24 07:21:21 UTC 2018


Hello!

When using removeIf, sometimes it's tempting to refer to the
collection from the predicate. E.g.:

    static List<Integer> select(Collection<Integer> input) {
        List<Integer> result = new LinkedList<>(input);
        result.removeIf(left -> result.stream().noneMatch(right ->
right == left * 2));
        return result;
    }

Here one may assume that every time the predicate returns true, the
collection is updated and the subsequent predicate invocations see the
updated collection. This works for default removeIf implementation,
but doesn't work for optimized ones like ArrayList, so replacing
LinkedList with ArrayList in this weird algorithm produces different
result (e.g. with List.of(1, 4, 2) input). I think that spec should
explicitly say that intermediate updates are not guaranteed to be
visible for predicate. What do you think?

With best regards,
Tagir Valeev


More information about the core-libs-dev mailing list