First note that you didn't specify what your ArrayList holds. I would assume it's Object. If it's however some container class then you need to adapt the code here and there a bit. Should be relatively easy. If you have difficulties, please don't bother commenting and giving additional information.

Without Java 8

Let's first take a look at how you would do it without Streams or Lambda:

ArrayList<Object> list = ...

List<String> result = new ArrayList<>();
// Iterate all elements
for (Object obj : list) {
    // Ignore elements that are not of type String
    if (!(obj instanceof String)) {
        continue;
    }

    // The element is String, cast it
    String objAsText = (String) obj;
    // Collect it
    result.add(objAsText);
}

The list result now only contains elements of the original list whose true type were String.


With Java 8 (Streams, Lambdas, Method references)

We can now easily write an equivalent version using the Stream API. Note that you probably confuse Streams in general with Lambda (they are different technologies, though Lambdas are often used in the Stream-API).

ArrayList<Object> list = ...

result = list.stream()                // Stream<Object>
    .filter(String.class::isInstance) // Stream<Object>
    .map(String.class::cast)          // Stream<String>
    .collect(Collectors.toList());

That's it, quite easy and readable. The Collection#stream (documentation) returns a Stream consisting of the elements in the given collection. The Stream#filter (documentation) method returns a Stream where the elements not matching the condition are skipped. The Stream#map (documentation) transforms a Stream<X> into a Stream<Y> by applying the given method to all objects (X can be equal Y). Finally the Stream#collect (documentation) method collects all elements by using the given Collector.

If you however truly wanted to use Lambdas, then this might be more what you want:

ArrayList<Object> list = ...
List<String> result = new ArrayList<>();

list.forEach(obj -> {    // This is a big lambda
    // Ignore elements that are not of type String
    if (!(obj instanceof String)) {
        return;
    }

    // The element is String, cast it
    String objAsText = (String) obj;
    // Collect it
    result.add(objAsText);
});

But I really think you confused the terms here.

Answer from Zabuzard on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_lambda.asp
Java Lambda Expressions
A lambda expression can be stored in a variable. The variable's type must be an interface with exactly one method (a functional interface). The lambda must match that method's parameters and return type.
๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ examples โ€บ iterate-over-arraylist-using-lambda-expression
Java Program to Iterate over ArrayList using Lambda Expression
import java.util.ArrayList; class Main { public static void main(String[] args) { // create an ArrayList ArrayList<String> languages = new ArrayList<>(); // add elements to the ArrayList languages.add("Java"); languages.add("Python"); languages.add("JavaScript"); // print arraylist System.out.print("ArrayList: "); // iterate over each element of arraylist // using forEach() method languages.forEach((e) -> { System.out.print(e + ", "); }); } } ... In the above example, we have created an arraylist named languages. Notice the code, languages.forEach((e) -> { System.out.print(e + ", "); }); Here, we are passing the lambda expression as an argument to ArrayList forEach().
Top answer
1 of 4
4

First note that you didn't specify what your ArrayList holds. I would assume it's Object. If it's however some container class then you need to adapt the code here and there a bit. Should be relatively easy. If you have difficulties, please don't bother commenting and giving additional information.

Without Java 8

Let's first take a look at how you would do it without Streams or Lambda:

ArrayList<Object> list = ...

List<String> result = new ArrayList<>();
// Iterate all elements
for (Object obj : list) {
    // Ignore elements that are not of type String
    if (!(obj instanceof String)) {
        continue;
    }

    // The element is String, cast it
    String objAsText = (String) obj;
    // Collect it
    result.add(objAsText);
}

The list result now only contains elements of the original list whose true type were String.


With Java 8 (Streams, Lambdas, Method references)

We can now easily write an equivalent version using the Stream API. Note that you probably confuse Streams in general with Lambda (they are different technologies, though Lambdas are often used in the Stream-API).

ArrayList<Object> list = ...

result = list.stream()                // Stream<Object>
    .filter(String.class::isInstance) // Stream<Object>
    .map(String.class::cast)          // Stream<String>
    .collect(Collectors.toList());

That's it, quite easy and readable. The Collection#stream (documentation) returns a Stream consisting of the elements in the given collection. The Stream#filter (documentation) method returns a Stream where the elements not matching the condition are skipped. The Stream#map (documentation) transforms a Stream<X> into a Stream<Y> by applying the given method to all objects (X can be equal Y). Finally the Stream#collect (documentation) method collects all elements by using the given Collector.

If you however truly wanted to use Lambdas, then this might be more what you want:

ArrayList<Object> list = ...
List<String> result = new ArrayList<>();

list.forEach(obj -> {    // This is a big lambda
    // Ignore elements that are not of type String
    if (!(obj instanceof String)) {
        return;
    }

    // The element is String, cast it
    String objAsText = (String) obj;
    // Collect it
    result.add(objAsText);
});

But I really think you confused the terms here.

2 of 4
2

If you want this as a lambda you can do the following:

Assuming you have a collection as follows:

Collection<Object> collection = new ArrayList<Object>();
collection.add(true);
collection.add("someStringValue");

Collection<String> onlyStrings =  collection.stream()
          .filter(String.class::isInstance)
          .map(object -> (String) object)
          .collect(Collectors.toList() );

//Now you have a collection of only Strings.
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ java โ€บ examples โ€บ iterate-over-arraylist-using-lambda-expression
Java Program to Iterate over ArrayList using Lambda Expression | Vultr Docs
December 19, 2024 - The use of lambda expressions in Java simplifies the process of iterating over collections such as ArrayLists. Introduced in Java 8, lambda expressions provide a clear and concise way to represent one-method interfaces using an expression.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ java-program-to-iterate-over-arraylist-using-lambda-expression
Java Program to Iterate over ArrayList using Lambda Expression
The forEach() method takes a lambda expression as an argument, which is applied to each element of the ArrayList.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-use-an-arraylist-in-lambda-expression-in-java
How to use an ArrayList in lambda expression in Java?\\n
July 11, 2020 - import java.util.*; public class LambdaWithArrayListTest { public static void main(String args[]) { ArrayList<Student> studentList = new ArrayList<Student>(); studentList.add(new Student("Raja", 30)); studentList.add(new Student("Adithya", 25)); studentList.add(new Student("Jai", 20)); studentList.removeIf(student -> (student.age <= 20)); // Lambda Expression System.out.println("The final list is: "); for(Student student : studentList) { System.out.println(student.name); } } private static class Student { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } } }
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 40697880 โ€บ lambda-expression-on-arraylist
java - Lambda expression on ArrayList - Stack Overflow
May 24, 2017 - If you're trying to implement a letsGo() method that can use a lambda expression with two inputs, then I think you want the signature to be like the following (assuming MyList is parameterised by type T):
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-lambda-expression-with-collections
Java Lambda Expression with Collections - GeeksforGeeks
July 11, 2025 - Expression: - Using lambda expression in place of comparator object for defining our own sorting in collections. ... import java.util.*; public class Demo { public static void main(String[] args) { ArrayList<Integer> al = new ArrayList<Integer>(); al.add(205); al.add(102); al.add(98); al.add(275); al.add(203); System.out.println("Elements of the ArrayList " + "before sorting : " + al); // using lambda expression in place of comparator object Collections.sort(al, (o1, o2) -> (o1 > o2) ?
Find elsewhere
๐ŸŒ
PREP INSTA
prepinsta.com โ€บ home โ€บ java tutorial โ€บ java program to iterate arraylist using lambda expression
Java Program to Iterate ArrayList using Lambda Expression | Prepinsta
May 26, 2023 - We define a lambda expression that implements this interface, which simply prints out each element of the ArrayList using System.out.println(). Then, we use the forEach method of the ArrayList to iterate over its elements, passing the lambda ...
๐ŸŒ
Medium
cdinuwan.medium.com โ€บ java-method-reference-lambda-expression-and-stream-c001675b2cf6
Java Method Reference, Lambda Expression, and Stream | by Chanuka Dinuwan | Medium
May 21, 2023 - Lambda expressions in Java provide a concise and expressive way to write anonymous functions. They are particularly useful when working with functional interfaces that require a single abstract method to be implemented.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ lambda-expressions-java-8
Java Lambda Expressions - GeeksforGeeks
This is a zero-parameter lambda expression! ... It is not mandatory to use parentheses if the type of that variable can be inferred from the context. Parentheses are optional if the compiler can infer the parameter type from the functional interface. ... import java.util.ArrayList; public class GFG{ public static void main(String[] args){ ArrayList<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); System.out.println("All elements:"); list.forEach(n -> System.out.println(n)); System.out.println("Even elements:"); list.forEach(n -> { if (n % 2 == 0) System.out.println(n); }); } }
Published ย  3 weeks ago
๐ŸŒ
myCompiler
mycompiler.io โ€บ view โ€บ 6ph8htBEkrM
Solution : Print all elements in an arraylist using lambda expression (Java) - myCompiler
November 10, 2022 - import java.util.*; import java.lang.*; import java.io.*; import java.util.ArrayList; //Objective : Write a lambda expression for printing out all the elements in an array public class Main { public static void main(String[] args) { ArrayList<Integer> numbers = new ArrayList<Integer>(); numbers.add(5); numbers.add(9); numbers.add(8); numbers.add(1); numbers.add(12); numbers.forEach( (n) -> { if(n%2==0) System.out.println(n + " is even"); else System.out.println(n + " is odd"); } ); } } Output ยท
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ converting-arraylist-to-hashmap-in-java-8-using-a-lambda-expression
Converting ArrayList to HashMap in Java 8 using a Lambda Expression - GeeksforGeeks
August 17, 2021 - Java Collection ยท Last Updated : 17 Aug, 2021 ยท A lambda expression is one or more line of code which works like function or method. It takes a parameter and returns the value. Lambda expression can be used to convert ArrayList to HashMap. Syntax: ...
๐ŸŒ
How to do in Java
howtodoinjava.com โ€บ home โ€บ collections framework โ€บ java arraylist โ€บ java arraylist foreach()
Java ArrayList forEach() with Examples - HowToDoInJava
January 12, 2023 - We can pass the simple lambda expression inline, as well. list.forEach(e -> System.out.println(e.toLowerCase())); If there are more than one statement in the Consumer action then use the curly braces to wrap the statements. list.forEach(e -> ...
๐ŸŒ
W3Schools Blog
w3schools.blog โ€บ home โ€บ java 8 lambda expression filter on a list
Java 8 lambda expression filter on list - w3schools.blog
April 14, 2018 - filtered_data = list.stream().filter(s -> s.rollNo > 2); // using lambda to iterate through collection filtered_data.forEach( student -> System.out.println(student.name) ); } }
๐ŸŒ
Java Training School
javatrainingschool.com โ€บ home โ€บ java lambda expressions
Java Lambda Expressions - Java Training School
June 5, 2024 - Thus, we can use (t) -> {//code} as lambda here inside forEach() method as an argument. Or t -> {//code} can also be used since there is just one parameter that accept(T t) method takes. package com.javatrainingschool; import java.util.ArrayList; ...
๐ŸŒ
Vultr
docs.vultr.com โ€บ java โ€บ standard-library โ€บ java โ€บ util โ€บ ArrayList โ€บ forEach
Java ArrayList forEach() - Apply Action To Each Item | Vultr Docs
September 27, 2024 - Prepare an ArrayList with sample elements. Apply the forEach() method to print each element. ... List<String> items = Arrays.asList("Apple", "Banana", "Cherry"); items.forEach(item -> System.out.println(item)); Explain Code ยท This example prints each string element in the items list. The lambda expression item -> System.out.println(item) is executed for each element.
๐ŸŒ
CodingNomads
codingnomads.com โ€บ java-lambda-expressions
Java Lambda Expressions
The action that is performed is determined by the lambda expression that you pass into the forEach() method. import java.util.ArrayList; class Main { public static void main(String args[]) { ArrayList<Integer> nums = new ArrayList<Integer>(); ...