Home  >  Article  >  Java  >  Use the values() method of the HashMap class in Java to get all the values ​​in the map

Use the values() method of the HashMap class in Java to get all the values ​​in the map

WBOY
WBOYOriginal
2023-07-24 14:49:301701browse

Use the values() method of the HashMap class in Java to obtain all the values ​​in the map

HashMap is a commonly used data structure in Java. It implements the Map interface and provides storage and storage of key-value pairs. Search function. Keys in HashMap are unique, while values ​​can be repeated. In some scenarios, we need to get all the values ​​in the HashMap. In this case, we can use the values() method of HashMap.

The values() method of HashMap returns a Collection object, which contains all the values ​​​​in the HashMap. We can get each value by traversing the Collection object. Now let's illustrate this through code.

import java.util.HashMap;
import java.util.Collection;

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

        // 向HashMap中添加键值对
        hashMap.put("Apple", 10);
        hashMap.put("Banana", 20);
        hashMap.put("Cherry", 30);

        // 使用values()方法获取HashMap中所有的值
        Collection<Integer> values = hashMap.values();

        // 遍历Collection对象并打印每一个值
        for (int value : values) {
            System.out.println(value);
        }
    }
}

In the above code, we first create a HashMap object and add three key-value pairs to it. Then, we called the values() method to get all the values ​​in the HashMap and assign them to a Collection object. Finally, we print each value by looping through the Collection object and using the System.out.println() method.

Run the above code, we will get the following output:

10
20
30

As can be seen from the output, we successfully obtained all the values ​​​​in the HashMap, and followed the order we added. to print.

It should be noted that the values() method of HashMap returns a Collection object, not an array or list. Therefore, we cannot access specific values ​​through indexes, but can only iterate through each value.

In actual development, obtaining all the values ​​​​in the HashMap can help us perform some statistical or analytical operations. For example, counting the total number of items of a certain type or calculating the average value of items of a certain type, etc. Therefore, mastering the use of HashMap's values() method will be very helpful to our development work.

To sum up, all the values ​​in HashMap can be easily obtained through the values() method of HashMap. We just iterate through the returned Collection object to get each value. Please note in the code that we need to assign the Collection object returned by the values() method to a corresponding collection object.

I hope this article has provided some help for everyone to understand and use the values() method of HashMap. I wish you all good luck in using the related functions of HashMap when writing Java programs!

The above is the detailed content of Use the values() method of the HashMap class in Java to get all the values ​​in the map. 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