Here are a few ways
aList.stream().map(A::getB).forEach(bList::add);
// or
aList.forEach(a -> bList.add(a.getB()));
or you can even create bList() on the fly:
List<B> bList = aList.stream().map(A::getB).collect(Collectors.toList());
Answer from Bohemian on Stack OverflowGeeksforGeeks
geeksforgeeks.org โบ java โบ arraylist-foreach-method-in-java
ArrayList forEach() Method in Java - GeeksforGeeks
July 20, 2026 - Introduced in Java 8, it accepts a Consumer functional interface, making it easy to use lambda expressions and method references for concise and readable code. Simplifies iteration without using explicit loops. Introduced in Java 8 as part of the Collections Framework enhancements. ... import java.util.ArrayList; public class GFG { public static void main(String[] args) { // Create an ArrayList of Strings ArrayList<String> s = new ArrayList<>(); s.add("Cherry"); s.add("Blueberry"); s.add("Strawberry"); // Use forEach() to print each fruit s.forEach(System.out::println); } }
Iterate list in java in 6 different ways | Loops in java | Loop ...
07:48
Iterating Over an ArrayList - Different Ways to Iterate an ArrayList ...
14:52
Java 8 forEach Method Tutorial | Iterate over List, Set, Stream ...
Java 8 forEach Method: Java Programming Tutorial | #java8 ...
Java 8 forEach() Tutorial: Learn How to Iterate Collections ...
14:06
ForEach Method in Java - YouTube
GeeksforGeeks
geeksforgeeks.org โบ java โบ iterate-through-list-in-java
Iterate through List in Java - GeeksforGeeks
July 23, 2025 - // Java Program iterating over a List // using stream.forEach() method // Importing all classes of // java.util method import java.util.*; // Class class GFG { // Main driver method public static void main(String args[]) { // Creating an ArrayList List<String> myList = new ArrayList<String>(); // Adding elements to the List // Custom inputs myList.add("A"); myList.add("B"); myList.add("C"); myList.add("D"); // stream.forEach() method prints // all elements inside a List myList.stream().forEach( (temp) -> System.out.println(temp)); } } Output ยท A B C D ยท Complexity of the above Method: Time Complexity: O(n), where 'n' is the size of the list. Auxiliary Space: O(1), Constant space is used for loop variables (i in this case). Java 8 introduced the Spliterator interface, which stands for "split iterator."
Mkyong
mkyong.com โบ home โบ java8 โบ java 8 foreach examples
Java 8 forEach examples | mkyong.com
December 4, 2020 - We can now reuse the same Consumer method and pass it to the forEach method of List and Stream. ... package com.mkyong.java8.misc; import java.util.*; import java.util.function.Consumer; import java.util.stream.Stream; public class ForEachConsumer { public static void main(String[] args) { List<String> list = Arrays.asList("abc", "java", "python"); Stream<String> stream = Stream.of("abc", "java", "python"); // convert a String to a Hex Consumer<String> printTextInHexConsumer = (String x) -> { StringBuilder sb = new StringBuilder(); for (char c : x.toCharArray()) { String hex = Integer.toHexString(c); sb.append(hex); } System.out.print(String.format("%n%-10s:%s", x, sb.toString())); }; // pass a Consumer list.forEach(printTextInHexConsumer); stream.forEach(printTextInHexConsumer); } }
Tpoint Tech
tpointtech.com โบ java-8-foreach
Java 8 forEach() method - Tpoint Tech
June 28, 2026 - The following Java program demonstrates how to use Java 8's forEach() method with a lambda expression to iterate through an ArrayList. ... import java.util.ArrayList; import java.util.List; public class Main { // main method public static void main(String[] args) { List ... (); // Creating a new ArrayList to store games // Adding games to the list gamesList.add("Football"); gamesList.add("Cricket"); gamesList.add("Chess"); gamesList.add("Hockey"); // Printing a separator System.out.println("------------Iterating by passing lambda expression--------------"); // Iterating over the list using forEach and printing each element using a lambda expression gamesList.forEach(game -> System.out.println(game)); } } Compile and Run
Educative
educative.io โบ answers โบ java-8-foreach
Java 8 forEach
The code snippet below illustrates the usage of the Java 8 forEach loop: import java.util.ArrayList; import java.util.List; class ForEachDemo { public static void main( String args[] ) { List<String> Fruits = new ArrayList<String>(); Fruits.add("Apple"); Fruits.add("Banana"); Fruits.add("Kiwi"); Fruits.add("Lemon"); // using forEach to print out the list using ยท
BeginnersBook
beginnersbook.com โบ 2017 โบ 10 โบ java-8-foreach
Java 8 forEach method with example
Inside forEach we are using a lambda expression to print each element of the list. import java.util.List; import java.util.ArrayList; public class Example { public static void main(String[] args) { List<String> fruits = new ArrayList<String>(); fruits.add("Apple"); fruits.add("Orange"); fruits.add("Banana"); fruits.add("Pear"); fruits.add("Mango"); //lambda expression in forEach Method fruits.forEach(str->System.out.println(str)); } }
ZetCode
zetcode.com โบ java โบ foreach
Java forEach - forEach on Java lists, maps, sets
June 17, 2025 - We explore its application with ... in handling various data structures. The forEach method executes a specified action for each element within an Iterable object, continuing until all elements are processed or an exception occurs. Introduced in Java 8, this method ...
Upgrad
upgrad.com โบ home โบ tutorials โบ software & tech โบ java arraylist foreach
Java ArrayList forEach - upGrad
2 weeks ago - Again, we use a lambda expression to print each fruit name to the console. ... import java.util.ArrayList; import java.util.List; public class upGradTutorials { public static void main(String[] args) { List<String> fruits = new ArrayList<>(); fruits.add("Apple"); fruits.add("Banana"); fruits.add("Orange"); fruits.add("Mango"); // Java 8 forEach() method System.out.println("Using forEach():"); fruits.forEach(fruit -> System.out.println(fruit)); // Java Stream forEachOrdered() method System.out.println("\nUsing forEachOrdered():"); fruits.stream().forEachOrdered(fruit -> System.out.println(fruit)); } }
Javabeginnerstutorial
javabeginnerstutorial.com โบ core-java-tutorial โบ java-8-lambda-foreach-list
Java 8 lambda foreach List - Java Beginners Tutorial
February 12, 2016 - public class java_8_forEach_list { public static void main(String[] args) { List<String> jbtObjs = new ArrayList<>(); jbtObjs.add("Java"); jbtObjs.add("Beginners"); jbtObjs.add("Tutorial"); jbtObjs.forEach(name -> System.out.println(name)); } } This site uses Akismet to reduce spam.
Oodlestechnologies
oodlestechnologies.com โบ blogs โบ java-8-foreach-and-lambda-expression-for-map-and-list
Java 8 forEach and Lambda Expression For Map and List
February 14, 2020 - List<String> vehicles = new ...ln(vehicle); } In Java 8, you can loop over a List with forEach and lambda expression like: vehicles.forEach(vehicle->System.out.println(vehicle)); vehicles.forEach(vehicle->{ if("bike".equals(vehicle)){ System.out.println(vehicle); } }); ...
Java67
java67.com โบ 2016 โบ 01 โบ how-to-use-foreach-method-in-java-8-examples.html
10 Examples of forEach() method in Java 8 | Java67
It explains all important features of Java 8 e.g. lambda expressions, streams, functional interfaces, Optional, new date, and time API, and other miscellaneous changes. ... Doesnโt talk about the most critical part of for each lambda, how to exit the for each. ReplyDelete ... Hello Umang, good point, I will probaby add that infomration when I update this article but for now, you can use the takeWhile method of Java 9 to break from forEach loop in Java like this takeWhile(n -> n.length() <=5) Delete
Codecademy
codecademy.com โบ docs โบ java โบ arraylist โบ .foreach()
Java | ArrayList | .forEach() | Codecademy
April 13, 2025 - This method is part of the Java ... implements. The .forEach() method provides a concise way to iterate through all elements in an ArrayList and apply the same operation to each element....
Stack Abuse
stackabuse.com โบ guide-to-java-streams-foreach-with-examples
Guide to Java Streams: forEach() with Examples
July 27, 2020 - AtomicInteger result2 = new AtomicInteger(); targetList.stream().forEach(integer -> { if (integer > 0) result2.addAndGet(1); }); System.out.println("Result: " + result2); The forEach() method is a really useful method to use to iterate over collections in Java in a functional approach. In certain cases, they can massively simplify the code and enhance clarity and brevity. In this article, we've gone over the basics of using a forEach() and then covered examples of the method on a List, Map and Set.