get Last Element from List - Android java.util

Android examples for java.util:List Element

Description

get Last Element from List

Demo Code


import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.StringTokenizer;

public class Main{
    public static void main(String[] argv){
        List list = java.util.Arrays.asList("asdf","book2s.com");
        System.out.println(getLast(list));
    }/*w ww. j  a  v  a  2s .c o  m*/
    public static <T> T getLast(List<T> list) {
        if (isEmpty(list)) {
            return null;
        }
        return list.get(list.size() - 1);
    }
    public static <T> T getLast(T[] array) {
        if (isEmpty(array)) {
            return null;
        }
        return array[array.length - 1];
    }
    public static <T> T getLast(List<T> list, Matcher<? super T> matcher) {
        if (isEmpty(list)) {
            return null;
        }
        ListIterator<T> it = list.listIterator(list.size());
        while (it.hasPrevious()) {
            T obj = it.previous();
            if (matcher.matches(obj)) {
                return obj;
            }
        }
        return null;
    }
    public static <T> T getLast(List<T> list, ListMatcher<? super T> matcher) {
        if (isEmpty(list)) {
            return null;
        }
        for (int I = list.size() - 1; I >= 0; I--) {
            T obj = list.get(I);
            if (matcher.matches(I, obj)) {
                return obj;
            }
        }
        return null;
    }
    public static <T> T getLast(T[] array, Matcher<? super T> matcher) {
        if (isEmpty(array)) {
            return null;
        }
        for (int I = array.length - 1; I >= 0; I--) {
            T obj = array[I];
            if (matcher.matches(obj)) {
                return obj;
            }
        }
        return null;
    }
    public static <T> T getLast(T[] array, ListMatcher<? super T> matcher) {
        if (isEmpty(array)) {
            return null;
        }
        for (int I = array.length - 1; I >= 0; I--) {
            T obj = array[I];
            if (matcher.matches(I, obj)) {
                return obj;
            }
        }
        return null;
    }
    public static boolean isEmpty(Collection<?> c) {
        return c == null || c.isEmpty();
    }
    public static boolean isEmpty(Map<?, ?> map) {
        return map == null || map.isEmpty();
    }
    @SafeVarargs
    public static <T> boolean isEmpty(T... array) {
        return array == null || array.length == 0;
    }
}

Related Tutorials