Make a set unmodifiable - Java java.util

Java examples for java.util:Set Operation

Description

Make a set unmodifiable

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());
    }//w w  w.j a va2s  .  c  o  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