Java Set Create asSet(T... values)

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

Description

as Set

License

Apache License

Declaration

@SafeVarargs
    public static <T> Set<T> asSet(T... values) 

Method Source Code

//package com.java2s;
/*//ww w .j  av  a  2s.  c o m
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

import java.util.HashSet;
import java.util.Iterator;

import java.util.List;
import java.util.Map;

import java.util.Set;

public class Main {
    @SafeVarargs
    public static <T> Set<T> asSet(T... values) {
        return new HashSet<>(asList(values));
    }

    @SafeVarargs
    public static <T> List<T> asList(T... values) {
        return isEmpty(values) ? Collections.emptyList() : Arrays.asList(values);
    }

    public static boolean isEmpty(CharSequence cs) {
        return length(cs) <= 0;
    }

    public static boolean isEmpty(Collection<?> c) {
        return (c == null) || c.isEmpty();
    }

    public static boolean isEmpty(Map<?, ?> m) {
        return (m == null) || m.isEmpty();
    }

    public static <T> boolean isEmpty(Iterable<? extends T> iter) {
        if (iter == null) {
            return true;
        } else if (iter instanceof Collection<?>) {
            return isEmpty((Collection<?>) iter);
        } else {
            return isEmpty(iter.iterator());
        }
    }

    public static <T> boolean isEmpty(Iterator<? extends T> iter) {
        return iter == null || !iter.hasNext();
    }

    @SafeVarargs
    public static <T> boolean isEmpty(T... a) {
        return length(a) <= 0;
    }

    public static boolean isEmpty(char[] chars) {
        return length(chars) <= 0;
    }

    public static int length(CharSequence cs) {
        return cs == null ? 0 : cs.length();
    }

    @SafeVarargs
    public static <T> int length(T... a) {
        return a == null ? 0 : a.length;
    }

    public static int length(char[] chars) {
        return (chars == null) ? 0 : chars.length;
    }
}

Related

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