Example usage for java.util Collections unmodifiableList

List of usage examples for java.util Collections unmodifiableList

Introduction

In this page you can find the example usage for java.util Collections unmodifiableList.

Prototype

public static <T> List<T> unmodifiableList(List<? extends T> list) 

Source Link

Document

Returns an unmodifiable view of the specified list.

Usage

From source file:Main.java

/**
 * Like Collections.unmodifiableList(), but with checks for null values.
 *//*from  w  w  w .  ja v  a  2  s .co  m*/
public static <T> List<T> unmodifiableList(List<T> list) {

    if (list == null) {
        return Collections.emptyList();
    }

    return Collections.unmodifiableList(list);
}

From source file:Main.java

public static <T> List<T> asConstList(Collection<T> in) {
    ArrayList<T> ret = new ArrayList(in);
    ret.trimToSize();/*w w w  . j a va 2s  .  c  o m*/
    return Collections.unmodifiableList(ret);
}

From source file:Main.java

/**
 * Converts the specified Array to an unmodifiable List containing the same elements.
 *
 * @param source Array of Objects to be converted to an unmodifiable List.
 * @return List of Objects containing the elements of the specified Array.
 *///from ww  w . jav a 2s .  co m
public static <E> List<E> unmodifiableList(E... source) {
    return Collections.unmodifiableList(Arrays.asList(source));
}

From source file:Main.java

public static <T> List<T> unmodifiableArrayList(Collection<? extends T> elements, Class<T> castClass) {
    return Collections.unmodifiableList(new ArrayList<T>(elements));
}

From source file:Main.java

/**
 * Crates an unmodifiable list filled with the given values.
 * //from  w  w w.j a v a 2s. c o  m
 * @param <T>
 *            the list's element type.
 * @param values
 *            the values.
 * @return the list.
 */
@SafeVarargs
public static <T> List<T> createUnmodifiableList(final T... values) {
    return Collections.unmodifiableList(createArrayList(values));
}

From source file:Main.java

public static <T> List<T> readOnlyCopy(List<T> orig) {
    if (orig.isEmpty())
        return Collections.emptyList();
    if (orig.size() == 1)
        return Collections.singletonList(orig.get(0));
    ArrayList<T> copy = new ArrayList<>(orig.size());
    copy.addAll(orig);// w  w w  . j  a v  a 2s  .co  m
    return Collections.unmodifiableList(copy);
}

From source file:Main.java

public static final <T> List<T> collect(Iterator<? extends T> it) {
    ArrayList<T> r = new ArrayList<T>();
    while (it.hasNext())
        r.add(it.next());//from  w w w. j av a 2  s  . c o  m
    return Collections.unmodifiableList(r);
}

From source file:Main.java

public static ArrayList<Pattern> createArrayListPFromStringArray(final String[] regexArray) {
    final ArrayList<Pattern> returnPatternsList = new ArrayList<Pattern>();
    for (int j = 0; j < regexArray.length; j++) {
        returnPatternsList.add(Pattern.compile(regexArray[j], Pattern.DOTALL));
    }/*from  w  ww  .ja  v  a 2s . c o m*/
    return new ArrayList<Pattern>(Collections.unmodifiableList(returnPatternsList));
}

From source file:Main.java

public static <K> Map<K, List<?>> copyNullSafeMultiHashMap(Map<? extends K, List<?>> map) {
    if (map == null)
        throw new NullPointerException("map");

    Map<K, List<?>> result = copyNullSafeMutableHashMap(map);
    for (Map.Entry<K, List<?>> entry : result.entrySet()) {
        List<?> value = entry.getValue();
        entry.setValue(Collections.unmodifiableList(new ArrayList<Object>(value)));
    }/*  w ww  .j av a2 s  .  c  o m*/
    return result;
}

From source file:Main.java

public static <T> List<T> toImmutableList(final Collection<T> c) {
    if (c.isEmpty()) {
        return Collections.emptyList();
    } else {/*  w ww. ja v  a 2s  . c om*/
        return Collections.unmodifiableList(new ArrayList<T>(c));
    }
}