Skip to main content

Java 8Stream API - Best way to transform a list: map or foreachforEach?

I have a list, myListToParse, where I want to filter the elements and apply a method on each element, and add the result in another list, myFinalList.

With the Stream API (added in Java 8), I noticed that I can do it in 2 different ways. I would like to know the more efficient way between them and understand why one way is better than the other one.

I'm open for any suggestion about a third way.

Method 1:

myFinalList = new ArrayList<>();
myListToParse.stream()
        .filter(elt -> elt != null)
        .forEach(elt -> myFinalList.add(doSomething(elt)));

Method 2:

myFinalList = myListToParse.stream()
        .filter(elt -> elt != null)
        .map(elt -> doSomething(elt))
        .collect(Collectors.toList()); 

I'm open for any suggestion about a third way.

Java 8 - Best way to transform a list: map or foreach?

I have a list myListToParse where I want to filter the elements and apply a method on each element, and add the result in another list myFinalList.

With Java 8 I noticed that I can do it in 2 different ways. I would like to know the more efficient way between them and understand why one way is better than the other one.

I'm open for any suggestion about a third way.

Method 1:

myFinalList = new ArrayList<>();
myListToParse.stream()
        .filter(elt -> elt != null)
        .forEach(elt -> myFinalList.add(doSomething(elt)));

Method 2:

myFinalList = myListToParse.stream()
        .filter(elt -> elt != null)
        .map(elt -> doSomething(elt))
        .collect(Collectors.toList()); 

Java Stream API - Best way to transform a list: map or forEach?

I have a list, myListToParse, where I want to filter the elements and apply a method on each element, and add the result in another list, myFinalList.

With the Stream API (added in Java 8), I noticed that I can do it in 2 different ways. I would like to know the more efficient way between them and understand why one way is better than the other one.

Method 1:

myFinalList = new ArrayList<>();
myListToParse.stream()
        .filter(elt -> elt != null)
        .forEach(elt -> myFinalList.add(doSomething(elt)));

Method 2:

myFinalList = myListToParse.stream()
        .filter(elt -> elt != null)
        .map(elt -> doSomething(elt))
        .collect(Collectors.toList()); 

I'm open for any suggestion about a third way.

Notice removed Reward existing answer by Naman
Bounty Ended with herman's answer chosen by Naman
Notice added Reward existing answer by Naman
Bounty Started worth 250 reputation by Naman
added instance creation for method 1 to not give the wrong impression that it is shorter
Source Link
herman
  • 12.1k
  • 5
  • 49
  • 60

I have a list myListToParse where I want to filter the elements and apply a method on each element, and add the result in another list myFinalList.

With Java 8 I noticed that I can do it in 2 different ways. I would like to know the more efficient way between them and understand why one way is better than the other one.

I'm open for any suggestion about a third way.

Method 1:

myFinalList = new ArrayList<>();
myListToParse.stream()
        .filter(elt -> elt != null)
        .forEach(elt -> myFinalList.add(doSomething(elt)));

Method 2:

myFinalList = myListToParse.stream()
        .filter(elt -> elt != null)
        .map(elt -> doSomething(elt))
        .collect(Collectors.toList()); 

I have a list myListToParse where I want to filter the elements and apply a method on each element, and add the result in another list myFinalList.

With Java 8 I noticed that I can do it in 2 different ways. I would like to know the more efficient way between them and understand why one way is better than the other one.

I'm open for any suggestion about a third way.

Method 1:

myListToParse.stream()
        .filter(elt -> elt != null)
        .forEach(elt -> myFinalList.add(doSomething(elt)));

Method 2:

myFinalList = myListToParse.stream()
        .filter(elt -> elt != null)
        .map(elt -> doSomething(elt))
        .collect(Collectors.toList()); 

I have a list myListToParse where I want to filter the elements and apply a method on each element, and add the result in another list myFinalList.

With Java 8 I noticed that I can do it in 2 different ways. I would like to know the more efficient way between them and understand why one way is better than the other one.

I'm open for any suggestion about a third way.

Method 1:

myFinalList = new ArrayList<>();
myListToParse.stream()
        .filter(elt -> elt != null)
        .forEach(elt -> myFinalList.add(doSomething(elt)));

Method 2:

myFinalList = myListToParse.stream()
        .filter(elt -> elt != null)
        .map(elt -> doSomething(elt))
        .collect(Collectors.toList()); 
deleted 9 characters in body; edited tags
Source Link
Eran
  • 392.1k
  • 56
  • 713
  • 780

I have a first list : myListToParse where iI want to filter the elements and apply a method on each elementselement, and add the result in another list : myFinalList.

With Java 8 I noticed that I can do it in 2 different way,ways. I would like to know the more efficient way between them and understand why one way is better than the other one.

I'm open for any suggestion about a third way.

Method 1:

myListToParse.stream()
        .filter(elt -> elt != null)
        .forEach(elt -> myFinalList.add(doSomething(elt)));

Method 2:

myFinalList = myListToParse.stream()
        .filter(elt -> elt != null)
        .map(elt -> doSomething(elt))
        .collect(Collectors.toList()); 

I have a first list : myListToParse where i want to filter the elements and apply a method on each elements and add the result in another list : myFinalList

With Java 8 I noticed that I can do it in 2 different way, I would like to know the more efficient way between them and understand why one way is better than the other one.

I'm open for any suggestion about a third way.

Method 1:

myListToParse.stream()
        .filter(elt -> elt != null)
        .forEach(elt -> myFinalList.add(doSomething(elt)));

Method 2:

myFinalList = myListToParse.stream()
        .filter(elt -> elt != null)
        .map(elt -> doSomething(elt))
        .collect(Collectors.toList()); 

I have a list myListToParse where I want to filter the elements and apply a method on each element, and add the result in another list myFinalList.

With Java 8 I noticed that I can do it in 2 different ways. I would like to know the more efficient way between them and understand why one way is better than the other one.

I'm open for any suggestion about a third way.

Method 1:

myListToParse.stream()
        .filter(elt -> elt != null)
        .forEach(elt -> myFinalList.add(doSomething(elt)));

Method 2:

myFinalList = myListToParse.stream()
        .filter(elt -> elt != null)
        .map(elt -> doSomething(elt))
        .collect(Collectors.toList()); 
changed title: 'parse' is not really the correct term in this context
Link
Loading
deleted 5 characters in body
Source Link
Emilien Brigand
  • 10.6k
  • 9
  • 34
  • 37
Loading
code format, added java8 tag
Source Link
tobias_k
  • 82.5k
  • 12
  • 127
  • 184
Loading
Source Link
Emilien Brigand
  • 10.6k
  • 9
  • 34
  • 37
Loading