- Selenium WebDriver Quick Start Guide
- Pinakin Chaubal
- 78字
- 2021-06-24 18:26:25
The forEach method for a collection
Starting with Java 8, we can invoke the forEach method on a collection and iterate through the contents of the collection. Let's compare the 7 and 8 versions of iterating over an array list of strings.
The following code, which is from Jave 7, fetches individual fruit names from the fruits list and prints it to the console:
List<String> fruits = Arrays.asList("Apples", "Oranges", "Bananas",
"Pears");
for (int i = 0; i < fruits.size(); i++) {
System.out.println(fruits.get(i));
}
A second alternative that you can use is as follows:
for (String fruit : fruits ){
System.out.println(fruit);
}
The example shown here does the same thing in Java 8 using lambda expressions:
fruits.forEach(i -> System.out.println(i));
推薦閱讀
- Kali Linux滲透測試全流程詳解
- Linux操作系統(tǒng)應(yīng)用編程
- 計算機應(yīng)用基礎(chǔ)(Windows 7+Office 2016)
- 統(tǒng)信UOS應(yīng)用開發(fā)進階教程
- Office 365 User Guide
- Agile IT Security Implementation Methodology
- OpenHarmony開發(fā)與實踐:基于紅莓RK2206開發(fā)板
- Windows網(wǎng)絡(luò)編程(第2版)
- Java EE 8 High Performance
- 電腦辦公(Windows10+Office2016)從新手到高手
- Serverless Architectures with Kubernetes
- 程序員必讀經(jīng)典(算法基礎(chǔ)+計算機系統(tǒng))
- Instant Getting Started with VMware Fusion
- STM32庫開發(fā)實戰(zhàn)指南
- 從零開始學(xué)Windows 7