- 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
Collectors collectingAndThen() method in Java 8
The collectingAndThen() method in Java Collectors class acclimates a Collector to perform an additional finishing transformation. It returns collector which performs the action of the downstream collector, followed by an additional ending step.
The syntax is as follows.
static <T,A,R,RR> Collector<T,A,RR> collectingAndThen(Collector<T,A,R> downstream, Function<R,RR> finisher)
Here, the parameter,
T − Type of the input elements
A − Intermediate accumulation type of the downstream collector
R − The result type of the downstream collector
RR − The result type of the resulting collector
downstream − Collector
finisher − A function to be applied to the final result of the downstream collector
To work with Collectors class in Java, import the following package.
import java.util.stream.Collectors;
The following is an example to implement collectingAndThen() method in Java.
Example
import java.util.List; import java.util.Collections; import java.util.stream.Collectors; import java.util.stream.Stream; public class Demo { public static void main(String[] args) { List<String> list = Stream.of("Demo1", "Demo2").collect(Collectors.collectingAndThen( Collectors.toList(), Collections::<String> unmodifiableList)); System.out.println(list); } }
Output
[Demo1, Demo2]
- Related Articles
- 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 partitioningBy() method in Java 8
- Collectors toList() method in Java 8\n
- Java Stream Collectors toCollection() in Java
- Collectors.joining() method in Java 8
- How to create IntSummaryStatistics from Collectors in Java?
- StringJoiner add() method in Java 8
- StringJoiner setEmptyValue() method in Java 8

Advertisements