All Collection members are added to the code using the add() method, which returns a boolean , indicating whether add() was executed or not. What the addAll method addAll is return true if any of the added successes and false if all of them failed. (This seems strange to me, as I would return true if all the additions were successful, but I got distracted.)
So you can do it like this:
@Override public boolean addAll(Collection<? extends E> c) { boolean result = false; for (E e : c) { if (add(e)) { result = true; } } return result; }
But this is a little more detailed, since you can act more directly with the result variable:
@Override public boolean addAll(Collection<? extends E> c) { boolean result = false; for (E e : c) { result = add(e) || result; } return result; }
So, we logically have the OR-old value of result with the return value of add to get the new value. (Note: we want the result be on the right side of || because || "shortcircuits" and does not interfere with checking the right side || if the left side is true ). So if add(e) and result were the other way around, this would not evaluate the right side - that is, if it did not run the add() method - once the result was true .)
The person who wrote this method decided that they want to be as concise as possible so that they change:
result = add(e) || result;
in
result |= add(e);
which matches with:
result = result | add(e);
Operator | is a bitwise OR that is not a logical OR, with the exception of Boolean, where the effect is basically equal to The same, the only difference is that | does not have the short circuit behavior mentioned above.
There is no ||= syntax in Java, so a bitwise OR is used, although even if it did, it would probably have the same squirrel cage problem mentioned above.
Dave webb
source share