1

I am trying to convert a for loop which takes a list of one object and stores that list into another object:

/*List<Rbs> cellList = new ArrayList<Rbs>();
    for (CommonPrep commonTest : commonTest.values()) {
        cellList.addAll(commonTestInfo.getRbsList());
}*/

List<Rbs> cellList = 
          commonTest.values()
                    .stream()
                    .flatMap(rbsList -> rbsList.getRbsList().stream())
                    .collect(Collectors.toList());

Is there a better way of doing it than using two streams (as I'm currently doing)?

Xavier Guihot
  • 54,987
  • 21
  • 291
  • 190
Simi
  • 13
  • 5
  • 1
    What makes you concerned about the number of stream calls made? – Ousmane D. Mar 29 '18 at 17:21
  • 1
    Note, as streams do not hold the objects it processes, it's a _cheap_ operation in creating stream objects. don't be concerned about it, focus more on what you want to achieve and let the library handle the rest. – Ousmane D. Apr 01 '18 at 14:30

1 Answers1

4

A slightly different variant.

List<Rbs> cellList = 
    commonTest.values()
              .stream()
              .map(CommonPrep::getRbsList)
              .flatMap(Collection::stream)
              .collect(Collectors.toList());

As you can see, eventually you'll need to call the stream method once in the .values().stream() and N times in the flatMap (due to the return value of the function passed to flatMap) where N is the number of elements returned from the map operation.

In fact, each intermediate operation also returns a Stream<T>, so there are more stream objects than what I've mentioned above.

There's no way to avoid it. plus creating a stream, in general, is a cheap operation so you shouldn't be concerned about it.

Thus, unless you're experiencing performance issues it's better not to think about how many Stream objects are created and instead focus on writing your query to achieve what you want and let the library handle the rest.

Even, if you were experiencing performance issues trying to find ways to avoid creating new streams on each function call or method call is not going to work as all intermediate operations returns a new stream on each invocation and some operations accepting behavioral parameters also return new streams as in the function passed to flatMap.

I very much doubt you're currently experiencing performance issues due to the creation of stream objects. Anyhow, you can always consider going parallel when you're experiencing performance problems.

It's important to understand several factors before even attempting to go parallel. You can read the answers here for things to consider.

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • @Simi you're more than welcome. The important thing to keep in mind is that streams do not hold the objects it processes, it's a _cheap_ operation in creating stream objects. don't be concerned about it, focus more on what you want to achieve and let the library handle the rest. – Ousmane D. Apr 02 '18 at 18:08