Java Map中value()与 entrySet()方法的区别
地图接口存在于Java.util包中,它主要提供了三个方法KeySet()、entrySet()和values()。这些方法分别用于检索地图的键、地图的键值对以及地图的值。由于这些方法是地图接口的一部分,所以我们可以在所有实现地图接口的类中使用这些方法,比如TreeMap、HashMap和LinkedHashMap。
方法1: 价值()_方法
Java中HashMap类的java.util.HashMap.values()方法用于从地图的值中创建一个集合。它基本上返回HashMap中数值的一个集合视图。
语法:
Hash_Map.values()
参数: 该方法不接受任何参数。
返回值: 该方法用于返回一个包含地图所有值的集合视图。
示例:
// 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<Integer, String> 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<String> itr = map.values().iterator();
// Condition check which holds true till
// there is single elementusing hasNext() method
while (itr.hasNext()) {
// Traversing across the 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());
}
}
输出
Geeks For Geeks
Geeks
For
Geeks
[Geeks, For, Geeks]
方法2: entrySet()方法
Java中的java.util.HashMap.entrySet()方法是用来从哈希图中包含的相同元素中创建一个集合。它基本上返回散列图的集合视图,或者我们可以创建一个新的集合,并将地图元素存储到其中。
语法:
hash_map.entrySet()
参数: 该方法不接受任何参数。
返回值: 该方法返回一个具有与哈希图相同元素的集合。
实现。
示例
// 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<Integer, String> 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<Integer, String> 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<Map.Entry<Integer, String> > 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()方法 | entrySet()方法 |
|---|---|
| 这个m方法返回地图中包含的所有值的集合视图。 | 该方法返回地图中所有映射的Set视图,即返回一组键、值对。 |
| 如果地图发生任何变化,那么它们也可以在集合中观察到,因为方法集合是由地图支持的。 | 如果地图发生了任何变化,那么它们也可以在集上观察到,因为集是由地图支持的。 |
| 当我们只需要处理地图中存在的值时,就会使用这种方法。 | 当我们需要处理地图中存在的键和值时,就会使用这种方法。 |
极客教程