Java Map中keySet()与value()方法的区别

Java Map中keySet()与value()方法的区别

Map接口存在于Java.util包中,它主要提供了三个方法KeySet()、entrySet()和values()。这些方法分别用于检索Map的键、Map的键值对以及Map的值。由于这些方法是Map接口的一部分,所以我们可以在所有实现Map接口的类中使用这些方法,比如TreeMap、HashMap和LinkedHashMap。

为了弄清它们之间的差异,让我们首先从概念上逐一分析,然后再从实施上弄清它们之间的主要差异。

方法1: keySet()方法

该方法用于返回该Map中包含的键的Set视图。这个集合是由Map支持的,所以Map的变化会反映在这个集合中,反之亦然。

语法:

Set keySet()
Java

参数: 这个方法没有参数。

返回: 该方法返回一个包含指定Map的键的集合。

实现

示例

// Java program demonstrating use of keySet() method
 
// Importing HashMap, Iterator, MAp and Stream classes
// from java.util package
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.stream.Stream;
 
// Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating object of Map Class
        // Declaring object of Interfere and string type
        Map<Integer, String> map = new HashMap<>();
 
        // Adding the elements to the objects
        // Elements here are key-value pairs in the map
 
        // Custom input entries
        map.put(1, "Geeks");
        map.put(2, "For");
        map.put(3, "Geeks");
 
        // Now, different ways of iteration are illustrated
        // to showcase keySet() method
 
        // Way 1
        // Iterating the keySet() using iterator
 
        // Creating an object of Integer type
        Iterator<Integer> itr = map.keySet().iterator();
 
        // Condition check where hasNext() method holds true
        // till there is single element remaining in the List
        while (itr.hasNext()) {
 
            // Print all the elements(key-value pairs)
            System.out.print(itr.next() + " ");
        }
 
        // New line
        System.out.println();
 
        // Way 2
        // Iterating the keySet()
        // using for loop
        for (Integer key : map.keySet()) {
 
            // Print all the key-value pairs
            System.out.print(key + " ");
        }
 
        // New line
        System.out.println();
 
        // Way 3
        // Iterating over the keySet() by
        // converting the map to the string
        // using the toString() method
        System.out.println(map.keySet().toString());
    }
}
Java

输出

1 2 3 
1 2 3 
[1, 2, 3]
Java

方法2: values()方法

Java中HashMap类的java.util.HashMap.values()方法是用来从Map的值中创建一个集合。它基本上返回HashMap中数值的一个集合视图。

语法:

Hash_Map.values()
Java

参数: 该方法不接受任何参数。

返回值: 该方法用于返回一个包含Map所有值的集合视图。

实现情况: 以下是使用values()方法的Java程序

示例

// Java program demonstrating use of values() method
 
// Importing several classes from
// java.util package
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.stream.Stream;
 
// Class
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Making map of Integer keys and String values
        Map<Integer, String> map = new HashMap<>();
 
        // Adding the elements to the above object
        // Declaring object of Integer and String type
 
        // Elements here are key-value pairs
        // Custom input entries
        map.put(1, "Geeks");
        map.put(2, "For");
        map.put(3, "Geeks");
 
        // values() method implemented by
        // demonstrating different ways of traversal
 
        // Way 1
        // Iterating the values() method
        // using iterator
        Iterator itr = map.values().iterator();
 
        // Condition check using hasNet() method which
        // holds true till there is single element remaining
        while (itr.hasNext()) {
            System.out.print(itr.next() + " ");
        }
        System.out.println();
 
        // Way 2
        // Iterating the values() method
        // using for each loop
        for (String key : map.values()) {
            System.out.print(key + " ");
        }
        System.out.println();
 
        // Way 3
        // iterating over the values() method
        // by converting the map to the string
        // using the toString() method
        System.out.println(map.values().toString());
    }
}
Java

输出

Geeks For Geeks 
Geeks For Geeks 
[Geeks, For, Geeks]
Java

最后,让我们看看keySet()方法和values()方法之间的区别如下。

keySet()方法 values()方法
该方法返回Map中存在的所有键的Set视图,即它返回一个键的集合。 该方法返回Map中包含的所有值的集合视图。
如果Map发生任何变化,那么它们也可以在集上观察到,因为集是由Map支持的。 如果Map发生任何变化,那么它们也可以在集合中观察到,因为集合是由Map支持的。
只有当我们需要处理Map中存在的所有键时,才会使用这种方法。 当我们只需要处理Map中存在的所有数值时,就可以使用这种方法。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册