Arrays.stream(values)
      .mapToObj(i -> Integer.toUnsignedString(i, 16))
      .forEach(System.out::println);
Answer from JB Nizet 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.
🌐
TutorialsPoint
tutorialspoint.com › how-to-initialize-an-array-using-lambda-expression-in-java
How to initialize an array using lambda expression in Java?
interface CustomArray<V> { V arrayValue(); } public class LambdaWithArray2 { public static void main(String args[]) { // Initilaize an array in Lambda Expression CustomArray<String>[] strArray = new CustomArray[] { () -> "Adithya", () -> "Jai", () -> "Raja", () -> "Surya" }; System.out.println(strArray[0].arrayValue()); System.out.println(strArray[1].arrayValue()); System.out.println(strArray[2].arrayValue()); System.out.println(strArray[3].arrayValue()); } }
🌐
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().
🌐
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
Find elsewhere
🌐
Oracle
docs.oracle.com › javase › tutorial › java › javaOO › lambdaexpressions.html
Lambda Expressions (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
To determine the type of a lambda expression, the Java compiler uses the target type of the context or situation in which the lambda expression was found. It follows that you can only use lambda expressions in situations in which the Java compiler can determine a target type: Variable declarations · Assignments · Return statements · Array initializers ·
🌐
C# Corner
c-sharpcorner.com › article › java-8-lambda-expressions
Java 8 - Lambda Expressions
July 15, 2020 - This article covers some Ideal Use Cases for Lambda Expressions which are explained below, Create method that match one characteristic within members · A simple approach to create several methods and each method searches that particular match, this method will print the elements of the list with odd length. package LambdaExpressions; import java.util.Arrays; import java.util.List; public class LambdaDemo { public static void main(String[] args) { List < String > list = Arrays.asList("Abc@d", "Cde", "GGhi", "Km", "MLno", "OpDq", "Qrs", "Stu23"); //using lambda 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.
🌐
Medium
medium.com › @bubu.tripathy › effective-lambda-expressions-in-java-2d4061dde77a
Effective Lambda Expressions in Java | by Bubu Tripathy | Medium
March 11, 2023 - When using Lambda expressions to filter collections, the Lambda expression is used to define a predicate that selects which elements in the collection should be included or excluded.
🌐
w3resource
w3resource.com › java-exercises › lambda › index.php
Java Lambda Expressions - Exercises, Practice, Solution
Write a Java program to implement a lambda expression to find the second largest and smallest element in an array.
🌐
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.
🌐
Medium
medium.com › @marcelogdomingues › java-lambda-expressions-techniques-for-advanced-developersava-lambda-expressions-techniques-for-c1d71c30bb1f
Java Lambda Expressions: Techniques for Advanced Developersava Lambda Expressions: Techniques for…
June 21, 2024 - Before lambda expressions, implementing functional interfaces required creating anonymous inner classes. This approach often led to verbose and less readable code. Example: Sorting a List Using an Anonymous Inner Class · import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; public class TraditionalExample { public static void main(String[] args) { List<String> names = Arrays.asList("John", "Jane", "Jack", "Jill"); Collections.sort(names, new Comparator<String>() { @Override public int compare(String a, String b) { return a.compareTo(b); } }); for (String name : names) { System.out.println(name); } } } Lambda Expression Approach ·
🌐
Stackabyte
stackabyte.com › tutorials › Java › java-lambda-expressions-tutorial
Java Lambda Expressions: Complete Guide with Examples | Stack a Byte
June 21, 2025 - Lambda expressions make collection processing more concise: List<String> names = Arrays.asList("Alice", "Bob", "Charlie"); // Before Java 8 for (String name : names) { System.out.println(name); } // With lambda expressions names.forEach(name ...
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.
🌐
InformIT
informit.com › articles › article.aspx
6.2 Lambda Expressions | Java Interfaces, Lambda Expressions, and Inner Classes | InformIT
You can form constructor references with array types. For example, int[]::new is a constructor reference with one parameter: the length of the array. It is equivalent to the lambda expression n -> new int[n]. Array constructor references are useful to overcome a limitation of Java.