Java List Last Item getLast(List elements)

Here you can find the source of getLast(List elements)

Description

get Last

License

Open Source License

Return

the last element from , or fails if is empty.

Declaration

public static <T> T getLast(List<T> elements) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011 Google, Inc.//from   ww w  .  j  ava2s  .  c o m
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Google, Inc. - initial API and implementation
 *******************************************************************************/

import java.util.List;

public class Main {
    /**
     * @return the last element from {@link List} , or fails if {@link List} is empty.
     */
    public static <T> T getLast(List<T> elements) {
        return elements.get(elements.size() - 1);
    }

    /**
     * @return first element of required type, <code>null</code> if not found.
     */
    @SuppressWarnings("unchecked")
    public static <T> T get(Class<T> clazz, Object... objects) {
        for (Object object : objects) {
            if (isAssignable(clazz, object)) {
                return (T) object;
            }
        }
        return null;
    }

    /**
     * @return first element of required type, <code>null</code> if not found.
     */
    @SuppressWarnings("unchecked")
    public static <T> T get(Class<T> clazz, List<?> objects) {
        for (Object object : objects) {
            if (isAssignable(clazz, object)) {
                return (T) object;
            }
        }
        return null;
    }

    /**
     * @return <code>true</code> object can be casted to given class.
     */
    private static boolean isAssignable(Class<?> clazz, Object object) {
        return object != null && clazz.isAssignableFrom(object.getClass());
    }
}

Related

  1. flattenByteSegments(List byteSegments, int eachSegmentButLastLength, int lastSegmentLength)
  2. getLast(final List list)
  3. getLast(final List list)
  4. getLast(List list)
  5. getLast(List aList)
  6. getLast(List items)
  7. getLast(List l)
  8. getLast(List list)
  9. getLast(List list)