Java 什么是 Map.Entry<K,V> 接口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18863910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:44:04  来源:igfitidea点击:

What is Map.Entry<K,V> interface?

javamap

提问by saplingPro

I came across the following code :

我遇到了以下代码:

for(Map.Entry<Integer,VmAllocation> entry : allMap.entrySet()) {
       // ...
}

What does Map.Entry<K,V>mean ? What is the entryobject ?

什么Map.Entry<K,V>意思?entry对象是什么?

I read that the method entrySetreturns a set view of the map. But I do not understand this initialization in for-eachloop.

我读到该方法entrySet返回地图的集合视图。但我不明白这个for-each循环初始化。

采纳答案by Ted Hopp

Map.Entryis a key/value pair that forms one element of a Map. See the docsfor more details.

Map.Entry是构成 a 的一个元素的键/值对Map。有关更多详细信息,请参阅文档

You typically would use this with:

您通常会将其用于:

Map<A, B> map = . . .;
for (Map.Entry<A, B> entry : map.entrySet()) {
    A key = entry.getKey();
    B value = entry.getValue();
}

If you need to process each key/value pair, this is more efficient than iterating over the key set and calling get(key)to get each value.

如果您需要处理每个键/值对,这比迭代键集并调用get(key)以获取每个值更有效。

回答by Nicole

Go to the docs: Map.Entry

转到文档:Map.Entry

Map.Entryis an object that represents one entry in a map. (A standard map has 1 value for every 1 key.) So, this code will iterator over all key-value pairs.

Map.Entry是一个对象,表示地图中的一个条目。(标准映射每 1 个键有 1 个值。)因此,此代码将迭代所有键值对。

You might print them out:

你可以把它们打印出来:

for(Map.Entry<Integer,VmAllocation> entry : allMap.entrySet()) {
       System.out.print("Key: " + entry.getKey());
       System.out.println(" / Value: " + entry.getValue());
}

回答by Aurand

An entry is a key/value pair. In this case, it is a mapping of Integersto VmAllocationobjects.

一个条目是一个键/值对。在这种情况下,它是IntegersVmAllocation对象的映射。

As the javadocsays

正如javadoc所说

A map entry (key-value pair). The Map.entrySet method returns a collection-view of the map, whose elements are of this class. The only way to obtain a reference to a map entry is from the iterator of this collection-view. These Map.Entry objects are valid only for the duration of the iteration; more formally, the behavior of a map entry is undefined if the backing map has been modified after the entry was returned by the iterator, except through the setValue operation on the map entry.

一个映射条目(键值对)。Map.entrySet 方法返回地图的集合视图,其元素属于此类。获取映射条目引用的唯一方法是从此集合视图的迭代器中获取。这些 Map.Entry 对象仅在迭代期间有效;更正式地说,如果在迭代器返回条目后修改了后备映射,则映射条目的行为是未定义的,除非通过对映射条目的 setValue 操作。

回答by TheKojuEffect

You can learn about Map.Entry Docs

你可以了解Map.Entry Docs

A map entry (key-value pair). The Map.entrySet method returns a collection-view of the map, whose elements are of this class. The only way to obtain a reference to a map entry is from the iterator of this collection-view. These Map.Entry objects are valid only for the duration of the iteration; more formally, the behavior of a map entry is undefined if the backing map has been modified after the entry was returned by the iterator, except through the setValue operation on the map entry.

一个映射条目(键值对)。Map.entrySet 方法返回地图的集合视图,其元素属于此类。获取映射条目引用的唯一方法是从此集合视图的迭代器中获取。这些 Map.Entry 对象仅在迭代期间有效;更正式地说,如果在迭代器返回条目后修改了后备映射,则映射条目的行为是未定义的,除非通过对映射条目的 setValue 操作。

Check For Each Loop Docs

检查每个循环文档

for(Map.Entry<Integer,VmAllocation> entry : allMap.entrySet()) 

entryis a variable of type Map.Entrywhich is instantiated with the Entrytype data in allMapwith each iteration.

entry是一个类型变量,Map.EntryEntryallMap每次迭代中使用类型数据进行实例化。