Use Streams on any Android Device!
Use Streams everywhere just like you wish you always could!
final List<ViewModel> viewModels = StreamCompat.of(models)
.filter(ExampleModel::isVisible)
.mapToInt(ExampleModel::getTextResId)
.mapToObj(context::getString)
.map(ViewModel::new)
.collect(Collectors.toList());
To use StreamCompat just add this to the dependencies closure in your build.gradle:
compile 'com.github.wrdlbrnft:stream-compat:0.3.0.12'
You can use Java 8 features like lambda expressions and method references by using at least version 26.0.0
of the Android Build Tools.
StreamCompat is not meant to be a full backport of the Stream API. The most important features are included based on an Iterator
based implementation. There is the basic Stream
for dealing with objects, as well as special versions for primitive types. The full list is:
Stream
: For streaming objectsIntStream
: For streaming int
values.LongStream
: For streaming long
values.FloatStream
: For streaming float
values.DoubleStream
: For streaming double
values.CharacterStream
: For streaming char
values.ByteStream
: For streaming byte
values.Each of those implementations supports almost all features of Javas Stream API, for example:
filter
: To filter elements.map
: To map values to other values.flatMap
: To map other streams into one long stream.reduce
: To combine elements from a stream into one result.distinct
: To remove duplicate elements.collect
: To collect elements into a container.toArray
: to collect elements into an array.As well as many other useful operations like limit
, skip
, count
etc. Anyone who already knows the Stream API from Java is going to have no problem to get started with StreamCompat, but there are a few differences and optimizations for mobile devices which means that some things are going to work a little bit differently. Notable differences are:
Collector
implementations for Android specific collections, and lots of helper methods to help dealing with them.