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

Example analysis of Stream data flow in Java8

黄舟
黄舟Original
2017-10-16 10:02:261434browse

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());

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);

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());

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);

Maximum value


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

Minimum value


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

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!

Statement:
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