Collections static final Set emptySet() method Example Program


Returns the empty set (immutable)

Program

package com.candidjava;

import java.util.*;

/**
 * 
 * @author karthikeyan.T
 * @description The following example shows the usage of Collections.emptySet()
 */
public class EmptySet {

	public static void main(String args[]) {
		Set emptyset = Collections.emptySet();
		System.out.println("Created empty immutable set: " + emptyset);
	}
}

Output

Created empty immutable set: []

Explanation

public static final <T> Set<T> emptySet()
Returns the empty set (immutable). This set is serializable. Unlike the like-named field, this method is parameterized.
This example illustrates the type-safe way to obtain an empty set:

     Set<String> s = Collections.emptySet();
 
Implementation note: Implementations of this method need not create a separate Set object for each call. Using this method is likely to have comparable cost to using the like-named field. (Unlike this method, the field does not provide type safety.)
Since:
1.5
See Also:
EMPTY_SET


Related Post

Comments


©candidjava.com