Home  >  Article  >  Java  >  Java uses the entrySet() function of the Map class to obtain a collection of key-value pairs

Java uses the entrySet() function of the Map class to obtain a collection of key-value pairs

WBOY
WBOYOriginal
2023-07-25 18:03:161721browse

Java uses the entrySet() function of the Map class to obtain a collection of key-value pairs

In Java programming, Map is a commonly used data structure that can store key-value pairs. Using Map makes it easy to store and search data. In Map, we can get the corresponding value by key, but sometimes we need to get the entire collection of key-value pairs. At this time, we can use the entrySet() function of the Map class to achieve this.

The entrySet() function of the Map class returns a Set collection, which contains all key-value pairs in the Map. Each key-value pair is stored as a Map.Entry. Map.Entry is a static inner class that represents a key-value pair in Map. By calling the getKey() and getValue() functions of Map.Entry, we can obtain the key and value in the key-value pair.

The following is a simple sample code that shows how to use the entrySet() function to obtain a collection of key-value pairs.

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

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

        // 向Map中添加键值对
        map.put("key1", "value1");
        map.put("key2", "value2");
        map.put("key3", "value3");
        map.put("key4", "value4");

        // 使用entrySet()函数获取键值对的集合
        Set<Map.Entry<String, String>> entrySet = map.entrySet();

        // 遍历键值对的集合,并输出键和值
        for (Map.Entry<String, String> entry : entrySet) {
            String key = entry.getKey();
            String value = entry.getValue();
            System.out.println("Key: " + key + ", Value: " + value);
        }
    }
}

In the above sample code, we first create a HashMap object as an instance of Map. Then, we added four key-value pairs to the Map. Next, we use the entrySet() function to obtain a collection of key-value pairs and save this collection in a Set object.

Next, we use a for-each loop to traverse the collection of key-value pairs and obtain the key and value by calling the getKey() and getValue() functions. Finally, we print the key and value to the console.

Run the above example code, we can see the output as follows:

Key: key1, Value: value1
Key: key2, Value: value2
Key: key3, Value: value3
Key: key4, Value: value4

By using the entrySet() function, we can easily get all the key-value pairs in the Map and compare them Perform traversal and operations. This method is very practical when you need to traverse all key-value pairs in the Map.

To summarize, the Map class in Java provides the entrySet() function to obtain a collection of key-value pairs. By traversing each Map.Entry object in the collection, we can obtain the key and value. By using the entrySet() function, we can more conveniently operate the key-value pairs in the Map. I hope this article will help you learn to use the entrySet() function of the Map class!

The above is the detailed content of Java uses the entrySet() function of the Map class to obtain a collection of key-value pairs. 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