Here you can find the source of getLast(final List extends T> list)
Parameter | Description |
---|---|
T | The type of element in the list. |
list | The list to get the last element from. |
public static <T> T getLast(final List<? extends T> list)
//package com.java2s; /*/*ww w . j a v a 2 s . c o m*/ * File: CollectionUtil.java * Authors: Justin Basilico * Company: Sandia National Laboratories * Project: Cognitive Foundry * * Copyright March 25, 2008, Sandia Corporation. * Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive * license for use of this work by or on behalf of the U.S. Government. Export * of this program may require a license from the United States Government. * See CopyrightHistory.txt for complete details. * */ import java.util.Collection; import java.util.Iterator; import java.util.List; public class Main { /** * Gets the last element of the list. If the list is null or empty, null is * returned. * * @param <T> The type of element in the list. * @param list The list to get the last element from. * @return The last element from the list, if one exists. Otherwise, null. */ public static <T> T getLast(final List<? extends T> list) { if (list == null || list.isEmpty()) { return null; } else { return list.get(list.size() - 1); } } /** * Returns true if the given collection is null or empty. * * @param collection The collection to determine if it is null or empty. * @return True if the given collection is null or empty. */ public static boolean isEmpty(final Collection<?> collection) { return collection == null || collection.isEmpty(); } /** * Returns true if the given iterable is null or empty. * * @param iterable The iterable to determine if it is null or empty. * @return True if the given iterable is null or empty. */ public static boolean isEmpty(final Iterable<?> iterable) { if (iterable == null) { // It is null, so it is empty. return true; } else if (iterable instanceof Collection) { return ((Collection<?>) iterable).isEmpty(); } else { return !iterable.iterator().hasNext(); } } /** * Determines the size of the given collection, checking for null. * * @param collection The collection to get the size of. * @return The size of the collection. If it is null, zero is returned. */ public static int size(final Collection<?> collection) { if (collection == null) { return 0; } else { return collection.size(); } } /** * Determines the size of the given iterable. If it is null, zero is * returned. If it is a {@code Collection}, then the size method is used. * Otherwise, the iterable is iterated over to get the size. * * @param iterable The iterable to determine the size of. * @return The size of the given iterable. */ public static int size(final Iterable<?> iterable) { if (iterable == null) { // The size is zero. return 0; } else if (iterable instanceof Collection) { // Get the size from the collection. This cast is justified by // not having to loop over all the elements. return ((Collection<?>) iterable).size(); } else { // Cound up the elements in the iterable. int counter = 0; final Iterator<?> iterator = iterable.iterator(); while (iterator.hasNext()) { iterator.next(); counter++; } return counter; } } }