Convert Collection to Immutable List - Java java.util

Java examples for java.util:Collection to List

Description

Convert Collection to Immutable List

Demo Code


//package com.java2s;
import java.util.ArrayList;

import java.util.Collection;
import java.util.Collections;

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(toImmutableList(c));
    }//ww w. j a  v  a  2  s.  c  o  m

    public static <T> List<T> toImmutableList(final Collection<T> c) {
        if (c.isEmpty()) {
            return Collections.emptyList();
        } else {
            return Collections.unmodifiableList(new ArrayList<T>(c));
        }
    }
}

Related Tutorials