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 long[] toLongArray(List<Long> list) {
    if (list == null || list.isEmpty())
        return null;
    long[] arr = new long[list.size()];
    for (int i = 0; i < list.size(); i++) {
        arr[i] = list.get(i).longValue();
    }/*from   www.  j a v  a 2 s  . c  o  m*/
    return arr;
}

From source file:Main.java

/**
 * Returns the index of the maximum element in the list, using the elements'
 * natural ordering, or -1 if the list is empty.
 * @param list a list//from  www. j ava  2 s .  c o  m
 * @return the index of the maximum element
 */
public static <T extends Comparable<? super T>> int maxIndex(List<T> list) {
    if (list.isEmpty())
        return -1;
    if (list.size() == 1)
        return 0;
    int best = 0;
    //TODO: alternative implementation for non-RandomAccess lists?
    for (int i = 1; i < list.size(); ++i)
        if (list.get(i).compareTo(list.get(best)) > 0)
            best = i;
    return best;
}

From source file:Main.java

/**
 * Returns first item in the list if list is not empty.
 *
 * @param list The list to get first item from.
 *///from w ww.j  a v a 2 s.  c o  m
@Nullable
public static <T> T first(@Nullable final List<T> list) {
    return (list == null || list.isEmpty() ? null : list.get(0));
}

From source file:Main.java

public static int getAllListViewSectionCounts(ListView lv, List dataSource) {
    if (null == lv || dataSource == null || dataSource.isEmpty()) {
        return 0;
    }/*  w  w  w. j  a  v  a 2s. c o m*/
    return dataSource.size() + lv.getHeaderViewsCount() + lv.getFooterViewsCount();
}

From source file:Main.java

/**
 * Returns the index of the minimum element in the list, using the elements'
 * natural ordering, or -1 if the list is empty.
 * @param list a list// w  ww  .jav a  2  s. co  m
 * @return the index of the minimum element
 */
public static <T extends Comparable<? super T>> int minIndex(List<T> list) {
    if (list.isEmpty())
        return -1;
    if (list.size() == 1)
        return 0;
    int best = 0;
    //TODO: alternative implementation for non-RandomAccess lists?
    for (int i = 1; i < list.size(); ++i)
        if (list.get(i).compareTo(list.get(best)) < 0)
            best = i;
    return best;
}

From source file:Main.java

public static <T extends Comparator<T>> Boolean isEmpty(List<T> collection) {
    return collection == null || collection.isEmpty();
}

From source file:Main.java

public static <T> String listToString(List<T> list) {
    if (null == list || list.isEmpty()) {
        return "";
    }/*from  w  w w  . ja  va  2 s  . c o m*/
    StringBuilder sb = new StringBuilder();
    for (T i : list) {
        sb.append(i.toString());
        sb.append(",");
    }
    int index = sb.lastIndexOf(",");
    if (-1 != index) {
        sb.deleteCharAt(index);
    }
    return sb.toString();
}

From source file:Main.java

public static Double listAvg(List<Double> list) {
    if (list == null || list.isEmpty()) {
        return null;
    }/* w  w w  .ja  v a2s . c  o m*/

    Double listSum = listSum(list);

    return listSum / list.size();
}

From source file:Main.java

public static <T> T getSingleResult(List<T> results) {
    return (results == null || results.isEmpty()) ? null : results.get(0);
}

From source file:Main.java

private static String extract(Uri uri, int index) {
    List<String> pathSegments = uri.getPathSegments();
    if (pathSegments.isEmpty() || pathSegments.size() < index)
        return null;
    return pathSegments.get(index);
}