Java tutorial
//package com.java2s; import java.util.*; public class Main { /** * Returns the given list with the given element added at the end. * If it is an empty list, a new list is created. Otherwise, the given list is reused. * This method is especially useful for dealing with immutable empty lists, * which are used to save memory. */ public static <T> List<T> addOrNew(List<T> list, T element) { if (list.size() == 0) list = new ArrayList<T>(1); list.add(element); return list; } }