Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
list
array
communitycreator

What is the CopyOnWriteArrayList.isEmpty() method in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

CopyOnWriteArrayList is a thread-safe version of an ArrayList. For all the write operations like add(), set(), etc., it makes a fresh copy of the underlying array and performs the operations in the cloned array. This results in lower performance when compared to ArrayList. Read more about CopyOnWriteArrayList here.

The isEmpty() method verifies if the CopyOnWriteArrayList object contains no elements.

Syntax

public boolean isEmpty();

Parameters

This method takes no arguments.

Return value

This method returns true if the list contains no elements. Otherwise, it returns false.

The following code demonstrates the use of the isEmpty() method:

import java.util.concurrent.CopyOnWriteArrayList;
class IsEmpty {
public static void main( String args[] ) {
// Creating an empty list
CopyOnWriteArrayList<Integer> list = new CopyOnWriteArrayList<>();
// Printing the list
System.out.println("The list is: " + list);
// Checking if the list is empty
System.out.println("Is list Empty: " + list.isEmpty());
// Adding elements
list.add(1);
System.out.println("The list is: " + list);
System.out.println("Is list Empty: " + list.isEmpty());
}
}
Use of isEmpty()

Explanation

In the code above, we create a CopyOnWriteArrayList object named, list. Initially, it contains no elements, so the isEmpty() method returns true. Then, we add one element to the list object. Now, the isEmpty() method returns false because the list object contains one element.

RELATED TAGS

java
list
array
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring