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.Comparator;

import java.util.List;

import java.util.TreeSet;

public class Main {

    public static <T> TreeSet<T> createTreeSet() {
        return new TreeSet<T>();
    }

    public static <T, V extends T> TreeSet<T> createTreeSet(V... args) {
        return (TreeSet<T>) createTreeSet(null, args);
    }

    public static <T> TreeSet<T> createTreeSet(Iterable<? extends T> c) {
        return createTreeSet(null, c);
    }

    public static <T> TreeSet<T> createTreeSet(Comparator<? super T> comparator) {
        return new TreeSet<T>(comparator);
    }

    public static <T, V extends T> TreeSet<T> createTreeSet(Comparator<? super T> comparator, V... args) {
        TreeSet<T> set = new TreeSet<T>(comparator);
        if (args != null) {
            for (V v : args) {
                set.add(v);
            }
        }
        return set;
    }

    public static <T> TreeSet<T> createTreeSet(Comparator<? super T> comparator, Iterable<? extends T> c) {
        TreeSet<T> set = new TreeSet<T>(comparator);

        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);
        }
    }
}