Sum(list Java)

Sum of list with stream filter in Java

JavaServer Side ProgrammingProgramming

To get sum of list with stream filter in Java, the code is as follows

Example

Live Demo

import java.util.*; public class Demo { public static void main[String[] args] { List my_list = new ArrayList[]; my_list.add[11]; my_list.add[35]; my_list.add[56]; my_list.add[78]; my_list.add[91]; System.out.println[sum[my_list]]; } public static int sum[List my_list] { System.out.println["In the main function, the sum of list with filter is "]; return my_list.stream[].filter[i -> i > 5].mapToInt[i -> i].sum[]; } }

Output

In the main function, the sum of list with filter is 271

A class named Demo contains the main function that creates an ArrayList. Elements are added into the arraylist using the add[] function. The sum of all the elements of the list can be printed on the screen using the sum function. Another function named sum is defined that returns the filtered output by mapping the value to an integer.

AmitDiwan
Published on 13-Jul-2020 11:52:31
Previous Page Print Page
Next Page
Advertisements

Video liên quan

Chủ Đề