Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning stream rather than list [duplicate]

In Java 8 I am increasingly replacing Collection return values with Stream.

So where I would once have had:

public List<Element> getElementList() {
    return elements;
}

I am now using:

public Stream<Element> streamElements() {
    return elements.stream();
}

My arguments for this are:

  1. It enforces the immutability of the underlying list
  2. It hides the fact that there is an underlying list. It could later be changed to a set or some other structure without changing the method signature.
  3. It nicely encapsulates that the user of the method is expected to do something with the items not something with a list.
  4. It can be trivially parallelised later if required.

In fact now, in my code, returning a List or some other collection is explicitly a recognition that the user may consider the collection mutable and is expected to be able to change it.

Clearly some of this can be achieved with immutable collections.

My question is: can anyone see any disadvantages with this design? Are there any advantages of immutable collections over returning a Stream?

like image 631
sprinter Avatar asked Jan 27 '15 06:01

sprinter


People also ask

Is it good practice to return stream?

For most of the cases you should return Stream . It is more flexible, is designed for better performance, and can be easily turned into Collection . You should return Collection when there are strong consistency requirements and you have to produce snapshot of a moving target.

How do you find duplicates in a list using Streams?

Get the stream of elements in which the duplicates are to be found. For each element in the stream, count the frequency of each element, using Collections. frequency() method. Then for each element in the collection list, if the frequency of any element is more than one, then this element is a duplicate element.

What does stream () return?

Generating Streams With Java 8, Collection interface has two methods to generate a Stream. stream() − Returns a sequential stream considering collection as its source. parallelStream() − Returns a parallel Stream considering collection as its source.


1 Answers

I'm not saying you shouldn't return a Stream, and even less that you should never return a Stream, but doing it also has many disadvantages:

  • it doesn't tell the user of the API if the collection is ordered (List) or not (Set), or sorted (SortedSet)
  • it doesn't tell the user of the API if the collection can contain duplicates (List) or not (Set)
  • it doesn't allow the user to easily and quickly access the first or last element of the list, or even to know which size it has.
  • if the user of the API needs to do multiple passes over the collection, he's forced to copy every element into a new collection.

I would say that choosing to return a stream rather than a collection also depends on what you already have. If the collection is already materialized (think about a JPA entity having a OneToMany already materialized as a Set), I'd probably return an immutable wrapper over the collection. If, on the other hand, the collection to return is the result of a computation or transformation of another collection, returning a Stream might be a better choice.

like image 106
JB Nizet Avatar answered Sep 21 '22 15:09

JB Nizet