Java List Copy copyAndClearList(List sourceList)

Here you can find the source of copyAndClearList(List sourceList)

Description

Copy items from source to target list; then clear source list and set it to null

License

Open Source License

Parameter

Parameter Description
sourceList List to be copied

Return

deep copy of list

Declaration

public static List<String> copyAndClearList(List<String> sourceList) 

Method Source Code


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

import java.util.List;

public class Main {
    /**/*from   w  w  w.ja va 2  s .co  m*/
     * Copy items from source to target list; then clear source list and set it to null
     * @param sourceList List to be copied
     * @return deep copy of list
     */
    public static List<String> copyAndClearList(List<String> sourceList) {
        ArrayList<String> newList = null;
        if (sourceList != null) {
            newList = new ArrayList<String>();
            for (int i = 0; i < sourceList.size(); i++) {
                String oldString = sourceList.set(i, null);
                String newItem = new String(oldString);
                oldString = null;
                newList.add(newItem);
            }
            sourceList.clear();

        }
        return newList;
    }
}

Related

  1. copy(List list)
  2. copy(List original)
  3. copy(List toCopy)
  4. copy(List toCopy)
  5. copy(List[] sourceLists, List[] destLists)
  6. copyAndRemove(final List in, final String remove)
  7. copyAndSet(final Map> in, final String key, final String value)
  8. copyArrayToList(Object[] input, List output)
  9. copyFirst(List list, int count)