Home  >  Article  >  Java  >  Interpretation of Java documentation: Detailed explanation of the usage of the values() method of the HashMap class

Interpretation of Java documentation: Detailed explanation of the usage of the values() method of the HashMap class

PHPz
PHPzOriginal
2023-11-04 16:09:471437browse

Interpretation of Java documentation: Detailed explanation of the usage of the values() method of the HashMap class

Interpretation of Java documentation: Detailed explanation of the usage of the values() method of the HashMap class

The HashMap class is one of the commonly used data structures in Java. It provides a key value The right storage method. In actual development, we often encounter scenarios where we need to obtain all values ​​in HashMap. The values() method of the HashMap class is the method used to return all the values ​​in the HashMap. This article will analyze the usage of the values() method of the HashMap class in detail and give specific code examples.

First, we need to understand the basic usage of the values() method. According to the Java documentation, the values() method returns a Collection containing all the values ​​in the HashMap. This means that by calling this method, we can get all the values ​​stored in the HashMap and return them in the form of a collection. The following is a basic calling example of the values() method:

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

public class HashMapValuesExample {
    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");

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

        // 遍历输出所有值
        for (String value : values) {
            System.out.println(value);
        }
    }
}

Run the above code, we can get the output result:

Apple
Banana
Orange

As you can see, by calling the values() method of HashMap, we can Easily get all the values ​​in the HashMap and return them in the form of a collection. Next, we will further explore some features of the values() method.

First of all, the values() method returns a Collection. Collection is an interface in Java that represents a collection of objects. In Java, common classes that implement the Collection interface include ArrayList, LinkedList, etc. Therefore, we can use collection classes such as ArrayList and LinkedList to store the results returned by the values() method. The following is a sample code that uses ArrayList to store the value of HashMap:

import java.util.HashMap;
import java.util.ArrayList;

public class HashMapValuesExample {
    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");

        // 使用values()方法获取HashMap中的所有值,并存储到ArrayList中
        ArrayList<String> valuesList = new ArrayList<>(hashMap.values());

        // 遍历输出ArrayList中的所有值
        for (String value : valuesList) {
            System.out.println(value);
        }
    }
}

Running the above code, we can get the same output.

In addition, since the values() method returns a Collection, we can use a series of methods provided by the Collection class to operate on this collection. For example, you can use the contains() method to determine whether a value exists in a HashMap; you can use the isEmpty() method to determine whether a HashMap is empty; you can use the size() method to get the number of values ​​in a HashMap, and so on. The following is a sample code that uses the contains() method to determine whether a value exists:

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

public class HashMapValuesExample {
    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");

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

        // 判断值是否存在
        boolean isContains = values.contains("Apple");
        System.out.println("是否包含Apple:" + isContains);

        isContains = values.contains("Grape");
        System.out.println("是否包含Grape:" + isContains);
    }
}

Run the above code, we can get the output:

是否包含Apple:true
是否包含Grape:false

Through the above example, we have analyzed the HashMap class in detail The usage of values() method and specific code examples are given. By calling the values() method, we can easily get all the values ​​in the HashMap and perform corresponding operations. This is very helpful for us to deal with HashMap data structure in actual development.

The above is the detailed content of Interpretation of Java documentation: Detailed explanation of the usage of the values() method of the HashMap class. 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