Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

import java.util.Enumeration;

import java.util.List;

import java.util.Map;

import java.util.StringTokenizer;

public class Main {
    @SafeVarargs
    public static <T> List<T> toList(T... array) {
        if (isEmpty(array)) {
            return new ArrayList<>(0);
        }
        return new ArrayList<>(Arrays.asList(array));
    }

    public static <T> List<T> toList(Collection<T> c) {
        if (isEmpty(c)) {
            return new ArrayList<>(0);
        }
        return new ArrayList<>(c);
    }

    public static List<String> toList(StringTokenizer tokenizer) {
        List<String> list = new ArrayList<>();
        while (tokenizer.hasMoreTokens()) {
            list.add(tokenizer.nextToken());
        }
        return list;
    }

    public static List<Integer> toList(int... array) {
        if (isEmpty(array)) {
            return new ArrayList<>(0);
        }
        List<Integer> list = new ArrayList<>(array.length);
        for (int I : array) {
            list.add(new Integer(I));
        }
        return list;
    }

    public static List<Double> toList(double... array) {
        if (isEmpty(array)) {
            return new ArrayList<>(0);
        }
        List<Double> list = new ArrayList<>(array.length);
        for (double d : array) {
            list.add(new Double(d));
        }
        return list;
    }

    public static List<Boolean> toList(boolean... array) {
        if (isEmpty(array)) {
            return new ArrayList<>(0);
        }
        List<Boolean> list = new ArrayList<>(array.length);
        for (boolean b : array) {
            list.add(Boolean.valueOf(b));
        }
        return list;
    }

    public static List<Long> toList(long... array) {
        if (isEmpty(array)) {
            return new ArrayList<>(0);
        }
        List<Long> list = new ArrayList<>(array.length);
        for (long l : array) {
            list.add(new Long(l));
        }
        return list;
    }

    public static <T> List<T> toList(Enumeration<T> enumeration) {
        List<T> list = new ArrayList<>();
        addAll(list, enumeration);
        return list;
    }

    public static boolean isEmpty(Collection<?> c) {
        return c == null || c.isEmpty();
    }

    public static boolean isEmpty(Map<?, ?> map) {
        return map == null || map.isEmpty();
    }

    @SafeVarargs
    public static <T> boolean isEmpty(T... array) {
        return array == null || array.length == 0;
    }

    public static <T> void addAll(Collection<T> c, T[] array) {
        if (isEmpty(array)) {
            return;
        }
        c.addAll(Arrays.asList(array));
    }

    public static <T> void addAll(Collection<T> c, Enumeration<T> enumeration) {
        while (enumeration.hasMoreElements()) {
            c.add(enumeration.nextElement());
        }
    }
}