Java. Data streams. Stream API. General information




Data streams. Stream API. General information

It is recommended that you familiarize yourself with the following topics before exploring this topic:


Contents


Search other websites:

1. Stream API. Characteristics

Starting with the release of the JDK 8 in the Java language have been introduced tools to work with data streams, which are called Stream Application Programming Interface (Stream API). Work with the Stream API is based on the use of lambda expressions.

The characteristic features of this interface is the use of different operations on data streams. These operations include:

  • data search;
  • data modification;
  • filtering the data stream to get a new stream;
  • data sorting;
  • various other data manipulations.

Operations on data streams are performed based on the formation of appropriate queries. The Stream API is well suited for processing large amounts of data using parallel mechanisms.

 

2. Data stream. Definition

The term “data stream” refers to the data transmission channel. The concept of a data source is defined for a data stream. The data sources can be an array, a collection, a list, and the like. The data stream operates on these sources. Data stream can also be defined as a sequence of objects.

In the stream itself, the data is not stored, but only moved during its processing (filtering, sorting, etc.). While processing the data stream, the data source is not changed. This means that when the data is sorted, a new sorted data stream is created, and the original source remains unsorted.

 

3. Interfaces from java.util.stream package
3.1. Interface BaseStream. Interface methods

The Stream API is part of the java.util.stream package. This package contains a set of streaming interfaces that form a hierarchy.

The base streaming interface is BaseStream, which has the following declaration

interface BaseStream<T, S extends BaseStream<T, S>>

here

  • T – the type of elements in the data stream;
  • S – a data stream type that extends the BaseStream interface.

The BaseStream interface declares a number of methods, listed below.

1. Close the data stream

void close()

Method closes the calling data stream. It is imperative to close the data streams associated with the files.

2. Determine if data stream is parallel

boolean isParallel()

The method returns true if the data stream is parallel.

3. Get the iterator for data stream

Iterator<T> iterator()

here

  • T – type of data stream items.

The method receives an iterator for a data stream and returns a reference to it. The method is the final operation.

4. Set a handler for the stream close event

S onClose(Runnable handler)

here

  • S – type of newly created data stream.
  • handler – a method containing code to be executed when the stream is closed.

The onClose() method returns a new data stream with the specified close event handler. The specified handler is called when the data stream is closed. The method is an intermediate operation.

5. Return a parallel data stream

S parallel()

here

  • S – the type of the newly created parallel data stream.

The method returns a parallel data stream based on the calling data stream. If the calling data stream is parallel, then it is returned. This is an intermediate operation.

6. Return the sequential data stream

S sequential()

here

  • S – the type of the newly created serial data stream.

The method returns a sequential data stream based on the calling data stream. If the calling data stream is already sequential, then that stream is returned. The method is an intermediate operation.

7. Get a delimiter iterator

Spliterator<T> spliterator()

here

  • T – type of data stream items.

The method receives a delimiter iterator for the data stream and returns a reference to it. The method is the final operation.

8. Return an unordered data stream

S unordered()

here

  • S – the type of the resulting unordered data stream.

The method returns an unordered data stream based on the calling data stream. If the calling data stream is already unordered, then that one is returned. The method is an intermediate operation.

 

3.2. Interface Stream. Review of methods

Several interfaces are inherited from the base interface BaseStream. The most common of these is the generic interface Stream<T>, which has the following declaration

interface Stream<T>

here

  • T – the type of items in the data stream.

The Stream<T> interface defines a number of methods that you can use when processing streams of data. These methods are described below.

1. Collect the elements in the container

<R, A> R collect(Collector<? super T, A, R> collector)

here

  • R – the type of container in which the items are accumulated;
  • T – the type of the element from the calling data stream;
  • A – internal storage type;
  • collector – an accumulation function that is represented by a lambda expression. The function determines the order of the accumulation process.

The method implements the mutable reduction operation. The method accumulates items in a changing container and returns that container.

The collect() method is the final operation.

2. Get the number of elements in a stream

long count()

The count() method is the final operation.

3. Generate a new data stream for a given filter

Stream<T> filter(Predicate<? super T> predicate)

here

  • T – type of data stream elements;
  • predicate – the condition by which a new data stream is formed.

The filter() method is the intermediate operation.

4. Perform an action on each element of the data stream

void forEach(Consumer<? super T> action)

here

  • action – reference to the standard functional interface Consumer<T>. The Consumer<T> interface implements a method that performs some action on an element of type T. This action will be applied to every item in the data stream.

The forEach() method is the final operation.

5. Apply specified mapping function for generic type R

<R> Stream<R> map(Function<? super T, ? extends R> map_function)

here

  • map_function – a display function that is applied to items from the calling data stream. The result of the function operation is a new data stream that contains these elements.

This is an intermediate operation.

6. Apply display function for a stream of type DoubleStream

DoubleStream mapToDouble(ToDoubleFunctin <? super T> map_function)

here

  • map_function – a display function that is applied to the elements of the calling data stream. The result of the mapToDouble() function is a new data stream of the DoubleStream type. The type of the elements in the stream is set to Double. This is an intermediate operation.

7. Apply display function for IntStream stream

IntStream mapToInt(ToIntFunctin <? super T> map_function)

here

  • map_function – a display function that is applied to the elements of the calling data stream. The result of the mapToInt() function is a new data stream of type IntStream. The type of the elements in the stream is set to Integer. This is an intermediate operation.

8. Apply display function for a stream of type LongStream

LongStream mapToLong(ToLongFunctin <? super T> map_function)

here

  • map_function – a display function that is applied to the elements of the calling data stream. Based on the specified display function, a new LongStream is created containing these elements. This is an intermediate operation.

9. Finding the minimum value in a data stream of type T

Optional<T> min(Comparator<? super T> comparator)

here

  • comparator – a method reference that describes the code for comparing two elements of type T. The code of this method determines the element with the minimum value in the data stream. The min() method is the final operation.

10. Finding the maximum value in a data stream of type T

Optional<T> max(Comparator<? super T> comparator)

here

  • comparator – method reference that describes the code for comparing two elements of type T. Based on the code of this method, the element with the maximum value in the data stream is determined.

The max() method is the final operation.

11. Implement reduction for items in the calling data stream

T reduce (T identityVal, BinaryOperator<T> storage)

here

  • identityVal – an identity value that is used in conjunction with the storage function to get the same item unchanged;
  • storage – a function that operates on two values of type T and returns the result.

Method reduce() is the final operation.

12. Sorting the data stream

Stream<T> sorted()

The sorted() method is designed to sort the data stream in natural order (ascending elements). If you need to change the sorting order of elements, then you need to implement the standard functional interface Comparator<T> and pass the lambda expression to this method. This is the final operation.

13. Create an array from elements in the calling data stream

Object[] toArray()

The toArray() method is used to convert the data stream to an array of type Object[]. The method allows you to operate on any types (Integer, Double, Float, etc.).

 


Related topics