Java Collection removeAll() Method24 Mar 2025 | 2 min read The removeAll() method of Java Collection Interface only removes those elements of the Collection that are contained in the specified collection. SyntaxParametersHere, the parameter 'c' represents the collection which contains the elements to be removed from the invoked collection. Return Value:The removeAll () method returns a Boolean value 'true' if the collection has changed as a result of the call, else it returns 'false'. Throws:UnsupportedOperationException- if the removeAll method is not supported by this collection. ClassCastException- if the types of one or more elements in this collection are not compatible with the invoked collection. NullPointerException- if this collection is null or it contains one or more null elements and this collection does not allow null elements. Example 1Output: collection : [1, 2, 3, 4, 5, 6, 7, 8, 9] List of even numbers : [2, 4, 6, 8] Odd numbers : [1, 3, 5, 7, 9] Example 2Output: Total no : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] Table of 2 : [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] Example 3Output: Exception in thread "main" java.lang.NullPointerException at java.util.concurrent.ConcurrentLinkedQueue.checkNotNull(ConcurrentLinkedQueue.java:920) at java.util.concurrent.ConcurrentLinkedQueue.offer(ConcurrentLinkedQueue.java:327) at java.util.concurrent.ConcurrentLinkedQueue.add(ConcurrentLinkedQueue.java:297) at com.tpointtech.JavaCollectionRemoveAllExample3.main(JavaCollectionRemoveAllExample3.java:13) Next TopicJava-Collection |
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 spliterator() method of Collection Interface returns a Spliterator across the elements of this collection. Syntax public Spliterator<E>spliterator() Parameters NA Specified By The spliterator() method of Collection class is specified by: Spliterator() method in interface Iterable<E>. Return Value The spliterator() method returns a Spliterator across the elements of this collection. Example 1 import java.util.Collection; import...
2 min read
Java method The retainAll() method of Java Collection Interface keeps only those elements in this queue that are present in the specified collection. Syntax public boolean retainAll(Collection<?> c) Parameters The parameter 'c' represents the collection having elements to be kept in this collection. Return Value The retainAll() method returns true if...
5 min read
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 removeIf() method of Java Collection Interface removes the elements of this queue that satisfies the given predicate filter. Syntax public boolean removeIf (Predicate<? super E> filter) Parameters The parameter 'filter' represents a predicate which returns true for the elements to be removed. Return Value: The removeIf...
4 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 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 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 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
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
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