Java Basics:Java Library Methods-2
Java HashMap Methods
Java HashMap clear()
The Java HashMap clear() method removes all the key/value pairs from the hashmap.
The syntax of the clear()
method is:
hashmap.clear()
Here, hashmap is an object of the HashMap
class.
clear() Parameters
The clear()
method does not take any parameters.
clear() Return Value
The clear()
method does not return any value. Rather, it makes changes to the hashmap.
Example: Java HashMap clear()
import java.util.HashMap;
class Main {
public static void main(String[] args) {
HashMap<String, Integer> numbers = new HashMap<>();
numbers.put("One", 1);
numbers.put("Two", 2);
numbers.put("Three", 3);
System.out.println("HashMap: " + numbers);
// remove all mappings from HashMap
numbers.clear();
System.out.println("HashMap after clear(): " + numbers);
}
}
Output
HashMap: {One=1, Two=2, Three=3} HashMap after clear(): {}
In the above example, we have created a hashmap named numbers. Here, we have used the clear()
method to remove all the key/value pairs from numbers.
Note: We can use the Java HashMap remove() method to remove a single item from the hashmap.
Reinitialize The HashMap
In Java, we can achieve the functionality of the clear()
method by reinitializing the hashmap. For example,
import java.util.HashMap;
class Main {
public static void main(String[] args) {
HashMap<String, Integer> numbers = new HashMap<>();
numbers.put("One", 1);
numbers.put("Two", 2);
numbers.put("Three", 3);
System.out.println("HashMap: " + numbers);
// reinitialize the hashmap
numbers = new HashMap<>();
System.out.println("New HashMap: " + numbers);
}
}
Output
HashMap: {One=1, Two=2, Three=3} New HashMap: {}
In the above example, we have created a hashmap named numbers. The hashmap consists of 3 elements. Notice the line,
numbers = new HashMap<>();
Here, the process doesn't remove all items from the hashmap. Instead, it creates a new hashmap and assigns the newly created hashmap to numbers. And, the older hashmap is removed by Garbage Collector.
Note: It may appear that the reinitialization of HashMap
and the clear()
method is working in similar way. However, they are two different processes.
Java HashMap clone()
The Java HashMap clone() method makes the shallow copy of the hashmap and returns it.
Here, the shallow copy means the keys and values are not copied. Instead, references to keys/values are copied.
The syntax of the clone()
method is:
hashmap.clone()
Here, hashmap is an object of the HashMap
class.
clone() Parameters
The clone()
method does not take any parameters.
clone() Return Value
- returns a copy of the
HashMap
instances (objects)
Example 1: Make a Copy of HashMap
import java.util.HashMap;
class Main {
public static void main(String[] args){
// create HashMap
HashMap<String, Integer> languages = new HashMap<>();
languages.put("Java", 14);
languages.put("Python", 3);
languages.put("JavaScript", 1);
System.out.println("HashMap: " + languages);
// create copy of languages
HashMap<String, Integer> cloneLanguages = (HashMap<String, Integer>)languages.clone();
System.out.println("Cloned HashMap: " + cloneLanguages);
}
}
Output
HashMap: {Java=14, JavaScript=1, Python=3} Cloned HashMap: {Java=14, JavaScript=1, Python=3}
In the above example, we have created an hashmap named languages. Notice the expression,
(HashMap<String, Integer>)languages.clone()
Here,
languages.clone()
- returns a copy of the object languages(HashMap<String, Integer>)
- converts object returned byclone()
into a hashmap ofString
type key andInteger
type values
Example 2: Print the Return Value of clone()
import java.util.HashMap;
class Main {
public static void main(String[] args){
// create a hashmap
HashMap<String, Integer> primeNumbers = new HashMap<>();
primeNumbers.put("Two", 2);
primeNumbers.put("Three", 3);
primeNumbers.put("Five", 5);
System.out.println("Numbers: " + primeNumbers);
// print the return value of clone()
System.out.println("Return value of clone(): " + primeNumbers.clone());
}
}
Output
Prime Numbers: {Five=5, Two=2, Three=3} Return value of clone(): {Five=5, Two=2, Three=3}
In the above example, we have created an hashmap named primeNumbers. Here, we have printed the value returned by clone()
.
Note: The clone()
method is not specific to the HashMap
class. Any class that implements the Clonable
interface can use the clone()
method.
Comments
Post a Comment