Java Collection clear() Method24 Mar 2025 | 2 min read 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. SyntaxParametersNA ThrowsUnsupportedOperationException- if the clear operation is not supported by this collection. Example 1Output: Collection : [23, 123, 56] After applying clear() method : Collection : [] Example 2Output: 1. Sugar 2. Salt 3. Potato 4. Rice Is Your shopping done.? true/false : true Your queue has been emptied successfully. Process finished with exit code 0 Example 3Output: Exception in thread "main" java.lang.NullPointerException at java.util.concurrent.LinkedBlockingDeque.offerLast(LinkedBlockingDeque.java:357) at java.util.concurrent.LinkedBlockingDeque.addLast(LinkedBlockingDeque.java:334) at java.util.concurrent.LinkedBlockingDeque.add(LinkedBlockingDeque.java:633) at com.tpointtech.JavaCollectionClearExample3.main(JavaCollectionClearExample3.java:9) Next TopicJava Collection |
Java method The addAll() method of Java Collection Interface appends or inserts all the elements of the specified collection to this collection. Syntax boolean addAll(Collection<? extends E> c) Parameters The parameter 'c' represents the elements to be added into this Collection. Return Value The addAll() method returns a Boolean value...
3 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 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 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 isEmpty() method of Java Collection Interface returns the boolean value 'true' if this collection contains no elements. Syntax public boolean isEmpty() Parameters NA Return The isEmpty () method returns the boolean value 'true' if this collection contains no elements else it returns false. Example 1 import java.util.Collection; import java.util.concurrent.ConcurrentLinkedQueue; public class JavaCollectionIsEmptyExample1...
2 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 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 containsAll() method of Java Collection Interface returns a Boolean value 'true', if this collection contains all the elements in the invoked collection. Syntax public boolean containsAll(Collection<?> c) Parameters The parameter 'c' represents the collection to be checked for occurrence in this collection. Throws ClassCastException- if the types of...
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