Home  >  Article  >  Java  >  Use java's HashMap.put() function to add mapping relationships to HashMap

Use java's HashMap.put() function to add mapping relationships to HashMap

PHPz
PHPzOriginal
2023-07-24 15:51:211311browse

Title: Use Java's HashMap.put() function to add mapping relationships to HashMap

Introduction:
In Java programming, using HashMap is very common and important. HashMap is a collection of key-value pairs that can store and retrieve data. This article will focus on how to use Java's HashMap.put() function to add a mapping relationship to a HashMap, and illustrate it with code examples.

  1. HashMap Introduction
    HashMap is an implementation based on a hash table, which uses key-value pairs to store data. The characteristics of HashMap include fast insertion and search speed, but the order of elements is not guaranteed. In HashMap, keys are unique but values ​​can be repeated.
    The HashMap class is defined in the Java.util package, so you need to import the package before use.
  2. HashMap.put() function
    HashMap.put() function is a method of the HashMap class, used to add mapping relationships to HashMap. Its method signature is as follows:
    public V put(K key, V value)
    where K represents the type of key and V represents the type of value. The function accepts a key and a value as parameters, and adds the mapping relationship between the key and the value to the HashMap.
  3. Sample code
    The following is a sample code that uses the HashMap.put() function to add a mapping relationship to a HashMap:
import java.util.HashMap;

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

        // 向HashMap中添加映射关系
        hashMap.put("apple", 10);
        hashMap.put("banana", 15);
        hashMap.put("orange", 8);

        // 打印HashMap的内容
        System.out.println("HashMap中的映射关系为:" + hashMap);
    }
}

In the above sample code, we first create A HashMap object is obtained, the key type is String, and the value type is Integer. Then, use the HashMap.put() function to add three mapping relationships to the HashMap, namely "apple" and 10, "banana" and 15, and "orange" and 8.
Finally, we printed the contents of the HashMap through the System.out.println() function.

  1. Result Analysis
    Running the above example code, we can get the following output:

    HashMap中的映射关系为:{orange=8, apple=10, banana=15}

    It can be seen that the mapping relationship in HashMap is in an unordered manner Stored with keys and values ​​separated by "=" symbols.

  2. Summary
    Through the introduction and sample code of this article, we have learned how to use Java's HashMap.put() function to add mapping relationships to HashMap. HashMap is a very important and commonly used data structure in Java programming, which can store and retrieve data efficiently. In practical applications, we can use HashMap to store and manage data according to specific needs, so that the program has efficient performance and good scalability.

Reference materials:
https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html

The above is the detailed content of Use java's HashMap.put() function to add mapping relationships to 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