Java List First Item getFirstElement(List list)

Here you can find the source of getFirstElement(List list)

Description

This is a convenience method to return the first element from a list.

License

Open Source License

Parameter

Parameter Description
list a parameter

Declaration

public static <T> T getFirstElement(List<T> list) 

Method Source Code

//package com.java2s;
/**/*from ww  w .  j  av a2s  .co m*/
 * Aptana Studio
 * Copyright (c) 2005-2011 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the GNU Public License (GPL) v3 (with exceptions).
 * Please see the license.html included with this distribution for details.
 * Any modifications to this file must keep this entire header intact.
 */

import java.util.Collection;

import java.util.List;
import java.util.Map;

public class Main {
    /**
     * This is a convenience method to return the first element from a list. If the list is empty, then null is
     * returned.
     * 
     * @param list
     * @return
     */
    public static <T> T getFirstElement(List<T> list) {
        if (!isEmpty(list)) {
            return list.get(0);
        }

        return null;
    }

    /**
     * This is a convenience method that returns true if the specified collection is null or empty
     * 
     * @param <T>
     *            Any type of object
     * @param collection
     * @return
     */
    public static <T> boolean isEmpty(Collection<T> collection) {
        return collection == null || collection.isEmpty();
    }

    /**
     * This is a convenience method that returns true if the specified map is null or empty
     * 
     * @param <T>
     *            any type of key
     * @param <U>
     *            any type of value
     * @param map
     * @return
     */
    public static <T, U> boolean isEmpty(Map<T, U> map) {
        return map == null || map.isEmpty();
    }
}

Related

  1. getFirstAsString(Map> multiValueMap, String key)
  2. getFirstColumn(List> llo, String reason)
  3. getFirstDuplicateValue(List values)
  4. getFirstElement(List list)
  5. getFirstElement(List collection)
  6. getFirstElement(List list)
  7. getFirstElementFromList(List list)
  8. getFirstFromList(List list)
  9. getFirstInstanceOf(List source, String className)