Here you can find the source of copyAndClearSet(SortedSet
Parameter | Description |
---|---|
sourceSet | set to be copied |
public static SortedSet<String> copyAndClearSet(SortedSet<String> sourceSet)
//package com.java2s; import java.util.Iterator; import java.util.SortedSet; import java.util.TreeSet; public class Main { /**//from ww w . j ava 2s .c o m * Copy items from source to target set; then clear source set and set it to null * @param sourceSet set to be copied * @return deep copy of set */ public static SortedSet<String> copyAndClearSet(SortedSet<String> sourceSet) { TreeSet<String> newSet = null; if (sourceSet != null) { newSet = new TreeSet<String>(); Iterator<String> iter = sourceSet.iterator(); while (iter.hasNext()) { String oldKey = iter.next(); String key = new String(oldKey); newSet.add(key); oldKey = null; } for (String copyKey : newSet) { sourceSet.remove(copyKey); } } return newSet; } }