Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 * Aptana Studio
 * Copyright (c) 2005-2011 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the GNU Public License (GPL) v3 (with exceptions).
 * Please see the license.html included with this distribution for details.
 * Any modifications to this file must keep this entire header intact.
 */

import java.util.LinkedHashSet;

import java.util.Set;

public class Main {
    /**
     * Convert a varargs list of items into a Set while preserving order. An empty set is returned if items is null.
     * 
     * @param <T>
     *            Any type of object
     * @param items
     *            A variable length list of items of type T
     * @return Returns a new LinkedHashSet<T> or an empty set
     */
    public static final <T> Set<T> newInOrderSet(T... items) {
        return addToSet(new LinkedHashSet<T>(items != null ? items.length : 0), items);
    }

    /**
     * Add a varargs list of items into a set. If the set or items are null then no action is performed. Note that the
     * destination set has no requirements other than it must be a Set of the source item's type. This allows the
     * destination to be used, for example, as an accumulator.<br>
     * <br>
     * Note that this method is not thread safe. Users of this method will need to maintain type safety against the set.
     * 
     * @param set
     *            A set to which items will be added
     * @param items
     *            A list of items to add
     */
    public static final <T, U extends T> Set<T> addToSet(Set<T> set, U... items) {
        if (set != null && items != null) {
            for (int i = 0; i < items.length; i++) {
                set.add(items[i]);
            }
        }

        return set;
    }
}