|= bitwise OR
| (Bitwise OR) sets the bit to 1 if one or both corresponding bits in its operands are equal to 1, and to 0 if both corresponding bits are equal to 0. In other words, | returns one in all cases except when the corresponding bits of both operands are zero. The resulting bit pattern is the set bit (1 or true) of either of the two operands. This property is used to "set" or "enable" the "flag" (bit set to one) in your flags or options, regardless of whether this flag was set earlier or not. Multiple flag bits can be set if a combined MASK is defined.
// To set or turn on a flag bit(s) flags = flags | MASK; // or, more succintly flags |= MASK;
So your code is equivalent:
boolean result = false; for (T element : elements){ result = result | c.add(element); } return result;
Initially, the result will be false , and as one of the elements successfully added to the collection is set to true ie c.add(element); . Thus, it will return true if one of the elements is added.
source share