Java linkedlist isempty: In the previous article, we have discussed about Java LinkedList size() Method with Examples
In this article we are going to see the use of Java LinkedList isEmpty() method along with suitable examples.
Java LinkedList isEmpty() Method with Examples
Linked list isempty: This java.util.LinkedList.isEmpty()
method is used to check if the LinkedList is empty or not.
It returns true if the list is empty or it contains 0 element else it returns false.
Syntax:
LinkedListName.isEmpty()
Where,
LinkedListName
refers to the name of your LinkedList.
Let’s see different examples to understand it more clearly.
Example-1: Java LinkedList isEmpty() Method – Example with String Type LinkedList
Approach:
- Create a new LinkedList of type String.
- Add string elements into the LinkedList using the add() method.
- Display the LinkedList elements.
- Then clear all the elements from the LinkedList using
clear()
method. - Check if the LinkedList is empty or not using
isEmpty()
method.
Program:
import java.util.*; public class Main { public static void main(String[] args) { // Create a LinkedList of string datatype LinkedList<String> l = new LinkedList<String>(); // Adding some elements to the LinkedList l.add("Hello"); l.add("this"); l.add("is"); l.add("an"); l.add("LinkedList"); // Prints the LinkedList elements System.out.println("The elements of LinkedList are: "+l); // clearing all the element from the LinkedList l.clear(); // Prints the new LinkedList elements System.out.println("The new elements of LinkedList are: "+l); // checking if the LinkedList is empty System.out.println("Is the LinkedList empty : "+l.isEmpty()); } }
Output: The elements of LinkedList are: [Hello, this, is, an, LinkedList] The new elements of LinkedList are: [] Is the LinkedList empty : true
Example-2: Java LinkedList isEmpty() Method – Example with Integer Type LinkedList
Approach:
- Create a new LinkedList of type Integer.
- Add integer elements into the LinkedList using the add() method.
- Display the LinkedList elements.
- Then remove one specific elements from the LinkedList using
remove()
method. - Print the new LinkedList.
- Check if the LinkedList is empty or not using
isEmpty()
method.
Program:
import java.util.*; public class Main { public static void main(String[] args) { // Create a LinkedList of Integer datatype LinkedList<Integer> l = new LinkedList<Integer>(); // Adding some elements to the LinkedList l.add(2); l.add(52); l.add(13); l.add(17); l.add(1); // Prints the LinkedList elements System.out.println("The elements of LinkedList are: "+l); // Remove the specific element from the LinkedList l.remove(2); // Prints the LinkedList elements System.out.println("The elements of LinkedList are: "+l); // checking if the LinkedList is empty System.out.println("Is the LinkedList empty : "+l.isEmpty()); } }
Output: The elements of LinkedList are: [2, 52, 13, 17, 1] The elements of LinkedList are: [2, 52, 17, 1] Is the LinkedList empty : false
Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.
Related Java Programs: