Java中的Map.Entry接口示例
Java中的 Map.Entry 接口提供了某些方法来访问 Map 中的条目。通过访问 Map 的条目,我们可以轻松地操作它们。 Map.Entry 是一个泛型,在Java.util 包中定义。
宣言 :
Interface Map.Entry
k -> Key
V -> Value
方法:
- equals (Object o) – 比较对象(调用对象)与 Object o 是否相等。
句法 :boolean equals(Object o) Parameters : o -> Object to which we want to compare Returns: true: if both objects are equals false: otherwise
- K getKey() – 返回对应映射条目的键。
句法 :K getKey() Parameters : ------------- Returns: K -> Returns the corresponding Key of a entry on which it is invoked
例外 -
- 从地图中删除条目时抛出 IllegalSateException 。
- V getValue() – 返回对应映射条目的值。
句法 :V getValue() Parameters : ------------- Returns: V -> Returns the corresponding value of a entry on which it is invoked
- int hashcode() – 返回对应映射条目的哈希码。
句法 :int hashcode() Parameters : ------------- Returns: int -> Returns the hash-code of entry on which it is invoked
- V setValue(V v) - 使用指定值 v 设置地图的值
V setValue(V v) Parameters : v -> Value which was earlier stored in the entry on which it is invoked Returns: int -> Returns the hash-code of a entry on which it is invoked
例外 :
- 如果值“v”的类不是映射的正确类型,则抛出ClassCastException 。
- 如果 'null' 存储在 'v' 中,并且 map 不支持 'null',则会引发NullPointerException 。
- 如果我们无法操作地图或地图不支持放置操作,则会引发UnsupportedOperationException 。
- 抛出 IllegalArgumetException如果参数有问题,即 v
- 从地图中删除条目时抛出 IllegalStateException
Set
注意:这不是 Map.entry 接口的方法,但在这里讨论,因为此方法在使用 Map.Entry 接口时很有用。
Set entrySet()
Parameters :
---------------
Returns:
Set ->: Returns a Set containing the Map.Entry values
下面的程序演示了 Map.Entry 的工作:
// Java Program to demonstrate the
// methods of Map.Entry
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
public class GFG
{
public static void main(String[] args)
{
// Create a LinkedHashMap
LinkedHashMap m =
new LinkedHashMap();
m.put("1 - Bedroom" , 25000);
m.put("2 - Bedroom" , 50000);
m.put("3 - Bedroom" , 75000);
m.put("1 - Bedroom - hall", 65000);
m.put("2 - Bedroom - hall", 85000);
m.put("3 - Bedroom - hall", 105000);
// Using entrySet() to get the entry's of the map
Set> s = m.entrySet();
for (Map.Entry it: s)
{
// Using the getKey to get key of the it element
// Using the getValue to get value of the it element
System.out.println("Before change of value = " +
it.getKey() + " " + it.getValue());
// Changing the value of 1 - Bedroom.
double getRandom = Math.random() * 100000;
int getRoundoff = (int) Math.round(getRandom);
// Using setValue to change the value of the
// map element
it.setValue(getRoundoff);
System.out.println("After change of value = " +
it.getKey() + " " + it.getValue());
}
}
}
输出:
Before change of value = 1 - Bedroom 25000
After change of value = 1 - Bedroom 59475
Before change of value = 2 - Bedroom 50000
After change of value = 2 - Bedroom 51650
Before change of value = 3 - Bedroom 75000
After change of value = 3 - Bedroom 95200
Before change of value = 1 - Bedroom - hall 65000
After change of value = 1 - Bedroom - hall 74112
Before change of value = 2 - Bedroom - hall 85000
After change of value = 2 - Bedroom - hall 41490
Before change of value = 3 - Bedroom - hall 105000
After change of value = 3 - Bedroom - hall 10902