Java Set Create asSet(T... ts)

Here you can find the source of asSet(T... ts)

Description

Returns an unmodifiable set containing the given elements.

License

LGPL

Parameter

Parameter Description
ts the elements from which to create a set
T the type of the element in the set

Return

an unmodifiable set containing the given elements or null in case the given element array is null .

Declaration

public static <T> Set<T> asSet(T... ts) 

Method Source Code


//package com.java2s;
/*/*from w w w  .  j ava2s  .  c o m*/
 * Hibernate OGM, Domain model persistence for NoSQL datastores
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later
 * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
 */

import java.util.Collections;

import java.util.HashSet;

import java.util.Set;

public class Main {
    /**
     * Returns an unmodifiable set containing the given elements.
     *
     * @param ts the elements from which to create a set
     * @param <T> the type of the element in the set
     * @return an unmodifiable set containing the given elements or {@code null} in case the given element array is
     * {@code null}.
     */
    public static <T> Set<T> asSet(T... ts) {
        if (ts == null) {
            return null;
        } else if (ts.length == 0) {
            return Collections.emptySet();
        } else {
            Set<T> set = new HashSet<T>(ts.length);
            Collections.addAll(set, ts);
            return Collections.unmodifiableSet(set);
        }
    }
}

Related

  1. asSet(T... elements)
  2. asSet(T... elements)
  3. asSet(T... elements)
  4. asSet(T... items)
  5. asSet(T... ts)
  6. asSet(T... values)
  7. asSet(T... values)
  8. toSet(Iterable things)
  9. toSet(Iterable items)