In this article, I will introduce some ways to get the keys and values stored in Map in the iterative process regardless of implementation. The target readers of this article are mainly beginners to intermediates.
I think that there are many situations where you use list data structures such as ʻArrayList and
LinkedList` as a way to manage a large amount of information when programming, but by mastering the data structure of Map, you can easily solve more complicated problems. I would like you to learn how to use it because you will be able to handle it.
There are mainly the following methods to get the key and value from the collection of Map structure in the iterative process.
--Use ʻEntrySet --Use the
forEach (BiConsumer <T, U>)` method (JDK 1.8 or later)
It may not be familiar to you now, but you can easily get the keys and values stored in the Map in an iterative process by using ʻEntrySet`.
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public final class TestEntrySet {
public static void main(String[] args) {
final Map<String, String> testMap = new HashMap<>(3);
testEntrySet.put("test1", "value1");
testEntrySet.put("test2", "value2");
testEntrySet.put("test3", "value3");
final Set<Entry<String, String>> testEntrySet = testMap.entrySet();
for (Map.Entry<String, String> entry : testEntrySet) {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
}
}
In the above sample code, the map for the test implemented by HashMap
is declared, the value for the test is set, and then the ʻentrySet ()` method is called.
You can get ʻEntrySet of type
Set <Entry <String, String >> by calling the ʻentrySet ()
method. Use the ʻEntrySet obtained here in the extended for statement. Of course, the sample code above gets ʻEntrySet
before iterating, but you can also get ʻEntrySet` when using the extension for as shown below.
//Get EntrySet when defining extended for statement
for (Map.Entry<String, String> entry : testMap.entrySet()) {
}
You can get the key stored in the Map by using the getKey ()
method in the iterative process, and the value stored in the Map by using the getValue ()
method.
Therefore, the execution result of the above sample code is output as follows.
test1
value1
test2
value2
test3
value3
Perhaps the method using this forEach ()
method is now more mainstream than the iterative process using ʻEntrySetintroduced earlier. This
forEach () method is a relatively new method implemented in JDK1.8 with the addition of functional interfaces, and is now more intuitive than ʻEntrySet
.
import java.util.HashMap;
import java.util.Map;
public final class TestForEach {
public static void main(String[] args) {
final Map<String, String> testMap = new HashMap<>(3);
testEntrySet.put("test1", "value1");
testEntrySet.put("test2", "value2");
testEntrySet.put("test3", "value3");
//BiConsumer as an argument<T, U>Pass a lambda expression that implements
testMap.forEach((key, value) -> {
System.out.println(key);
System.out.println(value);
});
}
}
At first glance, you'll find that it's more concise than the ʻEntrySet` method.
The point that seems difficult is the argument, but the argument of the forEach ()
method is passed a lambda expression that implements the functional interface BiConsumer <T, U>
. I will omit the lambda expression and functional interface because they deviate from the purpose of this article, but when using the forEach ()
method in Map, it is okay if you remember how to write the above sample code.
The variable name specified in the lambda expression can be changed arbitrarily. In the case of the above sample code, the Map key is stored in key
and the Map value is stored in value
.
Therefore, the execution result of the above sample code will be as follows as when using ʻEntrySet`.
test1
value1
test2
value2
test3
value3
Due to the characteristics of the functional interface BiConsumer <T, U>
, the return value cannot be returned in the iterative process using the forEach ()
method. Some functional interfaces return a value, but the forEach ()
method just repeats the process for the size of the Map.
For example, if you want to return false
as a boolean value when an error is detected in the iterative process of Map, we recommend using ʻEntrySet instead of
forEach () `.
Recommended Posts