Example usage for java.util List isEmpty

List of usage examples for java.util List isEmpty

Introduction

In this page you can find the example usage for java.util List isEmpty.

Prototype

boolean isEmpty();

Source Link

Document

Returns true if this list contains no elements.

Usage

From source file:Main.java

public static boolean isEmpty(List list) {
    if (list == null) {
        return true;
    } else {/*  w  w w.jav a2  s  .  co  m*/
        return list.isEmpty();
    }
}

From source file:Main.java

/**
 * Splits list into several sub-lists according to predicate.
 * @param <T>/* w  w w. ja v a2s  .  c  om*/
 * @param list
 * @param predicate
 * @return 
 */
public static final <T> List<List<T>> split(List<T> list, Predicate<T> predicate) {
    if (list.isEmpty()) {
        return Collections.EMPTY_LIST;
    }
    List<List<T>> lists = new ArrayList<>();
    boolean b = predicate.test(list.get(0));
    int len = list.size();
    int start = 0;
    for (int ii = 1; ii < len; ii++) {
        boolean t = predicate.test(list.get(ii));
        if (b != t) {
            lists.add(list.subList(start, ii));
            start = ii;
            b = t;
        }
    }
    lists.add(list.subList(start, len));
    return lists;
}

From source file:Main.java

@SuppressWarnings("rawtypes")
public static boolean isEmpty(Object array) {
    if (null != array) {
        if (array instanceof List) {
            List list = (List) array;
            if (!list.isEmpty()) {
                return false;
            }/*w ww. j av a 2s .  c  o m*/
        } else if (array instanceof Map) {
            Map map = (Map) array;
            if (!map.isEmpty()) {
                return false;
            }
        } else if (array instanceof Object[]) {
            Object[] arr = (Object[]) array;
            if (arr.length > 0) {
                return false;
            }
        }
    }

    return true;
}

From source file:Main.java

/** Credit Victor from StackOverflow
 * Combines several collections of elements and create permutations of all of them, taking one element from each
 * collection, and keeping the same order in resultant lists as the one in original list of collections.
 *
 * <ul>Example/*from w  w  w  . ja v  a2 s . co m*/
 * <li>Input  = { {a,b,c} , {1,2,3,4} }</li>
 * <li>Output = { {a,1} , {a,2} , {a,3} , {a,4} , {b,1} , {b,2} , {b,3} , {b,4} , {c,1} , {c,2} , {c,3} , {c,4} }</li>
 * </ul>
 *
 * @param collections Original list of collections which elements have to be combined.
 * @return Resultant collection of lists with all permutations of original list.
 */
public static <T> Collection<List<T>> permutations(List<Collection<T>> collections) {
    if (collections == null || collections.isEmpty()) {
        return Collections.emptyList();
    } else {
        Collection<List<T>> res = Lists.newLinkedList();
        permutationsImpl(collections, res, 0, new LinkedList<T>());
        return res;
    }
}

From source file:Main.java

public static boolean isApplicationBroughtToBackground(final Context context) {
    try {// ww  w . jav  a 2s .  c  o  m
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
        if (!tasks.isEmpty()) {
            ComponentName topActivity = tasks.get(0).topActivity;
            if (!topActivity.getPackageName().equals(context.getPackageName())) {
                return true;
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return false;
}

From source file:Main.java

@Nullable
static <T> List<T> nonEmptyOrNull(Collection<T> collection) {
    List<T> list = Collections.unmodifiableList(new ArrayList<>(collection));
    return list.isEmpty() ? null : list;
}

From source file:Main.java

/**
 * Used to know if Apollo was sent into the background
 * /* w  w  w  .  j  ava2  s.  c om*/
 * @param context The {@link Context} to use
 */
public static final boolean isApplicationSentToBackground(final Context context) {
    final ActivityManager activityManager = (ActivityManager) context
            .getSystemService(Context.ACTIVITY_SERVICE);
    final List<RunningTaskInfo> tasks = activityManager.getRunningTasks(1);
    if (!tasks.isEmpty()) {
        final ComponentName topActivity = tasks.get(0).topActivity;
        if (!topActivity.getPackageName().equals(context.getPackageName())) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

/**
 * Returns the first element of the collection.
 * //  w w  w  . jav a  2  s.c om
 * @param <T>
 *            the element type
 * @param iterable
 *            the collection
 * @return the first element or null if the list is empty
 */
public static <T> T first(Iterable<T> iterable) {
    if (iterable instanceof Deque<?>)
        return ((Deque<T>) iterable).getFirst();
    if (iterable instanceof List<?>) {
        List<T> list = (List<T>) iterable;
        return list.isEmpty() ? null : list.get(0);
    }
    Iterator<T> iterator = iterable.iterator();
    return iterator.hasNext() ? iterator.next() : null;
}

From source file:Main.java

public static Element element(Element element) {
    Element onlyChild = null;//w w  w  .  j  a  v  a2  s  . c  o m
    List elements = elements(element);
    if (!elements.isEmpty()) {
        onlyChild = (Element) elements.get(0);
    }
    return onlyChild;
}

From source file:Main.java

public static int hashCode(List list) {
    if (list == null) {
        return 0;
    }/*from ww  w.j  av  a  2 s  . c o m*/
    if (list.isEmpty()) {
        return 1;
    }
    ArrayList<?> tmp = new ArrayList<>(list);
    Collections.sort(tmp, (o1, o2) -> Integer.compare(o1.hashCode(), o2.hashCode()));
    return tmp.hashCode();
}