iteration
Everyone must have used Maps and Lists a lot while developing in Java. In my case, List loop is easy to implement, but Map always checks other code and code. I don`t use it often, so I forget it every time. So, I took this opportunity to summarize it briefly.
Map의 iteration
There are two ways to implement the Map loop, a general method and a Java8 Lambda expression. The codes below all output the same content.
// Loop a Map
Map<String, String> cities = new HashMap<>();
cities.put("Tokyo", "Japan");
cities.put("Seoul", "Korea");
cities.put("Beijing", "China");
cities.put("Paris", "France");
cities.put("Washington", "USA");
cities.put("Brazilia", "Brazil");
// Normal ways
//1)
for (Map.Entry<String, String> entry : cities.entrySet()) {
System.out.println("Cities: " + entry.getKey() + ", " + entry.getValue());
}
//2)
for (String key : cities.keySet()) {
System.out.println("Cities: " + key + ", " + cities.get(key));
}
// Java8 forEach, Lambda
// 1)
cities.forEach((k, v) -> System.out.println("Cities: " + k + ", " + v));
// 2)
cities.forEach((k, v) -> {
System.out.println("Cities: " + k + ", " + v);
});
result
Cities: Beijing, China
Cities: Tokyo, Japan
Cities: Seoul, Korea
Cities: Brazilia, Brazil
Cities: Paris, France
Cities: Washington, USA
...
List의 iteration
There are two ways to implement a list loop, the general method as above and the use of Lambda. The codes below all output the same content.
// Loop a List
List<String> fruits = new ArrayList<>();
fruits.add("banana");
fruits.add("apple");
fruits.add("peach");
fruits.add("lemon");
fruits.add("mango");
// Normal way
// 1)
for (String item : fruits) {
System.out.println("Fruits: " + item);
}
// Java8 forEach, Lambda, Stream
// 1)
fruits.forEach(item -> System.out.println("Fruits: " + item));
// 2)
fruits.forEach(item -> {
System.out.println("Fruits: " + item);
});
// 3)
fruits.stream()
.forEach(item -> System.out.println("Fruits: " + item));
result
Fruits: banana
Fruits: apple
Fruits: peach
Fruits: lemon
Fruits: mango
...
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)