Java List Last Item getLast(List items)

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

Description

get Last

License

Apache License

Declaration

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

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Collection;

import java.util.Iterator;

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

public class Main {
    public static <T> T getLast(List<T> items) {
        if (items == null || items.size() == 0)
            return null;
        return items.get(items.size() - 1);
    }//ww w  . jav a2  s  .c om

    public static int size(Collection<?> col) {
        return col == null ? 0 : col.size();
    }

    /**
     * This method provides a way to obtain an object from a collection, given
     * an equal object (logical equals). In the collection and set interfaces
     * (even list) the assumption is that if you have an equal object why would
     * you need the one out of the collection. Because logically equal objects
     * can be physically different objects we provide this method to return out
     * of a collection the actual object in the collection ( for partial object
     * checking and untrusted object arguements that reference an existing item
     * in a collection ).
     * 
     * @param p_oCol
     * @param p_oObj
     * @return
     */
    public static <T> T get(Collection<T> p_oCol, T p_oObj) {
        if (p_oObj == null)
            return null;
        if (isEmpty(p_oCol))
            return null;
        if (p_oCol instanceof List)
            return get(p_oCol, ((List<T>) p_oCol).indexOf(p_oObj));
        T oObj = null;
        T oTempObj = null;

        for (Iterator<T> oIter = p_oCol.iterator(); oIter.hasNext();) {
            oTempObj = oIter.next();
            if (p_oObj.equals(oTempObj)) {
                oObj = oTempObj;
                break;
            }
        }

        return oObj;
    }

    /**
     * Mostly provided for sets, Please keep in mind sets don't guarantee any
     * specific order. And it could be that for any one instance of a set this
     * method may return 2 different objects for the same index (dependent on
     * the set implementation) This method is useful for getting a single item
     * out of a collection when you don't know what type of collection you are
     * dealing with.
     * 
     * @param p_oCol
     * @param p_iIndex
     * @return
     */
    public static <T> T get(Collection<T> p_oCol, int p_iIndex) {
        if (isEmpty(p_oCol))
            return null;
        if (p_iIndex < 0 || p_iIndex >= p_oCol.size())
            return null;
        if (p_oCol instanceof List)
            return ((List<T>) p_oCol).get(p_iIndex);

        T oObj = null;
        T oTempObj = null;
        int i = 0;

        for (Iterator<T> oIter = p_oCol.iterator(); oIter.hasNext();) {
            oTempObj = oIter.next();
            if (i++ == p_iIndex) {
                oObj = oTempObj;
                break;
            }
        }

        return oObj;
    }

    public static boolean isEmpty(Iterable<?> i) {
        if (i instanceof Collection)
            return ((Collection<?>) i).isEmpty();
        return i == null || !i.iterator().hasNext();
    }

    public static boolean isEmpty(Map<?, ?> p_oCol) {
        return p_oCol == null || p_oCol.isEmpty();
    }
}

Related

  1. getLast(final List list)
  2. getLast(final List list)
  3. getLast(List list)
  4. getLast(List aList)
  5. getLast(List elements)
  6. getLast(List l)
  7. getLast(List list)
  8. getLast(List list)
  9. getLast(List list)