Java Array to Set arrayToSet(final T[] array)

Here you can find the source of arrayToSet(final T[] array)

Description

Converts the specified array to a set.

License

Apache License

Parameter

Parameter Description
T the type of elements maintained by the specified array
array the specified array

Return

a hash set

Declaration

public static <T> Set<T> arrayToSet(final T[] array) 

Method Source Code

//package com.java2s;
/*/*from   w w w  . j  av a2  s  . c o  m*/
 * Copyright (c) 2009-2016, b3log.org & hacpai.com
 *
 * Licensed 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.Collections;
import java.util.HashSet;

import java.util.Set;

public class Main {
    /**
     * Converts the specified array to a set.
     *
     * @param <T> the type of elements maintained by the specified array
     * @param array the specified array
     * @return a hash set
     */
    public static <T> Set<T> arrayToSet(final T[] array) {
        if (null == array) {
            return Collections.emptySet();
        }

        final Set<T> ret = new HashSet<T>();

        for (int i = 0; i < array.length; i++) {
            final T object = array[i];

            ret.add(object);
        }

        return ret;
    }
}

Related

  1. arrayToSet(Object[] in)
  2. toSet(final TYPE[] array)
  3. toSet(T[] array)
  4. translateToSet(T[] array)