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

📅  最后修改于: 2021-09-10 03:09:48             🧑  作者: Mango

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

方法一: values()方法

在Java中HashMap类中的Java .util.HashMap.values()方法被用来创建一个集合出来的图中的值的。它基本上返回 HashMap 中值的集合视图。

句法:

Hash_Map.values()

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

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

例子:

Java
// Java program demonstrating use of values() method
 
// Importing all input output classes
import java.io.*;
// Importing HashMap, Iterator, Map and Stream classes
// from the 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 a Map object
        // Declaring object of String and integer type
        Map map = new HashMap<>();
 
        // Now, adding the elements to the object created
        // Elements here are key- \value pairs
 
        // Custom input objects
        map.put(1, "Geeks");
        map.put(2, "For");
        map.put(3, "Geeks");
 
        // Showcasing different ways to illustrate
        // values() method
 
        // Way 1 - Using iterator
        // Iterating over the object elements of the
        // showcasing the values() method using iterator
 
        // Creating an object of Integer type
        Iterator itr = map.values().iterator();
 
        // Condition check which holds true till
        // there is single elementusing hasNext() method
        while (itr.hasNext()) {
 
            // Travering across the elements elements
            // using next() method
 
            // Printing the elements in the object
            System.out.print(itr.next() + " ");
        }
 
        // New line
        System.out.println();
 
        // Way 2 - Using loops
        // Iterating over the elements using for-each loop
        // to showacase value() method
        for (String key : map.values()) {
 
            // Printing all the element in object
            // key-value pairs
            System.out.println(key);
        }
 
        // New line
        System.out.println();
 
        // Iterating over the values() method by
        // converting the Map to the string
        System.out.println(map.values().toString());
    }
}


Java
// Java program demonstrating use of entrySet() method
 
//  Importing Map,Stream, hashMap and Iterator classes
// from the 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 an object of Map class
        // Declaring object of Integer and String type
        Map map = new HashMap<>();
 
        // Now, adding the elements to the object
        // Here elements are key-value pairs to map
 
        // Custom input elements
        map.put(1, "Geeks");
        map.put(2, "For");
        map.put(3, "Geeks");
 
        // Now, proposing different cases in which we will
        // be iterating over the elements using entrySet()
 
        // Case 1
        // Iterating the key value pairs
        // using for each loop
        for (Map.Entry entry :
             map.entrySet()) {
 
            // Corresponding key
            Integer key = (Integer)entry.getKey();
 
            // Corresponding pair
            String value = entry.getValue();
 
            // Printing all the corresponding key-value
            // pairs
            System.out.println(key + "=" + value);
        }
 
        // Case 2
        // Iterating the key-value pairs
        // using iterator
        Iterator > itr
            = map.entrySet().iterator();
 
        // Condition check using hasNext() method holding
        // true till there is single entry remaining
        while (itr.hasNext()) {
 
            // Go on printing key-value pairs
            System.out.println(itr.next());
        }
 
        // Case 3
        // Iterating and printing the key-value pairs
        // using Stream.of() method
 
        // Printing alongside by using scope resolution
        // operator
        Stream.of(map.entrySet().toArray())
            .forEach(System.out::println);
    }
}


输出
Geeks For Geeks 
Geeks
For
Geeks

[Geeks, For, Geeks]

方法二: entrySet()方法

在Java中.util.HashMap.entrySet()方法用于创建一组出的相同元件的Java的包含在哈希映射。它基本上返回哈希映射的集合视图,或者我们可以创建一个新集合并将映射元素存储到其中。

句法:

hash_map.entrySet()

参数:该方法不带任何参数。

返回值:该方法返回一个与哈希映射具有相同元素的集合。

执行:

例子

Java

// Java program demonstrating use of entrySet() method
 
//  Importing Map,Stream, hashMap and Iterator classes
// from the 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 an object of Map class
        // Declaring object of Integer and String type
        Map map = new HashMap<>();
 
        // Now, adding the elements to the object
        // Here elements are key-value pairs to map
 
        // Custom input elements
        map.put(1, "Geeks");
        map.put(2, "For");
        map.put(3, "Geeks");
 
        // Now, proposing different cases in which we will
        // be iterating over the elements using entrySet()
 
        // Case 1
        // Iterating the key value pairs
        // using for each loop
        for (Map.Entry entry :
             map.entrySet()) {
 
            // Corresponding key
            Integer key = (Integer)entry.getKey();
 
            // Corresponding pair
            String value = entry.getValue();
 
            // Printing all the corresponding key-value
            // pairs
            System.out.println(key + "=" + value);
        }
 
        // Case 2
        // Iterating the key-value pairs
        // using iterator
        Iterator > itr
            = map.entrySet().iterator();
 
        // Condition check using hasNext() method holding
        // true till there is single entry remaining
        while (itr.hasNext()) {
 
            // Go on printing key-value pairs
            System.out.println(itr.next());
        }
 
        // Case 3
        // Iterating and printing the key-value pairs
        // using Stream.of() method
 
        // Printing alongside by using scope resolution
        // operator
        Stream.of(map.entrySet().toArray())
            .forEach(System.out::println);
    }
}
输出
1=Geeks
2=For
3=Geeks
1=Geeks
2=For
3=Geeks
1=Geeks
2=For
3=Geeks

现在让我们看看 values() 方法和 entrySet() 方法的区别

values() Method entrySet() Method
This method returns the collection view of all the values contained in the map. This method returns the Set view of all the mappings present in the map, ie it returns a set of key, value pairs.
If any changes happen to the map, then they can be observed in the collection also, as the method collection is backed up by the map.   If any changes happen to the map, then they can be observed in the set also, as the set is backed up by the map. 
This method is used when we only need to deal with values present in the map.  This method is used when we need to deal with keys as well as values present in the map.