Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

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

import java.util.HashSet;

import java.util.Map;

import java.util.Set;

public class Main {
    @SafeVarargs
    public static <T> Set<T> toSet(T... array) {
        if (isEmpty(array)) {
            return new HashSet<>(0);
        }
        return new HashSet<>(Arrays.asList(array));
    }

    public static <T> Set<T> toSet(Collection<T> c) {
        if (isEmpty(c)) {
            return new HashSet<>(0);
        }
        return new HashSet<>(c);
    }

    public static Set<Integer> toSet(int... array) {
        if (isEmpty(array)) {
            return new HashSet<>(0);
        }
        Set<Integer> set = new HashSet<>(array.length);
        for (int I : array) {
            set.add(new Integer(I));
        }
        return set;
    }

    public static Set<Double> toSet(double... array) {
        if (isEmpty(array)) {
            return new HashSet<>(0);
        }
        Set<Double> set = new HashSet<>(array.length);
        for (double d : array) {
            set.add(new Double(d));
        }
        return set;
    }

    public static Set<Boolean> toSet(boolean... array) {
        if (isEmpty(array)) {
            return new HashSet<>(0);
        }
        Set<Boolean> set = new HashSet<>(array.length);
        for (boolean b : array) {
            set.add(Boolean.valueOf(b));
        }
        return set;
    }

    public static Set<Long> toSet(long... array) {
        if (isEmpty(array)) {
            return new HashSet<>(0);
        }
        Set<Long> set = new HashSet<>(array.length);
        for (long l : array) {
            set.add(new Long(l));
        }
        return set;
    }

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