java.util.Collections.emptyList() Method

Advertisements


Description

The emptyList() method is used to get the empty list (immutable). This list is serializable.

Declaration

Following is the declaration for java.util.Collections.emptyList() method.

public static final <T> List<T> emptyList()

Parameters

  • NA

Return Value

NA

Exception

  • NA

Example

The following example shows the usage of java.util.Collections.emptyList()

package com.tutorialspoint;

import java.util.*;

public class CollectionsDemo {
   public static void main(String args[]) {
      // create an empty list    
      List emptylst = Collections.emptyList();
      
      System.out.println("Created empty immutable list: "+emptylst);
       
      // try to add elements
      emptylst.add("A");
      emptylst.add("B");
   }    
}

Let us compile and run the above program, this will produce the following result.Exceeptions will be throws as the list is immutable.

Created empty immutable list: []
Exception in thread "main" java.lang.UnsupportedOperationException



Advertisements
Advertisements