Java Collection clear() MethodThe 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 1Test it NowOutput: 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 3Test it NowOutput: 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.javaTpoint.JavaCollectionClearExample3.main(JavaCollectionClearExample3.java:9) Next TopicJava Collection |
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