Java Utililty Methods Collection Last

List of utility methods to do Collection Last

Description

The list of methods to do Collection Last are organized into topic(s).

Method

ObjectgetLast(Collection c)
Returns the last element in a collection (by exhausting its iterator in case it is not a list).
if (c instanceof List)
    return getLast((List) c);
Object result = null;
Iterator it = c.iterator();
do { 
    result = it.next();
} while (it.hasNext());
return result;
...
TgetLastElement(Collection collection, T defaultValue)
Method which will return the "last" element in the given collection or a null value if not found.
T elem = defaultValue;
if (collection != null && !collection.isEmpty()) {
    if (List.class.isAssignableFrom(collection.getClass())) {
        elem = ((List<T>) collection).get(collection.size() - 1);
    } else {
        for (T item : collection) {
            elem = item;
return elem;
TgetLastElement(final Collection collection)
get Last Element
if (isEmpty(collection)) {
    return null;
if (collection instanceof List) {
    List<T> list = (List<T>) collection;
    return list.get(list.size() - 1);
} else {
    T last = null;
...
TgetLastOfCollection(Collection collection)
get Last Of Collection
T t = (T) collection.toArray()[collection.size() - 1];
return t;
TgetLastOrNull(Collection collection)
get Last Or Null
if (collection.isEmpty()) {
    return null;
} else if (collection instanceof List) {
    List<T> list = (List<T>) collection;
    return list.get(list.size() - 1);
} else {
    Iterator<T> iterator = collection.iterator();
    T last = null;
...
StringimplodeCollection(Collection items, String prefix, String suffix, String delimiter, String lastItemSuffix)
Glue together all items into one String.
if (items == null)
    throw new NullPointerException("items argument may not be null");
if (prefix == null)
    prefix = "";
if (suffix == null)
    suffix = "";
if (delimiter == null)
    delimiter = "";
...
intlastIndexOfObjectIdentity(Collection collection, Object object)
Returns the index position of the last occurring object within the Collection .
int retval = -1;
if (collection != null) {
    Iterator<E> iterator = collection.iterator();
    if (iterator != null) {
        int indexPosition = 0;
        boolean lastElementWasIdenticalElement = false;
        while ((retval < 0 || lastElementWasIdenticalElement) && iterator.hasNext()) {
            lastElementWasIdenticalElement = false;
...