Creates defensive copy of list if - Java java.util

Java examples for java.util:List Creation

Description

Creates defensive copy of list if

Demo Code


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

public class Main {
    public static void main(String[] argv) {
        List list = java.util.Arrays.asList("asdf", "java2s.com");
        System.out.println(defensiveCopy(list));
    }/*from   w ww .  j av a  2  s.co m*/

    /**
     * Creates defensive copy of list if
     * @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<>();
    }
}

Related Tutorials