Home > Java > javaTutorial > body text

Use java's HashMap.values() function to get all the values ​​in the HashMap

WBOY
Release: 2023-07-25 21:03:25
Original
1477 people have browsed it

Use Java's HashMap.values() function to obtain all values ​​in HashMap

In Java, HashMap is a commonly used data structure that can store key-value pairs and allows retrieval by key value. Sometimes, we need to get all the values ​​in the HashMap, then we can use the HashMap.values() function.

The HashMap.values() function returns a collection containing all values. This collection type is a subclass of the Collection class. We can use this collection to iterate through all values ​​in the HashMap or perform other operations. The following is a sample code:

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

public class Main {
    public static void main(String[] args) {
        // 创建一个HashMap
        HashMap<Integer, String> hashMap = new HashMap<>();
        
        // 向HashMap中添加键值对
        hashMap.put(1, "Apple");
        hashMap.put(2, "Banana");
        hashMap.put(3, "Orange");
        hashMap.put(4, "Grapes");
        
        // 使用HashMap.values()函数获取所有值的集合
        Collection<String> values = hashMap.values();
        
        // 遍历值的集合并输出
        for (String value : values) {
            System.out.println("值: " + value);
        }
    }
}
Copy after login

The above code first creates a HashMap and adds four key-value pairs to it. Then, use the HashMap.values() function to get the set of all values. Finally, iterate through the collection of values ​​through a for loop and output each value.

Run the above code, the following results will be output:

值: Apple
值: Banana
值: Orange
值: Grapes
Copy after login

You can see that all the values ​​in the HashMap are obtained using the HashMap.values() function, and these values ​​are successfully output.

If we need to perform other operations on the values ​​in HashMap, we can also use this collection object. For example, we can use collection methods to count the number of occurrences of a value, find specific values, and so on.

It should be noted that the values ​​in HashMap are unordered and duplicate values ​​are allowed. Therefore, ordering and uniqueness are not guaranteed when iterating through a collection of values. If we need to follow a specific order or remove duplicate values, we can do it before traversing.

To sum up, using Java’s HashMap.values() function can easily obtain all the values ​​in the HashMap. We can perform operations by traversing a collection of values, such as output values, statistical value occurrences, etc. When using HashMap, remember to use the HashMap.values() function appropriately to obtain values ​​for better subsequent processing.

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!