Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.util.Collection;

import java.util.LinkedHashSet;

import java.util.List;

public class Main {

    public static <T> LinkedHashSet<T> createLinkedHashSet() {
        return new LinkedHashSet<T>();
    }

    public static <T, V extends T> LinkedHashSet<T> createLinkedHashSet(V... args) {
        if (args == null || args.length == 0) {
            return new LinkedHashSet<T>();
        } else {
            LinkedHashSet<T> set = new LinkedHashSet<T>(args.length);

            for (V v : args) {
                set.add(v);
            }
            return set;
        }
    }

    public static <T> LinkedHashSet<T> createLinkedHashSet(Iterable<? extends T> c) {
        LinkedHashSet<T> set;
        if (c instanceof Collection<?>) {
            set = new LinkedHashSet<T>((Collection<? extends T>) c);
        } else {
            set = new LinkedHashSet<T>();
            iterableToCollection(c, set);
        }
        return set;
    }

    /**
     * Adds the given item to the list at specified <code>index</code>.
     * <p/>
     * if <code>index</code> is greater than list size, it simply appends to the
     * list.
     * 
     * @param list
     *            collection of elements
     * @param index
     *            specific index
     * @param item
     *            object to add.
     */
    public static <E, T extends E> void add(List<E> list, int index, T item) {
        if (index < list.size()) {
            list.add(index, item);
        } else {
            list.add(item);
        }
    }

    private static <T> void iterableToCollection(Iterable<? extends T> c, Collection<T> list) {
        for (T element : c) {
            list.add(element);
        }
    }
}