Home >Java >javaTutorial >Use java's HashMap.clear() function to delete all elements in the HashMap

Use java's HashMap.clear() function to delete all elements in the HashMap

王林
王林Original
2023-07-24 22:17:081500browse

Use Java's HashMap.clear() function to delete all elements in HashMap

In the development process of Java, we often use the HashMap class to store and manage a series of key-value pair data. When we need to delete all elements in a HashMap, we can use the clear() function of HashMap. This article will introduce how to use the HashMap.clear() function and give corresponding code examples.

HashMap is a commonly used collection class in Java, used to store key-value pair data. Its internal implementation is a hash table-based data structure that provides fast insertion, search, and deletion operations. Each element of a HashMap contains a key and a corresponding value, which allows us to quickly find the corresponding value through the key. But sometimes, we need to clear all elements in HashMap. At this time, you can use the clear() function of HashMap to achieve it.

The clear() function of HashMap is to delete all elements in HashMap. It will clear the keys and values ​​in the HashMap, making the HashMap an empty collection. We can demonstrate the use of the clear() function through the following code example:

import java.util.HashMap;

public class Main {
    public static void main(String[] args) {
        // 创建一个新的HashMap对象
        HashMap<String, Integer> hashMap = new HashMap<>();

        // 向HashMap中插入一些键值对数据
        hashMap.put("apple", 50);
        hashMap.put("banana", 30);
        hashMap.put("orange", 20);
        hashMap.put("strawberry", 40);

        // 打印HashMap中的元素数量
        System.out.println("元素数量:" + hashMap.size());

        // 清空HashMap中的所有元素
        hashMap.clear();

        // 打印HashMap中的元素数量
        System.out.println("元素数量:" + hashMap.size());
    }
}

In the above code example, we first created a HashMap object and inserted it into the HashMap through the put() function. Some key-value pair data. Then, we use the clear() function to clear all elements in the HashMap. Finally, we printed the number of elements in the HashMap through the size() function.

Run the above code, we can observe the output as follows:

元素数量:4
元素数量:0

You can see that before calling the clear() function, there are 4 elements in the HashMap; and after calling the clear() After the function, the number of elements in the HashMap becomes 0, that is, all elements are successfully deleted.

It should be noted that the clear() function deletes all elements in the HashMap, rather than deleting the HashMap object itself. When we re-insert new key-value pair data into the HashMap, the original space can still be retained for subsequent use.

To summarize, use Java's HashMap.clear() function to quickly delete all elements in a HashMap. By clearing the HashMap, we can reuse it to store new key-value pair data. When you need to clear the HashMap, you only need to simply call the clear() function. I hope this article will be helpful to you in the process of using HashMap.

The above is the detailed content of Use java's HashMap.clear() function to delete all elements in the HashMap. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn