Java Collection Tutorial - Java Collections.singletonList(T o)








Syntax

Collections.singletonList(T o) has the following syntax.

public static <T> List <T> singletonList(T o)

Example

In the following code shows how to use Collections.singletonList(T o) method.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
//w  ww  . ja v a  2  s . co  m
public class Main {
     public static void main(String args[]) {
      // create an array of string objs
      String init[] = { "One", "Two", "Three", "One", "Two", "Three","from java2s.com" };
      
      // create one list
      List<String> list = new ArrayList<String>(Arrays.asList(init));
      
      System.out.println("List value before: "+list);
      
      // create singleton list
      list = Collections.singletonList("One");
      
      System.out.println("List value after: "+list);
   }
}

The code above generates the following result.