Close

Java Collections - Collections.addAll() Examples

Java Collections Java Java API 


Class:

java.util.Collections

java.lang.Objectjava.lang.Objectjava.util.Collectionsjava.util.CollectionsLogicBig

Method:

@SafeVarargs
public static <T> boolean addAll(Collection<? super T> c,
                                              T... elements)

Adds all of the specified elements to the specified collection.

This method loops through each specified element and add to the collection individually:

boolean result = false;
for (T element : elements)
result |= c.add(element);
return result;

Examples


package com.logicbig.example.collections;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class AddAllExample {

public static void main(String... args) {
Set<Integer> set = new HashSet<>();
boolean b = Collections.addAll(set, 1, 2, 3, 4, 5);
System.out.println(b);
System.out.println(set);
}
}

Output

true
[1, 2, 3, 4, 5]




See Also