get First Element from List - Android java.util

Android examples for java.util:List Element

Description

get First 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(getFirst(list));
    }//w  w  w .j a  va  2 s  . c  o m
    public static <T> T getFirst(List<T> list) {
        if (isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }
    public static <T> T getFirst(T[] array) {
        if (isEmpty(array)) {
            return null;
        }
        return array[0];
    }
    public static <T> T getFirst(List<T> list, Matcher<? super T> matcher) {
        if (isEmpty(list)) {
            return null;
        }
        for (T obj : list) {
            if (matcher.matches(obj)) {
                return obj;
            }
        }
        return null;
    }
    public static <T> T getFirst(List<T> list,
            ListMatcher<? super T> matcher) {
        if (isEmpty(list)) {
            return null;
        }
        for (int I = 0; I < list.size(); I++) {
            T obj = list.get(I);
            if (matcher.matches(I, obj)) {
                return obj;
            }
        }
        return null;
    }
    public static <T> T getFirst(T[] array, Matcher<? super T> matcher) {
        if (isEmpty(array)) {
            return null;
        }
        for (T obj : array) {
            if (matcher.matches(obj)) {
                return obj;
            }
        }
        return null;
    }
    public static <T> T getFirst(T[] array, ListMatcher<? super T> matcher) {
        if (isEmpty(array)) {
            return null;
        }
        for (int I = 0; I < array.length; 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