Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data Streamable vs Stream

I know what is the purpose and how to handle Stream as a query output, i.e.:

Stream<Person> findAll();

However, recently I stumbled upon org.springframework.data.util.Streamable<T>, used in the same way as a Stream:

Streamable<Person> findByNameContaining(String name, Sort sort);

What is the use case of Streamable?

like image 371
Forin Avatar asked Sep 19 '25 09:09

Forin


1 Answers

interface Streamable

Functional Interface: This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

Spring doc

Stream from java.util is just an interface.

Streamable was created from Spring as a functional interface that extended Iterable<T>

Streamable also has some default methods and some static ones. But it was used to be able to provide an Iterator as a lambda function taking advantage of how functional interfaces work.

That's only technical info.

Practical now from what I understand it's main use is to convert an Iterable<T> into a Stream<T>

    Iterable<String> it = Arrays.asList("a","b","c");
    Stream<String> s = Streamable.of(it).stream();

It didn't exist an easy way to directly convert an Iterable<T> into a Stream<T> therefore they invented it.

You can find most of those uses here Uses of Spring Streamable

like image 58
Panagiotis Bougioukos Avatar answered Sep 21 '25 23:09

Panagiotis Bougioukos