isEmpty( )method of List Interface in Java


Posted in : Core Java Posted on : January 6, 2011 at 7:18 PM Comments : [ 0 ]
Advertisements
In the following example we will show you the method of checking a list, is it empty or any element does exist.

isEmpty( )method of List Interface in Java

In the following example we will show you the method of checking a list, is it empty or any element does exist.

Syntax

public boolean isEmpty( )

This method returns value either True or False.

In this method you can check your list, is it empty or it contains element. It returns either true or false. It returns 'true' when a list is empty and returns false when there is an element exist in list.

Parameter Description

It does not take any argument, but returns boolean value True/False. 

Example of isEmpty( ) method 

In this example we will show you how does isEmpty( ) method works in List interface. This example will help you to understand how to check an existing list is empty or contains element. Through this example we will show you the size of list, and the True value if existing list is empty/size = 0 or False if list size>0/contains any element.

Example:-

package Devmanuals.com;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
public class ListIsEmpty {
public static void main(String args []){
ArrayList al=new ArrayList();
System.out.println("Size of List "+al.size());
System.out.println("Is List empty ? "+al.isEmpty());
List ls1= Arrays.asList(new String[]{"Arya","Bipul"});
System.out.println("Size of List1 "+ls1.size());
System.out.println("Is List1 empty ? "+ls1.isEmpty());
List ls2= Arrays.asList(new String[]{,});
System.out.println("Size of List2 "+ls2.size());
System.out.println("Is List2 empty ? " +ls2.isEmpty());
}
}

Output :

Size of List 0

Is List empty ? true

Size of List1 2

Is List1 empty ? false

Size of List2 0

Is List2 empty ? true

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Tutorial Topics