Java Collection isEmpty() Method

The isEmpty() method of Java Collection Interface returns the boolean value 'true' if this collection contains no elements.

Syntax

Parameters

NA

Return

The isEmpty () method returns the boolean value 'true' if this collection contains no elements else it returns false.

Example 1

Test it Now

Output:

The queue is empty.

Example 2

Test it Now

Output:

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.javaTpoint.JavaCollectionIsEmptyExample2.main(JavaCollectionIsEmptyExample2.java:9)

Example 3

Test it Now

Output:

isEmpty() method returns false
Elements are : [1, 2, 3, 4, 5]
Next TopicJava Collection




Latest Courses