HashMap clone() in Java

clone(): This method is available in java.util.HashMap class of Java.

Syntax:

Object java.util.HashMap.clone()

This method returns a shallow copy of this HashMap instance: the keys and values themselves are not cloned.

Parameters: NA

Returns: a shallow copy of this map.

Exceptions: NA

Approach

Java

import java.util.HashMap;

public class HashMapclone {
    public static void main(String[] args) {
        HashMap<String, Integer> hashMap = new
HashMap<String, Integer>();

        hashMap.put("Hello", 1);
        hashMap.put("World", 2);

        System.out.println(hashMap.clone());
    }
}

Output:

{Hello=1, World=2}


No comments:

Post a Comment