Home >Java >javaTutorial >Usage of entry in java
The Entry interface in Java is part of the collection framework and represents the key-value pairs in the map. It allows access to keys and values, and modification of values. Mainly used to process collections based on key-value pairs, such as Map, providing benefits such as fast access, modified values, and customized comparisons. Its methods include getKey(), getValue(), and setValue(V).
Entry interface in Java
Entry interface is part of the Java collection framework, which represents a key in a map value pair. It provides access to keys and values and allows modification of their values.
Purpose
The Entry interface is usually used to handle collections based on key-value pairs, such as Map and Dictionary. It provides the following benefits:
Methods
Entry interface provides the following main methods:
Example
The following example shows how to use the Entry interface:
<code class="java">import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.Map.Entry; public class EntryExample { public static void main(String[] args) { Map<String, Integer> map = new HashMap<>(); map.put("John", 25); map.put("Mary", 30); map.put("Bob", 35); Set<Entry<String, Integer>> entries = map.entrySet(); for (Entry<String, Integer> entry : entries) { System.out.println(entry.getKey() + ": " + entry.getValue()); } // 修改值 map.get("John").setValue(27); System.out.println(map.get("John")); } }</code>
Output:
<code>John: 25 Mary: 30 Bob: 35 John: 27</code>
As you like See, the sample code uses the Entry interface to obtain a key-value pair and modify one of the values.
The above is the detailed content of Usage of entry in java. For more information, please follow other related articles on the PHP Chinese website!