Checks whether the COLLECTION is not NULL and has at least one element. - Java java.util

Java examples for java.util:Collection Null Element

Description

Checks whether the COLLECTION is not NULL and has at least one element.

Demo Code


//package com.book2s;

import java.util.Collection;

import java.util.Map;

public class Main {
    public static void main(String[] argv) {
        Collection collection = java.util.Arrays.asList("asdf",
                "book2s.com");
        System.out.println(hasElements(collection));
    }// www.j  av a 2 s .c  o  m

    /**
     * Checks whether the COLLECTION is not NULL and has at least one element.
     * 
     * @param <T>
     *            the generic type
     * @param collection
     *            the collection
     * @return true, if successful
     */
    public static <T> boolean hasElements(Collection<T> collection) {
        return (null != collection && collection.size() > 0);
    }

    /**
     * Checks whether the MAP is not NULL and has at least one element.
     * 
     * @param <T>
     *            the generic type
     * @param <V>
     *            the value type
     * @param map
     *            the map
     * @return true, if successful
     */
    public static <T, V> boolean hasElements(Map<T, V> map) {
        return (null != map && map.size() > 0);
    }
}

Related Tutorials