Java Array to Set toSet(T[] array)

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

Description

Converts an array to a Set .

License

Open Source License

Parameter

Parameter Description
T the type
array the array to convert

Return

the

Declaration

public static <T> Set<T> toSet(T[] array) 

Method Source Code


//package com.java2s;

import java.util.Collections;
import java.util.HashSet;

import java.util.Set;

public class Main {
    /**// w  ww.  j  a  v  a2  s  .co  m
     * Converts an array to a {@link Set}.
     * 
     * @param <T> the type
     * @param array the array to convert
     * @return the {@link Set}
     */
    public static <T> Set<T> toSet(T[] array) {
        final Set<T> result = new HashSet<T>();
        for (final T element : array) {
            result.add(element);
        }
        return Collections.unmodifiableSet(result);
    }
}

Related

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