Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.lang.reflect.Array;
import java.util.*;

public class Main {
    /**
     * Converts the given Collection into an array of the given type.
     *
     * @param collection The collection to convert
     * @param arrayType  The type of array to return
     * @return The new array
     */
    @SuppressWarnings({ "unchecked" })
    public static <E> E[] toArray(Collection<E> collection, Class<? extends E> arrayType) {
        return collection.toArray((E[]) Array.newInstance(arrayType, collection.size()));
    }

    /**
     * Nullsafe method returning the number of elements the incoming collection contains.
     *
     * @param c Collection of Objects to count the number of elements from
     * @return int containing the number of the elements in the collection, or -1 if the collection is null
     */
    public static int size(Collection c) {
        int size = -1;

        if (c != null) {
            size = c.size();
        }

        return size;
    }

    /**
     * Nullsafe method returning the number of entries the incoming map contains.
     *
     * @param m Map of Map.Entry objects to count the number of entries from
     * @return int containing the number of the entries in the map, or -1 if the map is null
     */
    public static int size(Map m) {
        int size = -1;

        if (m != null) {
            size = m.size();
        }

        return size;
    }
}