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:
Xử lý ngoại lệ trong Java (Exception Handling)
Spring Boot - Cloud Configuration Client
Java Program to implement Array Deque
Logout in an OAuth Secured Application
Java Program to Implement Leftist Heap
Instance Profile Credentials using Spring Cloud
Comparing Arrays in Java
Hướng dẫn Java Design Pattern – Singleton
Receive email using POP3
Java Program to Describe the Representation of Graph using Adjacency Matrix
Tạo ứng dụng Java RESTful Client với thư viện OkHttp
Spring Boot - Enabling Swagger2
Java Program to Implement Adjacency Matrix
Spring Boot: Customize the Jackson ObjectMapper
Java Program to Compute Discrete Fourier Transform Using the Fast Fourier Transform Approach
Java Program for Topological Sorting in Graphs
Exploring the Spring 5 WebFlux URL Matching
Spring Data MongoDB – Indexes, Annotations and Converters
Marker Interface trong Java
Java Program to Implement Binary Heap
ETL with Spring Cloud Data Flow
Intro to Spring Boot Starters
Guide to the Java ArrayList
LinkedHashSet trong java
Java Program to Represent Graph Using 2D Arrays
Extra Login Fields with Spring Security
Java Program to Perform Sorting Using B-Tree
Spring Boot - Scheduling
How to Set TLS Version in Apache HttpClient
Guide to Java 8’s Collectors
Java Program to Find Hamiltonian Cycle in an UnWeighted Graph
Java Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree