You could use the map() feature of the stream to convert each User instance in your list to a UserWithAge instance.

List<User> userList = ... // your list

List<UserWithAge> usersWithAgeList = userList.stream()
        .map(user -> {
                // create UserWithAge instance and copy user name
                UserWithAge userWithAge = new UserWithAge();
                userWithAge.setName(user.getName());
                userWithAge.setAge(27);
                return userWithAge;
         })
         .collect(Collectors.toList()); // return the UserWithAge's as a list
Answer from Floern on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 69823827 › iterate-over-list-in-java-using-java-8-streams-without-values
collections - Iterate over list in java using Java 8 Streams without values - Stack Overflow
November 3, 2021 - ... but then, the last element is missing "?," so if you also need the question mark after the last element, you end up with · String s = list.stream().collect(Collectors.joining("?,")).concat("?,");
🌐
Mkyong
mkyong.com › home › java8 › java 8 stream.iterate examples
Java 8 Stream.iterate examples - Mkyong.com
November 29, 2018 - Stream.iterate(new int[]{0, 1}, n -> new int[]{n[1], n[0] + n[1]}) .limit(20) .map(n -> n[0]) .forEach(x -> System.out.println(x)); ... int sum = Stream.iterate(new int[]{0, 1}, n -> new int[]{n[1], n[0] + n[1]}) .limit(10) .map(n -> n[0]) // ...
🌐
CodingTechRoom
codingtechroom.com › question › java-8-streams-iterate-over-map-of-lists
How to Iterate Over a Map of Lists Using Java 8 Streams - CodingTechRoom
Map<String, List<String>> map = new HashMap<>(); map.put("key1", Arrays.asList("value1", "value2")); map.put("key2", Arrays.asList("value3", "value4")); map.forEach((key, list) -> { list.forEach(value -> System.out.println(key + ": " + value)); }); In Java 8, the Stream API provides a powerful way to handle collections of data, including iterating over a Map of Lists.
🌐
TutorialsPoint
tutorialspoint.com › how-to-iterate-list-using-streams-in-java
How to iterate List Using Streams in Java?
We create a sequential stream using the stream() method and use forEach() method with lambda expression (->) to iterate over the sequential Stream: import java.util.ArrayList; import java.util.List; import java.util.stream.Stream; public class ...
Top answer
1 of 3
6

You can use anyMatch to find the first element matching one of your conditions and terminate. Use side effects for calling the processing methods :

boolean found =
  names.stream()
       .anyMatch (name -> {
                  if (name.equals(a)) {
                    doSomething();
                    return true;
                  } else if (name.equals(b)) {
                    doSomethingElse ();
                    return true;
                  } else if (name.equals(c)) {
                    doSomethingElseElse ();
                    return true;
                  } else {
                    return false;
                  }
                }
              );

Pretty ugly, but does what you asked in a single iteration.

2 of 3
5

The answer by Eran is definitely the straightforward way of performing the search. However, I would like to propose a slightly different approach:

private static final Map<String, Runnable> stringToRunnable = new HashMap<>();

{
  stringToRunnable.put("a", this::doSomething);
  stringToRunnable.put("b", this::doSomethingElse);
  stringToRunnable.put("c", this::doSomethingElseElse);
}

public static void main(String[] args) {
  List<String> names = Arrays.asList("1", "2", "b", "a");
  names.stream()
      .filter(stringToRunnable::containsKey)
      .findFirst()
      .ifPresent(name -> stringToRunnable.get(name).run());
}

private void doSomethingElseElse() {
}

private void doSomethingElse() {
}

public void doSomething() {
}

The part that does the job is the code below, but I added it to a main() function assuming a, b, and c are strings. However, the idea would work with any datatype.

names.stream()
    .filter(stringToRunnable::containsKey)
    .findFirst()
    .ifPresent(name -> stringToRunnable.get(name).run());

The idea is to keep a map of keys and Runnables. By having Runnable as value it is possible to define a void method reference without parameters. The stream first filters away all values not present in the map, then finds the first hit, and executes its method if found.

🌐
Baeldung
baeldung.com › home › java › java list › ways to iterate over a list in java
Ways to Iterate Over a List in Java | Baeldung
June 27, 2025 - Here we’ll demonstrate a typical use for streams: countries.stream().forEach((c) -> System.out.println(c)); In this article, we demonstrated the different ways to iterate over the elements of a list using the Java API. These options included the for loop, enhanced for loop, Iterator, ListIterator, and the forEach() method (included in Java 8).
🌐
Medium
neesri.medium.com › about-stream-iterate-e2984e87caea
✍️Stream.iterate in Java 8
July 20, 2025 - Here’s an example of using Stream.iterate in Java 8: ... Add 2 each time: 0, 2, 4, 6, ... ... int number = 7; Stream.iterate(1, n -> n + 1) .limit(10) .map(n -> n * number) .forEach(System.out::println); 💥 Boom!
Find elsewhere
🌐
How to do in Java
howtodoinjava.com › home › collections framework › iterate through a collection in java
Iterate through a Collection in Java - HowToDoInJava
February 23, 2023 - List<String> list = List.of("A", "B", "C", "D"); list.stream() //other operation as needed .forEach(System.out::println); Recently introduced in java 8, this method can be called on any Iterable and takes one argument implementing the functional ...
Top answer
1 of 2
1
public static void main(String... args) {
    List<String> list1 = Arrays.asList("Hello", "World!");
    List<String> list2 = Arrays.asList("Hi", "there");
    List<String> list3 = Arrays.asList("Help Me");

    List<String> list4 = concat(list1, list2, list3); //you can add as many lists as you like here
    System.out.println(list4);
}

private static List<String> concat(List<String>... lists) {
    return Stream.of(lists)
            .flatMap(List::stream)
            .collect(Collectors.toList());
}

Output

[Hello, World!, Hi, there, Help Me]
2 of 2
0

Try this create a multiple dimension array out of List, from there you can use stream too

Customdto[][] listArray = new Customdto[][]{  l1.toArray(new Customdto[]{})
        ,  l2.toArray(new Customdto[]{}),  l3.toArray(new Customdto[]{})};
    int size = listArray[0].length > listArray[1].length && listArray[0].length > listArray[2].length ?listArray[0].length
            :(listArray[1].length > listArray[2].length ? listArray[1].length:listArray[2].length);
    for(int i = 0; i <size;i++)
    {
        if(listArray[0].length >i && listArray[1].length >i && listArray[2].length >i &&
                listArray[0][i].equals(listArray[1][i]) 
                && listArray[1][i].getCalendarDate().equals(listArray[2][i].getCalendarDate()))
        {
            l4.add(listArray[1][i]);
        }else
        {
            l4.add(null);
        }
    }

Tried with Below Input

List<Customdto> l1 = new ArrayList<Customdto>();
        List<Customdto> l2 = new ArrayList<Customdto>();
        List<Customdto> l3 = new ArrayList<Customdto>();
        List<Customdto> l4 = new ArrayList<Customdto>();
        l1.add(new Customdto(1));
        l1.add(new Customdto(2));
        l1.add(new Customdto(3));
        l1.add(new Customdto(4));
        l2.add(new Customdto(1));
        l2.add(new Customdto(2));
        l2.add(new Customdto(3));
        l3.add(new Customdto(1));
        l3.add(new Customdto(2));

Output is

[Customdto [id=1], Customdto [id=2], null, null]
🌐
Stackify
stackify.com › streams-guide-java-8
A Guide to Java Streams in Java 8 - Stackify
September 4, 2024 - Please note that the Supplier passed ... used in parallel. iterate() takes two parameters: an initial value, called the seed element and a function that generates the next element using the previous value....
🌐
HowToDoInJava
howtodoinjava.com › home › java 8 › java stream foreach()
Java Stream forEach() with Examples - HowToDoInJava
March 14, 2022 - List<Integer> list = Arrays.asList(2, 4, 6, 8, 10); Consumer<Integer> action = System.out::println; list.stream() .forEach( action ); Note that we can write the above iteration using the enhanced for-loop as well.
🌐
YouTube
youtube.com › watch
Java 8 forEach Method Tutorial | Iterate over List, Set, Stream and Map Examples - YouTube
In this video tutorial, we will learn how to use java 8 forEach() method to iterate over List, Set, Stream and Map with examples.Java 8 provides a new method...
Published   March 10, 2020
🌐
Mkyong
mkyong.com › home › java8 › java 8 foreach examples
Java 8 forEach examples - Mkyong.com
December 4, 2020 - 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())); }; // pas
Top answer
1 of 3
12

Instead of using a forEach just use streams from the beginning:

List<PersonWrapper> wrapperList = jrList.stream()
    .flatMap(jr -> seniorList.stream()
         .filter(sr -> jr.getName().equals(sr.getName()))
         .map(sr -> new PersonWrapper(jr, sr))
    )
    .collect(Collectors.toList());

By using flatMap you can flatten a stream of streams (Stream<Stream<PersonWrapper>>) into a single stream (Stream<PersonWrapper>)

If you can't instantiate wrapperList by yourself or really need to append to it. You can alter above snippet to following:

List<PersonWrapper> wrapperList = new ArrayList<>();

jrList.stream()
    .flatMap(jr -> seniorList.stream()
         .filter(sr -> jr.getName().equals(sr.getName()))
         .map(sr -> new PersonWrapper(jr, sr))
    )
    .forEach(wrapperList::add);
2 of 3
4

While Lino's answer is certainly correct. I would argue that if a given person object in jrList can only ever have one corresponding match in seniorList maximum, in other words, if it's a 1-1 relationship then you can improve upon the solution given by Lino by finding the first match as follows:

List<PersonWrapper> resultSet = jrList.stream()
                .map(p -> seniorList.stream()
                        .filter(sr -> p.getName().equals(sr.getName()))
                        .findFirst()
                        .map(q -> new PersonWrapper(p, q))
                        .get())
                .collect(Collectors.toList());

or if there is no guarantee that each person in jrList will have a corresponding match in seniorList then change the above query to:

List<PersonWrapper> resultSet = jrList.stream()
                .map(p -> seniorList.stream()
                        .filter(sr -> p.getName().equals(sr.getName()))
                        .findFirst()
                        .map(q -> new PersonWrapper(p, q))
                        .orElse(null))
                .filter(Objects::nonNull)
                .collect(Collectors.toList());

The difference is that now instead of calling get() on the result of findFirst() we provide a default with orElse in case findFirst cannot find the corresponding value and then we filter the null values out in the subsequent intermediate operation as they are not needed.

Top answer
1 of 2
1

There are multiple ways you could change your snippet here to use Java-8 Streams. Here an overview of a few straight out of my head.

List<IDWrapper> list = IntStream.range( 0, 20 ).mapToObj( IDWrapper::new ).collect( Collectors.toList() );
    List<String> details = list.stream().map( IDWrapper::getId ).map( this::getDetails ).collect( Collectors.toList() );
    List<String> details2 = list.stream().map( item -> item.getId() ).map( id -> getDetails( id ) ).collect( Collectors.toList() );
    List<String> details3 = list.stream().map( item -> getDetails( item.getId() ) ).collect( Collectors.toList() );

Let's break them down and explain their differences a bite more.

1.

List<String> details = list.stream().map( IDWrapper::getId ).map( this::getDetails ).collect( Collectors.toList() );

This one uses MethodReferences to pass the desired functions down the Stream#map(Function) method. It can be read as 'map the items, which are IDWrapper-Objects and do it by calling IDWrapper#getId() on every object and put the resulting items (integer values in this case) back into the stream'. Afterwards another call to Stream#map(Function) is done but this time the object to operate on is 'this' (my dummy class i created for the example) and call the #getDetails(int) method on this class.

2.

List<String> details2 = list.stream().map( item -> item.getId() ).map( id -> getDetails( id ) ).collect( Collectors.toList() );

This one is quite simply equal to 1. but using usual lambda expression to achieve the same.

3.

List<String> details3 = list.stream().map( item -> getDetails( item.getId() ) ).collect( Collectors.toList() );

In this case we're defining the function as a lamda and simply chaining the method class.

As mentioned before there are lots of other possibilities, but here are an education few. Hope I could help you understanding streams a little bit better.

2 of 2
0

Something like:

Map<String,Integer> m =
    list.stream()
        .collect(Collectors.toMap(e -> e.getUser, // key extractor
                                  e -> getDetail(e.getId()).getBody().getData() // value extractor
                                 )
                );    

----EDIT----

Ho ho... This is exactly Holger's comment. Give credit where credit is due.