Java Collection addAll() method24 Mar 2025 | 2 min read The addAll() method of Java Collection Interface appends or inserts all the elements of the specified collection to this collection. SyntaxParametersThe parameter 'c' represents the elements to be added into this Collection. Return ValueThe addAll() method returns a Boolean value true, if the queue has changed as a result of this call else it returns false. ThrowsThe addAll() method throws: UnsupportedOperationException - if the addAll operation is not supported by this collection ClassCastException- if the class of the specified element prevents it from being added to this collection. NullPointerException- if the specified element or the specified collection is null and this collection does not allow null elements IllegalArgumentException- if some property of the element prevents it from being added to this collection IllegalStateException- if all the elements cannot be inserted at this time due to some insertion restrictions Example 1Output: Before applying addAll() : Elements in queue : [] Elements in list : [5, 10, 15, 20, 25, 30, 35, 40, 45, 50] After applying addAll : Elements in queue : [50, 35, 20, 5, 40, 25, 10, 45, 30, 15] Elements in list : [5, 10, 15, 20, 25, 30, 35, 40, 45, 50] Example 2Output: Exception in thread "main" java.lang.IllegalArgumentException at java.util.concurrent.ConcurrentLinkedQueue.addAll(ConcurrentLinkedQueue.java:526) at com.tpointtech.JavaCollectionAddAllExample2.main(JavaCollectionAddAllExample2.java:11) Example 3Output: Exception in thread "main" java.lang.NullPointerException at java.util.ArrayDeque.addLast(ArrayDeque.java:249) at java.util.ArrayDeque.add(ArrayDeque.java:423) at com.tpointtech.JavaCollectionAddAllExample3.main(JavaCollectionAddAllExample3.java:13) Next TopicJava Collection |
Java Method The toArray() method of Collection Interface returns an array containing all the elements present in the collection. The second syntax returns an array containing all of the elements in this collection where the runtime type of the returned array is that of the specified...
3 min read
Java Method The clear() method of Java Collection Interface removes all of the elements from this collection. It returns a Boolean value 'true', if it successfully empties the collection. Syntax public void clear() Parameters NA Throws UnsupportedOperationException- if the clear operation is not supported by this collection. Example 1 import java.util.Collection; import java.util.concurrent.LinkedBlockingDeque; public class...
3 min read
Java Method The equals() method of Java Collection Interface compares the specified object with this collection for equality. Syntax public boolean equals(Object o) Parameters The parameter 'o' represents the object to be compared for equality with this collection. Overrides This method overrides equals in class Object Return The equals method returns Boolean value...
2 min read
Java Method The remove() method of Java Collection Interface is used to remove a single instance of the specified element from this collection. Syntax public boolean remove(Object o) Parameters The parameter 'o' represents the element to be removed from this collection if it is present. Throws ClassCastException- if the type of...
3 min read
Java Method The hashCode() method of Java Collection Interface returns the hash code value for this collection. Syntax public int hashCode() Parameters NA Overrides This method overrides hashCode in class Object Return The hashCode() method returns the hash code value for this collection. Example 1 import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.List; public class JavaCollectionHashCodeExample1 { ...
1 min read
Java Method The removeAll() method of Java Collection Interface only removes those elements of the Collection that are contained in the specified collection. Syntax public boolean removeAll(Collection<?> c) Parameters Here, the parameter 'c' represents the collection which contains the elements to be removed from the invoked collection. Return Value: The removeAll...
4 min read
Java method The add() method of Java Collection Interface inserts the specified element in this Collection. It returns a Boolean value 'true', if it succeeds to insert the element in the specified collection else it returns 'false'. Syntax public boolean add(E e) Parameters The parameter 'e' represents the element...
3 min read
Java Method The contains() method of Java Collection Interface returns a Boolean value 'true', if it contains the specified element in the collection. Syntax public boolean containsAll(Object o) Parameters The parameter 'o' represents the element whose occurrence in this collection is to be tested. Throws ClassCastException- if the type of the...
3 min read
Java Method The iterator() method of Java Collection Interface returns an iterator over the elements in this collection. Syntax public Iterator <E> iterator() Parameters NA Return The iterator () method returns an iterator over the elements in this collection. Example 1 import java.util.Collection; import java.util.Iterator; import java.util.concurrent.ConcurrentLinkedQueue; public class JavaCollectionIteratorExample1 { static int...
3 min read
Java method The size() method of Java Collection Interface returns the total count of the elements contained in this collection. Syntax public int size() Parameters NA Return Value The size() method returns the total count of the elements present in this collection. Example 1 import java.util.Collection; import java.util.HashSet; public interface JavaCollectionSizeExample1 { ...
3 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India