Converts the given objects into a Set . - Java java.util

Java examples for java.util:Set Operation

Description

Converts the given objects into a Set .

Demo Code

/*******************************************************************************
 * Copyright (c) 2014 Karlsruhe Institute of Technology, Germany
 *                    Technical University Darmstadt, Germany
 *                    Chalmers University of Technology, Sweden
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from w  w  w.  ja  v  a2 s .c o  m*/
 *    Technical University Darmstadt - initial API and implementation and/or initial documentation
 *******************************************************************************/
//package com.java2s;

import java.util.LinkedHashSet;

import java.util.Set;

public class Main {
    /**
     * Converts the given objects into a {@link Set}.
     * @param <T> The type of the objects.
     * @param objects The objects array to convert.
     * @return The created {@link Set}.
     */
    public static <T> Set<T> toSet(T... objects) {
        if (objects != null) {
            Set<T> result = new LinkedHashSet<T>(objects.length);
            for (T obj : objects) {
                result.add(obj);
            }
            return result;
        } else {
            return new LinkedHashSet<T>(0);
        }
    }
}

Related Tutorials