You can use forEach()
when traversing HashMap. forEach()
takes functional interface as an argument as follows.
public void forEach(BiConsumer<? super K, ? super V> action)
BiConsumer
is a functional interface that takes two arguments and does not return.
So you can pass the argument as a lambda expression. The first argument is key, and the second argument is value.
The following is an example of printing key and value with forEach. If you look at the lambda expression, key and value are passed as arguments, and there is no return value.
Map<String, Integer> fruits = new HashMap<>();
fruits.put("apple", 1);
fruits.put("banana", 2);
fruits.put("kiwi", 3);
fruits.forEach((key, value)
-> System.out.println("key: " + key + ", value: " + value));
// key: banana, value: 2
// key: apple, value: 1
// key: kiwi, value: 3
Because it supports Java8`s functional interface, you need to know about lambda expression, Functional Interface.
EntrySet.forEach()
HashMaps EntrySet class also has a
forEach()method. If you look at the argument, it receives a functional interface called
Consumer` as an argument.
final class EntrySet extends AbstractSet<Map.Entry<K,V>> {
...
public final void forEach(Consumer<? super Map.Entry<K,V>> action)
...
}
Consumer
is a functional interface that takes 1 argument and does not return.
The following is an example that outputs the same result as above. If you look at the lambda expression, it receives one Entry object as an argument.
Map<String, Integer> fruits = new HashMap<>();
fruits.put("apple", 1);
fruits.put("banana", 2);
fruits.put("kiwi", 3);
fruits.entrySet().forEach((entry) -> System.out.println(
"key: " + entry.getKey() + ", value: " + entry.getValue()));
// key: banana, value: 2
// key: apple, value: 1
// key: kiwi, value: 3
KeySet.forEach(), Values.forEach()
HashMaps KeySet and Values classes also have a
forEach()method. It takes a functional interface called
Consumer` as an argument.
public final void forEach(Consumer<? super V> action)
You can use it like this:
Map<String, Integer> fruits = new HashMap<>();
fruits.put("apple", 1);
fruits.put("banana", 2);
fruits.put("kiwi", 3);
fruits.keySet().forEach((key) -> System.out.println("key: " + key));
fruits.values().forEach((value) -> System.out.println("value: " + value));
// key: banana
// key: apple
// key: kiwi
// value: 2
// value: 1
// value: 3
Reference
Related Posts
- Java - Remove items from List while iterating
- Java - How to find key by value in HashMap
- Java - Update the value of a key in HashMap
- Java - How to put quotes in a string
- Java - How to put a comma (,) after every 3 digits
- BiConsumer example in Java 8
- Java 8 - Consumer example
- Java 8 - BinaryOperator example
- Java 8 - BiPredicate Example
- Java 8 - Predicate example
- Java 8 - Convert Stream to List
- Java 8 - BiFunction example
- Java 8 - Function example
- Java - Convert List to Map
- Exception testing in JUnit
- Hamcrest Collections Matcher
- Hamcrest equalTo () Matcher
- AAA pattern of unit test (Arrange/Act/Assert)
- Hamcrest Text Matcher
- Hamcrest Custom Matcher
- Why Junit uses Hamcrest
- Java - ForkJoinPool
- Java - How to use Futures
- Java - Simple HashTable implementation
- Java - Create a file in a specific path
- Java - Mockito의 @Mock, @Spy, @Captor, @InjectMocks
- Java - How to write test code using Mockito
- Java - Synchronized block
- Java - How to decompile a ".class" file into a Java file (jd-cli decompiler)
- Java - How to generate a random number
- Java - Calculate powers, Math.pow()
- Java - Calculate the square root, Math.sqrt()
- Java - How to compare String (==, equals, compare)
- Java - Calculate String Length
- Java - case conversion & comparison insensitive (toUpperCase, toLowerCase, equalsIgnoreCase)