Clone a map in Java

Description

The following code shows how to clone a map.

Example


/* www  . jav a  2  s.  c o m*/
import java.util.HashMap;
import java.util.Map;


public class Main {
  /**
   * Clones a map.
   *
   * @param source the source map
   * @return the clone of the source map
   */
  public static Map copy(Map source) {

      if(source == null) {
          return null;
      }

      Map result = new HashMap();
      result.putAll(source);
      return result;
  }

}




















Home »
  Java Tutorial »
    Java Collection »




Java ArrayList
Java Collection
Java Comparable
Java Comparator
Java HashMap
Java HashSet
Java Iterator
Java LinkedHashMap
Java LinkedHashSet
Java LinkedList
Java List
Java ListIterator
Java Map
Queue
Java Set
Stack
Java TreeMap
TreeSet