Convert Array as Set - Java Collection Framework

Java examples for Collection Framework:Array Convert

Description

Convert Array as Set

Demo Code


//package com.java2s;

import java.util.Collections;
import java.util.LinkedHashSet;

import java.util.Set;

public class Main {

    @SafeVarargs// w w w .j  a  v  a 2s.c  o m
    public static <E> Set<E> asSet(E... elements) {
        if (elements == null || elements.length == 0) {
            return Collections.emptySet();
        }
        LinkedHashSet<E> set = new LinkedHashSet<E>(
                elements.length * 4 / 3 + 1);
        Collections.addAll(set, elements);
        return set;
    }
}

Related Tutorials