Example usage for java.util List size

List of usage examples for java.util List size

Introduction

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

Prototype

int size();

Source Link

Document

Returns the number of elements in this list.

Usage

From source file:Main.java

public static String joinSelectionCriteria(List<String> input) {
    if (input.size() == 0) {
        return null;
    }//from  w  ww  .ja v a2s  .  c om
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < input.size(); i++) {
        if (i > 0) {
            sb.append(" AND ");
        }
        sb.append(input.get(i));
    }
    return sb.toString();
}

From source file:Main.java

public static <T> List<T> takeLast(List<T> list, int length) {
    if (list.size() > length) {
        int fromIndex = Math.max(0, list.size() - length);
        int toIndex = list.size();
        List<T> newList = new ArrayList<T>();
        newList.addAll(list.subList(fromIndex, toIndex));
        return newList;
    }/*ww  w  .j a  v  a 2 s .c om*/
    return list;
}

From source file:Main.java

public static <E> E sample(List<E> l, Random r) {
    int i = r.nextInt(l.size());
    return l.get(i);
}

From source file:Main.java

/**
 * Creates defensive copy of list if/*from  w ww  .  j av a 2 s .com*/
 * @param list source list to be copied.
 * @param <T> type of list.
 * @return defensive copy of non-empty list or empty List.
 */
public static <T> List<T> defensiveCopy(List<T> list) {
    return list != null && list.size() > 0 ? new ArrayList<>(list) : new ArrayList<>();
}

From source file:Main.java

public static <T> boolean isNotEmpty(List<T> list) {
    return (list != null && list.size() != 0);
}

From source file:Main.java

/**
 * Return the first item of <code>l</code> if it's the only one, otherwise <code>null</code>.
 * // ww  w  .  ja v  a  2  s  . c  om
 * @param <T> type of list.
 * @param l the list.
 * @return the first item of <code>l</code> or <code>null</code>.
 */
public static <T> T getSole(List<T> l) {
    return l.size() == 1 ? l.get(0) : null;
}

From source file:Main.java

public static <T> boolean isEmpty(List<T> list) {
    return (list != null && list.size() == 0);
}

From source file:Main.java

public static <T> List<T> toImmutableList(List<T> list) {
    switch (list.size()) {
    case 0:// w w w .  j av  a2s  .  c  o  m
        return Collections.emptyList();
    case 1:
        return Collections.singletonList(list.get(0));
    default:
        return Collections.unmodifiableList(list);
    }
}

From source file:Main.java

public static <T> boolean IsListEmpty(final List<T> list) {
    return list == null || list.size() == 0;
}

From source file:Main.java

public static byte[] toByteArray(List<Byte> list) {
    byte[] ret = new byte[list.size()];
    for (int i = 0; i < ret.length; i++)
        ret[i] = list.get(i);//from  w w  w. j av a 2 s  . co  m
    return ret;
}