Table of Contents
Phương thức forEach() là một tính năng mới của java 8. Nó là một phương thức mặc định (default method) được định nghĩa trong interface Iterable và Stream. Các lớp Collection extends từ interface Iterable có thể sử dụng vòng lặp forEach() để duyệt các phần tử.
Định nghĩa của phương thức forEach() trong Interface Iterable:
1. Ví dụ sử dụng forEach() với Map
package com.maixuanviet.forEach;
import java.util.HashMap;
import java.util.Map;
public class ForEachMapExample {
public static void main(String[] args) {
Map<Integer, String> hmap = new HashMap<Integer, String>();
hmap.put(1, "Java");
hmap.put(2, "Javascript");
hmap.put(3, "PHP");
hmap.put(4, "C#");
hmap.put(5, "C++");
// forEach to iterate and display each key and value pair of HashMap
hmap.forEach((key, value) -> System.out.println(key + " - " + value));
}
}
Output của chương trình trên như sau:
1 - Java 2 - Javascript 3 - PHP 4 - C# 5 - C++
2. Ví dụ sử dụng forEach() với List
package com.maixuanviet.forEach;
import java.util.Arrays;
import java.util.List;
public interface ForEachExample {
public static void main(String[] args) {
List<String> languages = Arrays.asList("Java", "C#", "C++", "PHP", "Javascript");
System.out.println("Iterating by passing lambda expression: ");
languages.forEach(lang -> System.out.println(lang));
System.out.println("Iterating by passing method reference: ");
languages.forEach(System.out::println);
}
}
Output của chương trình trên như sau:
Iterating by passing lambda expression: Java C# C++ PHP Javascript Iterating by passing method reference: Java C# C++ PHP Javascript
3. Ví dụ sử dụng forEachOrdered()
Phương thức forEachOrdered() được sử dụng để duyệt các phần tử theo thứ tự được quy định bởi Stream.
package com.maixuanviet.forEach;
import java.util.Arrays;
import java.util.List;
public class ForEachOrderedExample {
public static void main(String[] args) {
List<String> languages = Arrays.asList("Java", "C#", "C++", "PHP", "Javascript");
System.out.println("Iterating by passing lambda expression: ");
languages.stream().forEachOrdered(lang -> System.out.println(lang));
System.out.println("Iterating by passing method reference: ");
languages.stream().forEachOrdered(System.out::println);
}
}
Output của chương trình trên như sau:
Iterating by passing lambda expression: Java C# C++ PHP Javascript Iterating by passing method reference: Java C# C++ PHP Javascript
Related posts:
Hướng dẫn Java Design Pattern – Factory Method
Java Program to Implement Control Table
An Intro to Spring Cloud Zookeeper
Java Program to Implement Cartesian Tree
Custom Thread Pools In Java 8 Parallel Streams
Life Cycle of a Thread in Java
Fixing 401s with CORS Preflights and Spring Security
Apache Tiles Integration with Spring MVC
Spring Boot - Scheduling
Spring Boot - CORS Support
Java Program to Implement Red Black Tree
Java Program to Find the Nearest Neighbor Using K-D Tree Search
Introduction to the Java ArrayDeque
RegEx for matching Date Pattern in Java
Comparing Two HashMaps in Java
Bootstrapping Hibernate 5 with Spring
Spring Security Registration – Resend Verification Email
Spring Cloud – Adding Angular
Spring MVC + Thymeleaf 3.0: New Features
Java Program to Generate a Graph for a Given Fixed Degree Sequence
Converting Between a List and a Set in Java
Java Program to Implement Selection Sort
HttpClient with SSL
Check If Two Lists are Equal in Java
Guide to @ConfigurationProperties in Spring Boot
Java Program to Implement Flood Fill Algorithm
Java Program to Find Nearest Neighbor for Static Data Set
Immutable Map Implementations in Java
Retrieve User Information in Spring Security
Java Program to Solve any Linear Equations
Concurrent Test Execution in Spring 5
Jackson – Unmarshall to Collection/Array