Java List First Item getFirstLeaf(List aList)

Here you can find the source of getFirstLeaf(List aList)

Description

Returns the first non-list object in the given list hierarchy, recursing if a list is found.

License

Open Source License

Declaration

public static Object getFirstLeaf(List aList) 

Method Source Code


//package com.java2s;

import java.util.*;

public class Main {
    /**//ww w .  ja v a  2s. c om
     * Returns the first non-list object in the given list hierarchy, recursing if a list is found.
     */
    public static Object getFirstLeaf(List aList) {
        for (int i = 0, iMax = aList.size(); i < iMax; i++) {
            Object obj = aList.get(i);
            if (obj instanceof List)
                obj = getFirstLeaf((List) obj);
            if (obj != null)
                return obj;
        }

        return null;
    }

    /**
     * Returns the size of a list (accepts null list).
     */
    public static int size(List aList) {
        return aList == null ? 0 : aList.size();
    }

    /**
     * Returns the object at the given index (returns null object for null list or invalid index).
     */
    public static <T> T get(List<T> aList, int anIndex) {
        return aList == null || anIndex < 0 || anIndex >= aList.size() ? null : aList.get(anIndex);
    }
}

Related

  1. getFirstElement(List list)
  2. getFirstElementFromList(List list)
  3. getFirstFromList(List list)
  4. getFirstInstanceOf(List source, String className)
  5. getFirstItemOrNull(List items)
  6. getFirstNameIndex(List list)
  7. getFirstNonEmptyIndex(final List> list)
  8. getFirstObject(List valueList)
  9. getFirstOccurNic(List nics, String line)

  10. HOME | Copyright © www.java2s.com 2016