Java List First Item firstElement(List list)

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

Description

Returns the first element of the given List .

License

Apache License

Parameter

Parameter Description
list a parameter

Declaration

public static <E> E firstElement(List<E> list) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2011 Danny Kunz// w  w  w  .  j a v  a2s. c om
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

import java.util.List;

public class Main {
    /**
     * Returns the first element of the given {@link List}. Returns null if the {@link List} reference is null or the
     * {@link List#isEmpty()}.
     * 
     * @see #lastElement(List)
     * @see #elementAt(List, int)
     * @param list
     */
    public static <E> E firstElement(List<E> list) {
        int index = 0;
        return elementAt(list, index);
    }

    /**
     * Returns the element at the given index position of the given {@link List}. If the {@link List} reference is null or the
     * {@link List#size()} is to small, null is returned.
     * 
     * @see #elementAtInverseIndex(List, int)
     * @see #firstElement(List)
     * @see #lastElement(List)
     * @param list
     * @param index
     */
    public static <E> E elementAt(List<E> list, int index) {
        //    
        E retval = null;

        //
        if (list != null && index >= 0 && list.size() > index) {
            retval = list.get(index);
        }

        //
        return retval;
    }

    /**
     * Returns the element at the given index position within the given {@link List} instance. <br>
     * <br>
     * If the given {@link List} is null, null is returned. <br>
     * If the index is out of bounds, null is returned.
     * 
     * @see #elementAt(List, int)
     * @param list
     *          {@link List}
     * @param index
     * @return element at the specific index position
     */
    public static <E> E get(List<E> list, int index) {
        E retval = null;
        if (list != null && index >= 0 && index < list.size()) {
            retval = list.get(index);
        }
        return retval;
    }
}

Related

  1. first(List list)
  2. first(List list)
  3. first_nItems(int n, Collection fromList)
  4. firstBoolean(List list)
  5. firstColumnRemovable(List data)
  6. firstItem(Iterable list)
  7. firstItem(List items)
  8. firstMatch(List src, String... lookup)
  9. firstObjectFromList(List list)