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 Overflow
๐ŸŒ
GeeksforGeeks
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); } }
๐ŸŒ
Java Guides
javaguides.net โ€บ 2019 โ€บ 11 โ€บ java-8-foreach-with-list-set-and-map.html
Java 8 forEach with List, Set and Map Examples
September 14, 2020 - Collection classes that extend the Iterable interface can use the forEach() loop to iterate elements. In this post, we will learn how to forEach() method to iterate over collections, maps, and streams. Let's use normal for-loop to loop a List. ...
๐ŸŒ
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
Find elsewhere
๐ŸŒ
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
๐ŸŒ
Javaprogramto
javaprogramto.com โ€บ 2019 โ€บ 07 โ€บ java-8-foreach.html
Java 8 forEach Examples on List, Set and Map
August 16, 2020 - Example programs before and after java 8 on iterating ArrayList using forEach. ... fruits = new ArrayList<>(); fruits.add("Orange"); fruits.add("Jack Fruit"); fruits.add("Mango"); fruits.add("Apple");
๐ŸŒ
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....
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ core java โ€บ guide to the java foreach loop
Guide to the Java forEach Loop | Baeldung
June 17, 2025 - Introduced in Java 8, the forEach() method provides programmers with a concise way to iterate over a collection.
๐ŸŒ
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.
๐ŸŒ
How to do in Java
howtodoinjava.com โ€บ home โ€บ collections framework โ€บ java arraylist โ€บ java arraylist foreach()
Java ArrayList forEach() with Examples - HowToDoInJava
January 12, 2023 - ArrayList forEach() method iterate the list and performs the argument action for each element of the list until all elements have been processed.