Home > Java > javaTutorial > Example analysis of Stream data flow in Java8

Example analysis of Stream data flow in Java8

黄舟
Release: 2017-10-16 10:02:26
Original
1458 people have browsed it

Stream provides a high-level abstraction for Java set operations and expressions in an intuitive way similar to querying data from a database using SQL statements. Next, I will share with you the knowledge of Stream data flow in java8 through this article. Friends who are interested should take a look.

Stream is an API introduced in java8 that heavily uses lambda expressions. Stream provides a high-level abstraction for Java set operations and expressions in an intuitive way similar to using SQL statements to query data from a database. Intuition means that when developers write code, they only need to focus on what the result they want is, rather than on the specific way to achieve the result. In this chapter, we will introduce why we need a new data processing API, the differences between Collection and Stream, and how to apply the StreamAPI to our coding.

Filter duplicate elements

The Stream interface supports the distinct method, It returns a stream of elements (implemented according to the hashCode and equals methods of the elements generated by the stream).

For example, the following code will filter out all even numbers in the list and ensure that there are no duplicates.


List<Dish> dishes = Dish.menu.stream()
.filter(Dish::isVegetarian)
.collect(Collectors.toList());
Copy after login

Skip the specified number of elements

Stream supports the skip(n) method and returns a The stream of first n elements is thrown away. If there are less than n elements in the stream, an empty stream is returned. limit(n) and skip(n) are complementary


List<Dish> dishSkip = Dish.menu.stream().filter(d -> d.getCalories() > 300).skip(2) //去掉符合要求的集合中的前2个元素后返回
.collect(Collectors.toList());
dishSkip.forEach(System.out::println);
Copy after login

map operation

Stream supports map method, which will accept a function as parameter. This function will be applied to each element and map it to a new element


List<String> list = st.skip(0).limit(2).map(s->s.toUpperCase()).collect(Collectors.toList());
Copy after login

Sum of elements


List<Integer> numbers = Arrays.asList(3,4,5,1,2);
int sum1 = numbers.stream().reduce(0,(a, b) -> a + b);
System.out.println(sum1);
int sum2 = numbers.stream().reduce(0,Integer::sum);
System.out.println(sum2);
Copy after login

Maximum value


int max = numbers.stream().reduce(0,Integer::max);
System.out.println(max);
Copy after login

Minimum value


//reduce不接受初始值,返回一个Optional对象(考虑流中没有任何元素的情况)
Optional<Integer> min = numbers.stream().reduce(Integer::min);
min.ifPresent(System.out::println);
Copy after login

Summarize

The above is the detailed content of Example analysis of Stream data flow in Java8. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template