![]() |
VOOZH | about |
The addAll() method of Java AbstractCollection is used to append all elements from a given collection to the current collection. If the collection being appended is a TreeSet, the elements are stored in sorted order, as TreeSet maintains a natural ordering. It is important to note that AbstractCollection cannot be instantiated directly, so we use Collection as the type instead.
Example 1: Adding Elements from One Collection to Another
In this example, we append all the elements from one collection to another using the addAll() method.
Collection 1: [4, Geeks, To, TreeSet, Welcome] Collection 2 (before addAll): [] Collection 2 (after addAll): [4, Geeks, To, TreeSet, Welcome]
boolean addAll(Collection c);
Example 2: Using addAll() method with integer value
Collection 1: [10, 20, 30, 40, 50] Collection 2 (before addAll): [] Collection 2 (after addAll): [10, 20, 30, 40, 50]