Does addAll () return false?

Looking at the source code of the CollectionUtils addAll method. I noticed that it uses the symbol |=

 public static <T> boolean addAll(Collection<? super T> c, T... elements) { boolean result = false; for (T element : elements) result |= c.add(element); return result; } 

From my understanding, |= is a bitwise or operator and just a shorthand for result = result|c.add(element) , for example, like this:

 System.out.println(false|true); //true System.out.println(false|false); //false System.out.println(true|false); //true System.out.println(true|true); //true 

This means that if any item is successfully added, it will return true. Now I was wondering if there will be an instance, will it return false? If not, why does it have a refund?

+5
source share
4 answers

If all the elements that you want to add were already in the collection (before calling addAll), and the collection does not allow duplication, it will return false, since all individual calls to the add method will return false. This is true for collections such as Set .

For other collections, add always returns true, so addAll returns true if you do not call it with an empty list of items to add.

+6
source

|= 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.

+3
source

From docs addAll()

returns:

true if the collection has changed as a result of the call.

If the collections are not changed at all, then false.

Consider the program below (follow the result in the comments)

 public static void main(String[] args) { List<String> l1= new ArrayList<String>(); l1.add("test"); List<String> l2= new ArrayList<String>(); System.out.println(l2.addAll(l1));//true System.out.println(l2.addAll(l1));//true Set<String> s1= new HashSet<String>(); s1.add("test"); Set<String> s2= new HashSet<String>(); System.out.println(s2.addAll(s1));//true System.out.println(s2.addAll(s1));//false } 
+2
source

Operation: all items in the specified collection are added to the end of this list. if the collection has changed as a result of the call, it returns true. if the collection has not changed as a result of the call, it returns false.

0
source

Source: https://habr.com/ru/post/1203781/


All Articles