Java List Last Item isLast(T object, List list)

Here you can find the source of isLast(T object, List list)

Description

is Last

License

Apache License

Declaration

public static <T> boolean isLast(T object, List<T> list) 

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> boolean isLast(T object, List<T> list) {
        if (isEmpty(list))
            return false;
        Object last = getLast(list);
        return last == null ? object == null : last.equals(object);
    }//from  w w  w  .  ja v a 2 s  .  c om

    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();
    }

    public static <T> T getLast(List<T> items) {
        if (items == null || items.size() == 0)
            return null;
        return items.get(items.size() - 1);
    }

    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;
    }
}

Related

  1. getLastOrNull(List l)
  2. getLastOrNull(List list)
  3. getLastSectionDays(List sections)
  4. getLastXItems(int lastXItems, List list)
  5. isLast(List list, Object o)
  6. isLastIdx(List l, int idx)
  7. isLastIndex(List list, int index)
  8. isLastIndex(List suggestedList, int i)
  9. lastIndexOf(List lines, String... conditions)