Map 接口存在于Java.util 包中,主要提供 KeySet()、entrySet() 和 values() 三个方法。这些方法分别用于检索地图的键、地图的键值对和地图的值。由于这些方法是 Map 接口的一部分,所以我们可以将这些方法用于实现 Map 接口的所有类,如 TreeMap、HashMap 和 LinkedHashMap。
为了找出差异,让我们首先从概念上逐一分析它们,然后通过实现来找出它们之间的主要差异。
方法一: keySet()方法
此方法用于返回此映射中包含的键的 Set 视图。该集合由地图支持,因此对地图的更改会反映在该集合中,反之亦然。
句法:
Set keySet()
参数:此方法没有参数。
返回:此方法返回一个包含指定映射键的集合。
执行:
例子
Java
// 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 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 itr = map.keySet().iterator();
// Condition check where hasNext() method holds true
// till there is single element remaining in th 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
// 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 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());
}
}
输出
1 2 3
1 2 3
[1, 2, 3]
方法 2: values() 方法
在Java中HashMap类中的Java .util.HashMap.values()方法被用来创建一个集合出来的图中的值的。它基本上返回 HashMap 中值的集合视图。
句法:
Hash_Map.values()
参数:该方法不接受任何参数。
返回值:该方法用于返回包含地图所有值的集合视图。
实现:下面是使用values()方法的Java程序
例子
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 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());
}
}
输出
Geeks For Geeks
Geeks For Geeks
[Geeks, For, Geeks]
现在终于到结论了,让我们看看 keySet() 方法和 values() 方法的区别如下:
keySet() method | values() method |
---|---|
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 collection view of all the values contained in the map. |
If any changes happen to the map, then they can be observed in the set also, as a set is backed up by the map. | If any changes happen to the map, then they can be observed in the collection also, as the collection is backed up by the map. |
This method is used only when we need to deal with all the keys present in the map. | This method is used when we only need to deal with all the values present in the map. |