Java examples for Collection Framework:Set
Create unmodifiable set from Set
//package com.java2s; import java.util.Collections; import java.util.HashSet; import java.util.Set; public class Main { public static void main(String[] argv) { System.out.println(set()); }//from w w w.j av a 2 s. co m public static <T> Set<T> set(final T... elements) { final HashSet<T> set = new HashSet<T>(elements.length); Collections.addAll(set, elements); return Collections.unmodifiableSet(set); } public static <T> Set<T> set() { return new HashSet<T>(); } /** * @return an <b>UNMODIFIABLE</b> Set<T> */ public static <T> Set<T> unmodifiableSet(final Set<? extends T> s) { return (s == null) ? Collections.<T> emptySet() : Collections .unmodifiableSet(s); } }