In this tutorial you’ll see how to use Collectors.collectingAndThen()
method with the help of some examples.
Collectors.collectingAndThen() method in Java Stream
As the name of the method itself suggests this method helps you to provide logic for two steps in a single method call.
- How to collect stream elements.
- What to do after collecting Stream elements.
Syntax of collectingAndThen() method is as given below-
Collector<T,A,RR> collectingAndThen(Collector<T,A,R> downstream, Function<R,RR> finisher)
Here parameters are-
- downstream- A collector that is applied to stream elements.
- finisher- A function to be applied to the final result of the downstream collector
Collectors.collectingAndThen() Java examples
1. If you are collecting stream elements to a List and want to convert that list to an unmodifiable list.
import java.util.Collections; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class CollectAndThenDemo { public static void main(String[] args) { List<Integer> numList = Stream.of(1,2,3,4,5) .collect(Collectors.collectingAndThen( Collectors.toList(), Collections::unmodifiableList)); System.out.println(numList); // Unsupported operation (List is immutable) numList.add(6); } }Output
[1, 2, 3, 4, 5] Exception in thread "main" java.lang.UnsupportedOperationException at java.base/java.util.Collections$UnmodifiableCollection.add(Collections.java:1062) at com.knpcode.javaprog.streams.CollectAndThenDemo.main(CollectAndThenDemo.java:15)
Trying to add a new element to the unmodifiable list results in an error. Just to demonstrate that we actually have an unmodifiable list.
2. We want to get the result of each student in the List. For that we have to get the average of marks and then use it to find whether student passed or failed.
Student class
Student class has a list of Marks.public class Student { private int rollNo; private String name; private int age; private List marks; Student(int rollNo, String name, int age, List marks){ this.rollNo = rollNo; this.name = name; this.age = age; this.marks = marks; } public int getRollNo() { return rollNo; } public String getName() { return name; } public int getAge() { return age; } public List getMarks() { return marks; } @Override public String toString() { return "Roll Number: " + getRollNo() + " Name: " + getName() + " Age: " + getAge() + " Marks: " + getMarks(); } }
We’ll have list of students and we need to iterate the Student list and then the list of marks for each student to get the average and then use the same average to get the result for that Collectors.collectingAndThen() method is used. Logic for pass is average marks greater than 75.
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class CollectAndThenDemo { public static void main(String[] args) { List<Student> studentList = Arrays.asList(new Student(1, "Mercy", 19, Arrays.asList(75, 61, 78)), new Student(2, "Ram", 20, Arrays.asList(99, 88, 91)), new Student(3, "Priscilla", 20, Arrays.asList(68, 78, 65)), new Student(4, "Jacques", 22, Arrays.asList(92, 89, 78)), new Student(5, "Peter", 19, Arrays.asList(68, 57, 70))); studentList.forEach(s -> { System.out.print("Name- " + s.getName()); String result = s.getMarks().stream().collect(Collectors.collectingAndThen( Collectors.averagingInt(Integer::intValue), (avg) -> avg > 75 ? "Pass" : "Fail")); System.out.println(" Result- " + result) ; }); } }Output
Name- Mercy Result- Fail Name- Ram Result- Pass Name- Priscilla Result- Fail Name- Jacques Result- Pass Name- Peter Result- Fail
That's all for the topic Java Stream Collectors.collectingAndThen() Examples. If something is missing or you have something to share about the topic please write a comment.
You may also like
- Java Stream Collectors averagingInt(), averagingLong(), averagingDouble()
- Java Stream Collectors.teeing() Examples
- java.time.Period Class With Examples
- Why wait(), notify() And notifyAll() Methods Are in Object Class
- Why String is Immutable in Java
- Creating Password Protected Zip File in Java
- First React App – Hello World React Example
- Hadoop MapReduce Word Count Program
No comments:
Post a Comment