Home >Java >javaTutorial >Java uses the put() function of the HashMap class to add key-value pairs to the collection

Java uses the put() function of the HashMap class to add key-value pairs to the collection

王林
王林Original
2023-07-24 18:19:541739browse

Java uses the put() function of the HashMap class to add key-value pairs to the collection

HashMap is a class in the Java collection framework. It implements the Map interface and is used to store key-value pairs. It uses a hash table to store data and provides fast lookup and insertion operations.

In HashMap, you can add key-value pairs to the collection through the put() function. The put() function has two parameters, the first parameter is the key to be added, and the second parameter is the value to be added. The following is a sample code that uses the put() function to add key-value pairs to a HashMap:

import java.util.HashMap;

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

        // 使用put()函数添加键值对
        map.put("apple", 1);
        map.put("banana", 2);
        map.put("orange", 3);

        // 打印HashMap中的键值对
        System.out.println("HashMap中的键值对:");
        for (String key : map.keySet()) {
            System.out.println(key + ":" + map.get(key));
        }
    }
}

Run the above code, you can get the following output:

HashMap中的键值对:
orange:3
banana:2
apple:1

The above sample code creates a HashMap object , and then use the put() function to add three key-value pairs in sequence: "apple" corresponds to a value of 1, "banana" corresponds to a value of 2, and "orange" corresponds to a value of 3. Finally, all key-value pairs in the HashMap can be printed out by traversing map.keySet().

It should be noted that in HashMap, the key is unique. If you use the put() function to add the same key, the new value will overwrite the old value. For example:

import java.util.HashMap;

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

        // 使用put()函数添加键值对
        map.put("apple", 1);
        map.put("banana", 2);
        map.put("apple", 3);

        // 打印HashMap中的键值对
        System.out.println("HashMap中的键值对:");
        for (String key : map.keySet()) {
            System.out.println(key + ":" + map.get(key));
        }
    }
}

Run the above code, you can get the following output:

HashMap中的键值对:
banana:2
apple:3

You can see that when the same key "apple" is added, the new value 3 overwrites the old value 1.

In summary, using the put() function of HashMap can easily add key-value pairs to the collection. Its usage is simple and straightforward, just pass in the key and value. At the same time, it should be noted that the keys in HashMap are unique, and the same key will cause the old key-value pair to be overwritten. Therefore, you need to pay attention to the uniqueness of the key when using the put() function.

The above is the detailed content of Java uses the put() function of the HashMap class to add key-value pairs to the collection. 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