📜  Java Map中keySet()和entrySet()方法的区别

📅  最后修改于: 2021-09-16 10:23:13             🧑  作者: Mango

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

keySet() 方法:

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

句法:

hash_map.keySet()

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

返回值:该方法返回一个具有哈希映射键的集合。

Java
// Java program demonstrating use of keySet()
  
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.stream.Stream;
  
class GFG {
    public static void main(String[] args)
    {
        // making map of Integer keys and String values
        Map map = new HashMap<>();
        
        // adding the key-value pairs to map
        map.put(1, "Geeks");
        map.put(2, "For");
        map.put(3, "Geeks");
  
        // below are the different and simple ways out of
        // many  to iterate over the keySet()
  
        // iterating the keySet() using iterator
        Iterator itr = map.keySet().iterator();
        
        while (itr.hasNext())
        {
            System.out.print(itr.next() + " ");
        }
        System.out.println();
  
        // iterating the keySet() using for loop
        for (Integer key : map.keySet()) {
            System.out.print(key + " ");
        }
  
        System.out.println();
        
        // iterating over the keySet() by converting the map
        // to the string
        System.out.println(map.keySet().toString());
    }
}


Java
// Java program demonstrating use of  entrySet()
  
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.stream.Stream;
  
class GFG {
    public static void main(String[] args)
    {
        // making map of Integer keys and String values
        Map map = new HashMap<>();
        
        // adding the key-value pairs to map
        map.put(1, "Geeks");
        map.put(2, "For");
        map.put(3, "Geeks");
  
        // below are the different and simple ways out of
        // many  to iterate over the entrySet()
  
        // iterating the key value pair using for each loop
        for (Map.Entry entry :map.entrySet()) 
        {
            Integer key = (Integer)entry.getKey();
            String value = entry.getValue();
  
            System.out.println(key + "=" + value);
        }
        
        // iterating the key-value pairs using iterator
        Iterator > itr = map.entrySet().iterator();
        
        while (itr.hasNext()) {
            System.out.println(itr.next());
        }
        
        // iterating the key-value pairs using Stream.of()
        // method
        Stream.of(map.entrySet().toArray())
            .forEach(System.out::println);
    }
}


输出
1 2 3 
1 2 3 
[1, 2, 3]

entrySet() 方法

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

句法:

hash_map.entrySet()

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

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

Java

// Java program demonstrating use of  entrySet()
  
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.stream.Stream;
  
class GFG {
    public static void main(String[] args)
    {
        // making map of Integer keys and String values
        Map map = new HashMap<>();
        
        // adding the key-value pairs to map
        map.put(1, "Geeks");
        map.put(2, "For");
        map.put(3, "Geeks");
  
        // below are the different and simple ways out of
        // many  to iterate over the entrySet()
  
        // iterating the key value pair using for each loop
        for (Map.Entry entry :map.entrySet()) 
        {
            Integer key = (Integer)entry.getKey();
            String value = entry.getValue();
  
            System.out.println(key + "=" + value);
        }
        
        // iterating the key-value pairs using iterator
        Iterator > itr = map.entrySet().iterator();
        
        while (itr.hasNext()) {
            System.out.println(itr.next());
        }
        
        // iterating the key-value pairs using Stream.of()
        // method
        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
keySet() entrySet()
This method returns the Set view of all the keys present in the map, ie it returns a set of keys. 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 set also,as set is backed up by the map.  For entrySet() method also, If any changes happen to the map, then they can be observed in the set also,as set is backed up by the map. 
If iterating through all the pairs of maps using keySet(), then the performance of keySet() is poorer as compared to entrySet(), as for each key, we have to access its corresponding value by using get() function. When Iterating through all the pairs of the map using entrySet(), then the performance of entrySet() is much better as compared to keySet().