Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement java 8 Stream processing

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to achieve java 8 Stream processing, the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Package staticTest

Import java.util.Arrays

Import java.util.Collection

Public class Streams {

Private enum Status {

OPEN, CLOSED

}

Private static final class Task {

Private final Status status

Private final Integer points

Task (final Status status, final Integer points) {

This.status = status

This.points = points

}

Public Integer getPoints () {

Return points

}

Public Status getStatus () {

Return status

}

@ Override

Public String toString () {

Return String.format ("[% s,% d]", status, points)

}

}

Public static void main (String args []) {

Final Collection tasks = Arrays.asList (

New Task (Status.OPEN, 5)

New Task (Status.OPEN, 13)

New Task (Status.CLOSED, 8)

);

/ / Calculate total points of all active tasks using sum ()

Final long totalPointsOfOpenTasks = tasks.stream () .filter (task-> task.getStatus () = = Status.OPEN)

.mapToInt (Task::getPoints) .sum ()

System.out.println ("Total points:" + totalPointsOfOpenTasks)

}

}

Treatment process

Task collections are transformed into stream stream objects, and data is filtered through filter, taking only those with status as OPEN.

The mapToInt operation calls the getPoints method of each task instance through Task::getPoints to convert the stream of Task into the stream of Integer.

Finally, all the scores are added up with the sum function to get the final result.

On how to achieve java 8 Stream processing to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report