Create unmodifiable set from Set - Java Collection Framework

Java examples for Collection Framework:Set

Description

Create unmodifiable set from Set

Demo Code


//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&lt;T&gt;
     */
    public static <T> Set<T> unmodifiableSet(final Set<? extends T> s) {
        return (s == null) ? Collections.<T> emptySet() : Collections
                .unmodifiableSet(s);
    }
}

Related Tutorials