Working with Streams

So, we learned a lot of theories regarding Streams, and we also learned that Streams have a set of functional interfaces to work with (actually, the functional interfaces is the only way to work with Streams), but as I mentioned before, they work in a slightly different way than the Collections API.

To make things clearer, have a look back at the following example:

  fun main(args: Array<String>) { 
      val stream = 1.rangeTo(10).asSequence().asStream() 
       val resultantList = stream.filter{ 
          it%2==0 
      }.collect(Collectors.toList()) 
      println(resultantList) 
  } 

The preceding program is a simple one; we just grabbed a stream of numbers 1 through 10 and filtered out the odd numbers from that stream, and then collected the results inside a new List.

But let's try to understand the mechanism of how it works. We are already familiar with functional interfaces and with the filter function, as we got introduced to them in the previous chapters, but the thing that's different here is the collect function and the Collectors value, which help collect the resultant data in a new List. We will have a closer look at the collect method and the Collectors value later in this chapter, but for now, let's have a look at the functional interfaces Streams offers, and types of Streams.

So, the following is the list of operations/functional interfaces from the Stream API and their descriptions:

  • filter(): Works in the same way like Collection.filter in Kotlin. It returns a stream values consisting of the elements of this stream that match the given predicate.
  • map(): Works in the same way as Collection.map in Kotlin. It returns a stream value consisting of the results of applying the given function to each element of this stream.
  • mapToInt()/mapToLong()/mapToDouble(): Works the same way as map, but instead of returning a stream value, they return IntStream, LongStream and IntStream values, respectively. We are covering IntStream, LongStream, and IntStream in detail later in this chapter.
  • flatMap(): Works the same way as Collection.flatMap in Kotlin.
  • flatMapToInt()/flatMapToLong()/flatMapToDouble(): Works the same way as flatMap, but instead of returning a stream value, they return the IntStream, LongStream, and IntStream values, respectively.
  • distinct(): Works in the same way as Collection.distinct. It returns a stream value of distinct elements.
  • peek(): This function doesn't have any Kotlin Collection counterpart, however, it has a counterpart in RxKotlin/RxJava. This function returns the stream value consisting of the elements of this stream, additionally performing the provided action on each element, as elements are consumed from the resulting stream, much like the doOnNext operator of RxJava.
  • anyMatch(): Similar to Collection.any(), it returns whether any elements of this stream match the provided predicate. It may not evaluate the predicate on all elements, if not necessary for determining the result. If the stream value is empty, then the false value is returned and the predicate is not evaluated.
  • allMatch(): Similar to Collection.all, it returns whether all elements of this stream match the provided predicate. It may not evaluate the predicate on all elements, if not necessary for determining the result. If the stream value is empty, then the true value is returned and the predicate is not evaluated.
  • noneMatch(): Similar to Collection.none, it returns whether no elements of this stream match the provided predicate. It may not evaluate the predicate on all elements, if not necessary for determining the result. If the stream is empty, then the false value is returned and the predicate is not evaluated.

We are skipping examples of these functions, as they are similar to the Collection functions and RxJava/RxKotlin operators.

If you're wondering about it then yes, if your project is purely in Kotlin (without any Java or any other language code), you can safely ditch Streams in favor of Collections and coroutines altogether.

So, let's now take a look at the IntStream, DoubleStream, and LongStream values we mentioned earlier and explore what purpose they serve.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.197.136