tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Java > Collections > Singleton Collection

Singleton Collection 

The following example shows how to create singleton collection. The Collections class provides several type safe methods which returns an immutable collection containing only the specified object. The returned collection is serializable.

  • Collections.singleton()
  • Collections.singletonList()
  • Collections.singletonMap()

File Name  :  
com/bethecoder/tutorials/utils/collections/SingletonCollection.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.utils.collections;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class SingletonCollection {

  /**
   @param args
   */
  public static void main(String[] args) {

    //singleton list of Strings
    List<String> strList = Collections.<String>singletonList("Hello World");
    print(strList);
    
    //singleton list of Booleans
    List<Boolean> boolList = Collections.<Boolean>singletonList(true);
    print(boolList);
    
    //singleton list of Employees
    List<Person> empList = Collections.<Person>singletonList(new Person("One"));
    print(empList);
    
    //singleton Set of Employees
    Set<Person> empSet = Collections.<Person>singleton(new Person("Two"));
    print(empSet);
    
    //singleton Map of Employees
    Map<String, Person> empMap = Collections.<String, Person>singletonMap("3"new Person("Three"));
    System.out.println(empMap + " size : " + empMap.size());
  }

  public static void print(Collection<?> collection) {
    System.out.println(collection + " size : " + collection.size());
  }
}

class Person {
  private String name;

  public Person(String name) {
    super();
    this.name = name;
  }
  
  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
  
  public String toString() {
    return "[Person : " + name + "]";
  }
}
   

It gives the following output,
[Hello World] size : 1
[true] size : 1
[[Person : One]] size : 1
[[Person : Two]] size : 1
{3=[Person : Three]} size : 1



 
  


  
bl  br