Java Set Create asSet(final T... array)

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

Description

Returns the specified array as a Set of elements.

License

Open Source License

Parameter

Parameter Description
T the class type of the elements in the array.
array the object array of elements to convert to a Set.

Return

a Set of elements contained in the specified array.

Declaration

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

Method Source Code


//package com.java2s;

import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;

import java.util.Set;

public class Main {
    /**//from   ww  w  .  j  a  va2  s  . co  m
     * Returns the specified array as a Set of elements.
     * <p/>
     * @param <T> the class type of the elements in the array.
     * @param array the object array of elements to convert to a Set.
     * @return a Set of elements contained in the specified array.
     * @see java.util.Arrays#asList(Object[])
     */
    @SafeVarargs
    public static <T> Set<T> asSet(final T... array) {
        Set<T> arraySet = new HashSet<T>(array.length);
        Collections.addAll(arraySet, array);
        return arraySet;
    }

    /**
     * Add all elements of an {@link Enumeration} to a {@link Collection}.
     * 
     * @param collection
     *          to add from enumeration.
     * @param enumeration
     *          to add to collection.
     * @return true if collection is modified, otherwise false.
     * @since 8.1
     * @see Collection#addAll(Collection)
     */
    public static final <T> boolean addAll(final Collection<T> collection, final Enumeration<T> enumeration) {
        if (null == enumeration) {
            return false;
        }

        boolean modified = false;
        while (enumeration.hasMoreElements()) {
            modified |= collection.add(enumeration.nextElement());
        }
        return modified;
    }
}

Related

  1. asSet(Collection c)
  2. asSet(E... elements)
  3. asSet(E... pEntities)
  4. asSet(final E... elements)
  5. asSet(final T t, final T... ts)
  6. asSet(int... values)
  7. asSet(Iterable iteratable)
  8. asSet(K... values)
  9. asSet(Object[] array)