How to use Java 8 Stream Reduce Examples with List and Array
java8
10
reduce
2
stream
11
list
16
array
10
Male avatar

loveprogramming viết ngày 09/04/2021

https://grokonez.com/java/java-8/how-to-use-java-8-stream-reduce-examples-with-list-and-array

How to use Java 8 Stream Reduce Examples with List and Array

Java 8 provides an extremely powerful abstract concept Stream with many useful mechanics for consuming and processing data in Java Collection. In the tutorial, We will do lots of examples to explore more the helpful of Stream API with reduction operation on the specific topic: "Java 8 Stream Reduce Collection”.

What will we do?

  • Explain how Java 8 Stream Reduce work?
  • Apply Stream Reduction on Java List, Array

Now let’s do examples for more details!

Related posts:

Java Stream Reduce

Java Stream Reduction is a terminal operation that aggregates a Java Stream then returns one value by combining the contents of a stream.

Many reduction operations perform a specific task such as: average, sum, min, max, and count.

  • Examples:

  • Initial Java List:

List<Integer> intLst = Arrays.asList(1, 3, 5, 2, 9, 8, 15, 12);
  • How to sum all Element of a Stream?
int totalOpt = intLst.stream().mapToInt(i->i).sum();
System.out.println(totalOpt);
// 55
  • How to get average value of an Integer List?

OptionalDouble averageOpt = intLst.stream().mapToInt(i->i).average();
averageOpt.ifPresent(System.out::println);
// 6.875
  • How to get Min/Max value of an Integer List?

/* Get Min Value */
OptionalInt minOpt = intLst.stream().mapToInt(i->i).min();
minOpt.ifPresent(System.out::println);
// 1

/* Get Max value */
OptionalInt maxOpt = intLst.stream().mapToInt(i->i).max();
maxOpt.ifPresent(System.out::println);
// 15

More:

https://grokonez.com/java/java-8/how-to-use-java-8-stream-reduce-examples-with-list-and-array

Bình luận


White
{{ comment.user.name }}
Bỏ hay Hay
{{comment.like_count}}
Male avatar
{{ comment_error }}
Hủy
   

Hiển thị thử

Chỉnh sửa

Male avatar

loveprogramming

545 bài viết.
98 người follow
Kipalog
{{userFollowed ? 'Following' : 'Follow'}}
Cùng một tác giả
Male avatar
1 0
Tutorial Link: (Link) (Ảnh) Django is a Pythonbased free and opensource web framework that follows the modeltemplateview architectural pattern. A...
loveprogramming viết 7 tháng trước
1 0
Male avatar
1 0
https://loizenai.com/angular11nodejspostgresqlcrudexample/ Angular 11 Node.js PostgreSQL Crud Example (Ảnh) Tutorial: “Angular 11 Node.js Postg...
loveprogramming viết 6 tháng trước
1 0
Male avatar
1 0
Angular Spring Boot jwt Authentication Example Github https://loizenai.com/angularspringbootjwt/ (Ảnh) Tutorial: ” Angular Spring Boot jwt Authe...
loveprogramming viết 6 tháng trước
1 0
Bài viết liên quan
Male avatar
0 0
https://grokonez.com/java/java8/howtousejava8streamcollectorsgroupingbyexamples How to use Java 8 Stream Collectors.groupingBy() Examples no_toc]...
loveprogramming viết 2 tháng trước
0 0
Male avatar
0 0
https://grokonez.com/java/java8/howtousejava8streamflatmapexampleswithlistarray How to use Java 8 Stream FlatMap Examples with List, Array In the...
loveprogramming viết 2 tháng trước
0 0
{{like_count}}

kipalog

{{ comment_count }}

bình luận

{{liked ? "Đã kipalog" : "Kipalog"}}


Male avatar
{{userFollowed ? 'Following' : 'Follow'}}
545 bài viết.
98 người follow

 Đầu mục bài viết

Vẫn còn nữa! x

Kipalog vẫn còn rất nhiều bài viết hay và chủ đề thú vị chờ bạn khám phá!