Java examples for Collection Framework:Collections Utility Methods
Create unmodifiable Collection
//package com.java2s; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.LinkedList; import java.util.List; public class Main { public static void main(String[] argv) { Collection c = java.util.Arrays.asList("asdf", "java2s.com"); System.out.println(unmodifiableCollection(c)); }//from www . j a v a 2 s .c om /** * @return an <b>UNMODIFIABLE</b> Collection<T> */ public static <T> Collection<T> unmodifiableCollection( final Collection<? extends T> c) { return (c == null) ? Collections.<T> emptyList() : Collections .unmodifiableCollection(c); } public static <T> List<T> asList(final Iterable<? extends T> iterable) { return (iterable instanceof Collection) ? new LinkedList<T>( (Collection<? extends T>) iterable) : new LinkedList<T>() { private static final long serialVersionUID = 3109256773218160485L; { if (iterable != null) { for (final T t : iterable) { add(t); } } } }; } public static <T> List<T> asList(final T t, final T... ts) { final ArrayList<T> list = new ArrayList<T>(ts.length + 1); list.add(t); Collections.addAll(list, ts); return list; } }