HashMap clone

In this chapter you will learn:

  1. How to clone a HashMap

Clone a HashMap

clone() creates a copy of the HashMap

import java.util.HashMap;
// j  ava 2  s. c  om
public class Main {
  public static void main(String[] a) {
    HashMap<String,String> map = new HashMap<String,String>();
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");

    HashMap map2 = (HashMap)map.clone();
    System.out.println(map2);
  }
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. What is Java TreeMap and how to use TreeMap
  2. A demo for TreeMap
Home » Java Tutorial » Map
Map interface
Map element adding
Map.Entry class
Map key
Map value
Map key/value search
Map delete/remove
Map comparison
HashMap Class
HashMap search
HashMap clone
TreeMap
TreeMap key
TreeMap head sub map
TreeMap tail sub map
TreeMap sub map
NavigableMap
NavigableMap key
NavigableMap key-value pair
LinkedHashMap Class
IdentityHashMap
SortedMap