Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

import java.util.Collections;

import java.util.List;

public class Main {
    /**
     * Copies the contents of a List to a new, unmodifiable List.  If the List is null, an empty List will be returned.
     * <p>
     * This is especially useful for methods which take List parameters.  Directly assigning such a List to a member
     * variable can allow external classes to modify internal state.  Classes may have different state depending on the
     * size of a List; for example, a button might be disabled if the the List size is zero.  If an external object
     * can change the List, the containing class would have no way have knowing a modification occurred.
     * 
     * @param <T> The type of element in the List.
     * @param list The List to copy.
     * @return A new, unmodifiable List which contains all of the elements of the List parameter.  Will never be null.
     */
    public static <T> List<T> copyToUnmodifiableList(List<T> list) {
        if (list != null) {
            List<T> listCopy = new ArrayList(list);
            return Collections.unmodifiableList(listCopy);
        } else {
            return Collections.emptyList();
        }
    }
}