- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create IntSummaryStatistics from Collectors in Java?
Let us first create a List:
List<Employee> emp = Arrays.asList( new Employee("John", "Marketing", 5), new Employee("David", "Operations", 10));
We have class Employee with name, department and rank of employees.
Now, create summary for the rank like count, average, sum, etc:
IntSummaryStatistics summary = emp .stream() .collect(Collectors.summarizingInt(p -> p.rank));
The following is an example to create IntSummaryStatistics from Collectors in Java:
Example
import java.util.Arrays; import java.util.IntSummaryStatistics; import java.util.List; import java.util.stream.Collectors; public class Demo { public static void main(String[] args) throws Exception { List<Employee> emp = Arrays.asList(new Employee("John", "Marketing", 5), new Employee("David", "Operations", 10)); IntSummaryStatistics summary = emp .stream() .collect(Collectors.summarizingInt(p → p.rank)); System.out.println("Summary Statistics = "+summary); } } class Employee { String name; String department; int rank; Employee(String name, String department, int rank) { this.name = name; this.department = department; this.rank = rank; } }
Output
Summary Statistics = IntSummaryStatistics{count=2, sum=15, min=5, average=7.500000, max=10}
- Related Articles
- Java Stream Collectors toCollection() in Java
- Collectors toSet() method in Java 8
- Collectors averagingLong () method in Java 8
- Collectors averagingDouble() method in Java 8
- Collectors counting() method in Java 8
- Collectors averagingInt() method in Java 8
- Collectors toCollection() method in Java 8
- Collectors maxBy() method in Java 8
- Collectors minBy() method in Java 8
- Collectors collectingAndThen() method in Java 8
- Collectors partitioningBy() method in Java 8
- Collectors toList() method in Java 8\n
- How to create an object from class in Java?
- How to create a Queue from LinkedList in Java?
- How to create an ArrayList from an Array in Java?

Advertisements