Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Check if LinkedList is empty in Java

In this post, I will be sharing how to check if Linkedlist is empty in Java. There are two ways to achieve our goal:

1. Using isEmpty() method [Recommended]
2. Using size() method

Read Also: Difference between ArrayList and LinkedList in Java

Let's dive deep into the topic:

Check if LinkedList object is empty in Java

1. Using isEmpty() method

LinkedList class isEmpty() method returns true if the LinkedList object is empty.

Syntax:
 public boolean isEmpty()

As the name suggests, the isEmpty() method returns a boolean value i.e true if the list object contains no elements otherwise false.
 import java.util.LinkedList;public class LinkedListIsEmptyExample {
public static void main(String args[]) {
//Creating a LinkedList object
LinkedList fruitsList = new LinkedList();

/* using LinkedList class isEmpty() method, it returns
* true if the object is empty, otherwise false.
*/

// Below line should return true as there are no elements in the LinkedList
System.out.println(fruitsList.isEmpty()); // true

//adding an element to the LinkedList
fruitsList.add("APPLE");

// Below line should return false as there is one element present in the LinkedList
System.out.println(fruitsList.isEmpty()); // false
}
}

Output:
true
false


2. Using size() method

LinkedList class size() method returns the number of elements present in the given list. If a given LinkedList is empty then its size will be 0(zero).

Syntax:
 public int size()

Please find the code below:
 import java.util.LinkedList;

public class LinkedListSizeExample {
public static void main(String args[]) {

//Creating a LinkedList object using new keyword
LinkedList fruitsList = new LinkedList();

/* using LinkedList class size() method, it returns
* an int value indicating total number of elements
* present in the list.
*/

// Below line should return 0(zero) as there are no elements in the LinkedList
System.out.println(fruitsList.size()); // 0

if(fruitsList.size() == 0)
System.out.println("LinkedList is empty");

//adding an element to the LinkedList
fruitsList.add("APPLE");

// Below line should return the number of elements present in the given LinkedList
System.out.println(fruitsList.size()); // 1

if(fruitsList.size() != 0)
System.out.println("LinkedList is NOT empty");
}
}

Output:
0
LinkedList is empty
1
LinkedList is NOT empty


What is the Recommended way to Check if LinkedList is Empty

LinkedList isEmpty() method internally uses size() method to check if the list is empty or not. As a result, performance-wise there is no much difference between them.

However, the isEmpty() method is more readable than getting the size of the list using the LinkedList class size() method and comparing it with 0(zero).

Hence, we recommend to prefer LinkedList isEmpty() method over size() method.

That's all for today, please mention in the comments in case you have any questions related to checking if LinkedList is empty in Java.


This post first appeared on Java Hungry, please read the originial post: here

Share the post

Check if LinkedList is empty in Java

×

Subscribe to Java Hungry

Get updates delivered right to your inbox!

Thank you for your subscription

×