In Java, collections and arrays are two common data structures, on which many operations are regularly performed, including addition, deletion, modification, query, aggregate, statistics, and filtering. These operations also exist in relational databases. However, it is not very convenient to process collections and arrays before Java 8.
This problem is greatly alleviated in Java 8 by introducing a new abstraction called Stream API, which allows us to process data in a declarative manner. This article shows how to use Stream. Note that the performance and principles of Stream are not the central part of this article.
Stream provides a high-level abstraction for Java collection operations and expressions by querying data from databases similar to SQL statements.
Stream API can significantly improve the productivity of Java programmers and allow them write effective, clean, and concise code.
A collection of elements to be processed are considered as a stream, which is transmitted in a pipeline. These elements can be processed on nodes of the pipeline, such as filter, sort, and aggregate.
Let's use an example to see what a Stream can do:
The preceding example gets some plastic balls as the data source, filters the red ones, melts them down and converts them into random triangles. Another filter removes small triangles. A reducer sums up the circumferences
As shown in the preceding figure, a Stream involves three critical operations: stream creation, intermediate operations and terminal operations.
In Java 8, many methods can be used to create a Stream.
In addition to many stream-related classes, Java 8 also enhances the collection class itself. The stream method in Java 8 can convert a collection into a Stream.
List<String> strings = Arrays.asList("Hollis", "HollisChuang", "hollis", "Hello", "HelloWorld", "Hollis");
Stream<String> stream = strings.stream();
The preceding example creates a Stream from an existing List. In addition, the parallelStream method can create a parallel stream for a collection.
It is also very common to create a Stream from a collection.
The of method provided by Stream can be used to directly return a Stream consisting of specified elements.
Stream<String> stream = Stream.of("Hollis", "HollisChuang", "hollis", "Hello", "HelloWorld", "Hollis");
The preceding code creates and returns a Stream by using the of method.
A Stream may have many intermediate operations, which can be combined to form a pipeline. Each intermediate operation is like a worker on the pipeline. Each worker can process the Stream. intermediate operations return a new Stream.
The following is a list of common intermediate operations:
The filter method is used to filter elements by specified conditions. The following code snippet uses the filter method to filter empty strings:
List<String> strings = Arrays.asList("Hollis", "", "HollisChuang", "H", "hollis");
strings.stream().filter(string -> ! string.isEmpty()).forEach(System.out::println);
//Hollis, , HollisChuang, H, hollis
The map method maps each elements to its corresponding result. The following code snippet use the map method to generate the square numbers of corresponding elements:
List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
numbers.stream().map( i -> i*i).forEach(System.out::println);
//9,4,4,9,49,9,25
Limit returns the first N elements in a Stream. Skip abandons the first N elements in a Stream. The following code snippet uses the limit method to retain the first four elements:
List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
numbers.stream().limit(4).forEach(System.out::println);
//3,2,2,3
The sorted method sorts elements in a Stream. The following code snippet uses the sorted method to sort Stream elements:
List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
numbers.stream().sorted().forEach(System.out::println);
//2,2,3,3,3,5,7
The distinct method is used to remove duplicates. The following code snippet uses the distinct method to deduplicate elements:
List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
numbers.stream().distinct().forEach(System.out::println);
//3,2,7,5
Next, we use an example and a figure to show what will happen to a Stream after performing operations filter, map, sort, limit, and distinct.
The following is the code:
List<String> strings = Arrays.asList("Hollis", "HollisChuang", "hollis", "Hello", "HelloWorld", "Hollis");
Stream s = strings.stream().filter(string -> string.length()<= 6).map(String::length).sorted().limit(3)
.distinct();
The following figure shows steps and the result for each step.
Stream terminal operations also return a Stream. How can we convert a Stream into the desired type? For example, count elements in a Stream and convert that Stream into a collection. To do this, we need terminal operations.
A terminal operation will consume a Stream and generate a final result. That is to say, after a terminal operation is performed on a Stream, the Stream is not reusable and any intermediate operations are not allowed on that Stream. Otherwise, an exception is thrown:
java.lang.IllegalStateException: stream has already been operated upon or closed
This is the same as the meaning of the saying "You cannot step into the same river twice".
The following table lists the common terminal operations.
The forEach method iterates through elements in a Stream. The following code snippet uses forEach to return 10 random numbers:
Random random = new Random();
random.ints().limit(10).forEach(System.out::println);
The count method counts the elements in a Stream.
List<String> strings = Arrays.asList("Hollis", "HollisChuang", "hollis","Hollis666", "Hello", "HelloWorld", "Hollis");
System.out.println(strings.stream().count());
//7
The collect operation is a reduce operation that can accept various parameters and accumulate the stream elements into a summary result:
List<String> strings = Arrays.asList("Hollis", "HollisChuang", "hollis","Hollis666", "Hello", "HelloWorld", "Hollis");
strings = strings.stream().filter(string -> string.startsWith("Hollis")).collect(Collectors.toList());
System.out.println(strings);
//Hollis, HollisChuang, Hollis666, Hollis
Next, we still use a figure to show the results of different terminal operations on the Stream given in the preceding example, on which filter, map, sort, limit, and distinct operations have been performed.
The following figure uses an example to show the input and output of all the operations described in this article.
This article explains the use and characteristics of Streams in Java 8. Stream creation, stream intermediate operations, and terminal operations are also described in this article.
We can use two methods to create a Stream: using the stream method of a Collection or using the of method of a Stream.
Stream intermediate operations can process Streams. Both the input and the output of intermediate operations are Streams. Intermediate operations include filter, map, and sort.
Stream terminal operations can convert a Stream into some other container, such as counting elements in a Stream, converting a Stream into a collection and iterating through elements in a Stream.
Ali-Perseus: Unified and Distributed Communication Framework for Deep Learning
Alibaba Builds High-Speed RDMA Network for AI and Scientific Computing
2,599 posts | 762 followers
Followmizhou - June 15, 2023
Changyi - April 14, 2020
Alibaba Cloud Community - March 16, 2023
Apache Flink Community China - May 18, 2022
Changyi - September 2, 2021
Alibaba Clouder - June 5, 2019
2,599 posts | 762 followers
FollowAPI Gateway provides you with high-performance and high-availability API hosting services to deploy and release your APIs on Alibaba Cloud products.
Learn MoreOpenAPI Explorer allows you to call an API through its web interface or WebCLI, and view the entire process.
Learn MoreAlibaba Cloud (in partnership with Whale Cloud) helps telcos build an all-in-one telecommunication and digital lifestyle platform based on DingTalk.
Learn MoreAn array of powerful multimedia services providing massive cloud storage and efficient content delivery for a smooth and rich user experience.
Learn MoreMore Posts by Alibaba Clouder