Java List First Item getFirstFromList(List list)

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

Description

Peaks first element from list

License

Open Source License

Parameter

Parameter Description
list a parameter

Return

T

Declaration

private static <T> T getFirstFromList(List<T> list) 

Method Source Code

//package com.java2s;
/*/*from  w  ww.ja  v  a  2s.  com*/
 * Lightmare, Lightweight embedded EJB container (works for stateless session beans) with JPA / Hibernate support
 *
 * Copyright (c) 2013, Levan Tsinadze, or third-party contributors as
 * indicated by the @author tags or express copyright attribution
 * statements applied by the authors.
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */

import java.util.Collection;

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

public class Main {
    public static final int FIRST_INDEX = 0;
    public static final int EMPTY_ARRAY_LENGTH = 0;

    /**
     * Peaks first element from list
     * 
     * @param list
     * @return T
     */
    private static <T> T getFirstFromList(List<T> list) {

        T value;

        if (valid(list)) {
            value = list.get(FIRST_INDEX);
        } else {
            value = null;
        }

        return value;
    }

    /**
     * Checks passed {@link Collection} instance on null and on emptiness
     * returns true if it is not null and is not empty
     * 
     * @param collection
     * @return <code></code>
     */
    public static boolean valid(Collection<?> collection) {
        return (collection != null && !collection.isEmpty());
    }

    /**
     * Checks passed {@link Map} instance on null and emptiness returns true if
     * it is not null and is not empty
     * 
     * @param map
     * @return <code>boolean</code>
     */
    public static boolean valid(Map<?, ?> map) {
        return (map != null && !map.isEmpty());
    }

    /**
     * Checks if passed array of {@link Object}'s instances is not null and is
     * not empty
     * 
     * @param array
     * @return <code>boolean</code>
     */
    public static boolean valid(Object[] array) {
        return (array != null && array.length > EMPTY_ARRAY_LENGTH);
    }
}

Related

  1. getFirstElement(List list)
  2. getFirstElement(List collection)
  3. getFirstElement(List list)
  4. getFirstElement(List list)
  5. getFirstElementFromList(List list)
  6. getFirstInstanceOf(List source, String className)
  7. getFirstItemOrNull(List items)
  8. getFirstLeaf(List aList)
  9. getFirstNameIndex(List list)