Collectors class
It is defined inside the java.util.stream.Collectors package.
Collectors class is used to collect elements of a Stream from Collection.
Collectors is a utility class that provides implementations of the Collector interface.
The implementation of Collector is used with the Stream collect() method,it is a terminal operation Stream interface.
It implement various useful reduction operations, such as accumulate elements into collections, summarizing elements etc.
All the predefined implementations is provided in Collectors class.
Collectors consist of three things – Stream.collect() method, Collector interface and Collectors class.
Stream can be converted into different types of collections like List, Set, Map.
The Collector class provides different methods e.g. toList(), toSet(), toMap(), and toConcurrentMap() to collect the result of Stream.
Java Collectors Methods
toCollection
toList()
toSet()
toMap
joining()
mapping
filtering
counting
collectingAndThen
maxBy
minBy
groupingBy
groupingByConcurrent
averagingInt
summingInt
summarizingInt
reducing
partitioningBy
Example of toCollection:-
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Test
{
public static void main(String[] args)
{
List obj = Arrays.asList(10, 5, 8, 7, 30, 61,81);
//Here collect() returning the result i.e OddNumbers
List result = obj.stream().filter(i -> i%2 != 0).collect(Collectors.toList());
System.out.println(result);
}
}
OutPut:-
[5, 7, 61, 81]
toList() Method
A toList() method to collect the result in a List.
Program of toList():-
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Test
{
public static void main(String[] args)
{
List li = Arrays.asList("aman", "kamal", "sanam", "daman", "daman");
//It will return duplicate values also
List upperCase = li.parallelStream()
.map(String::toUpperCase)
.collect(Collectors.toList());
System.out.println(upperCase);
}
}
OutPut:-
[AMAN, KAMAL, SANAM, DAMAN, DAMAN]
toSet() Method
A Set is a collection that contains no duplicate elements.
Program of toSet():-
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Test
{
public static void main(String[] args)
{
List li = Arrays.asList("aman", "kamal", "sanam", "daman", "daman");
//It will not return duplicate values
List upperCase = li.parallelStream()
.map(String::toUpperCase)
.collect(Collectors.toList());
System.out.println(upperCase);
}
}
OutPut:-
[AMAN, KAMAL, SANAM, DAMAN
toMap() Method
It return a Collector which produces a new instance of Map, populated with keys provides keyMapper function and values provides valueMap function.
Program of toMap():-
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class Test
{
public static void main(String args[])
{
Stream st = Stream.of("aman", "kamal", "sanam", "jana");
Map obj = st.collect(
Collectors.toMap(s1 -> s1.charAt(0),
s1 -> s1));
System.out.println(obj);
}
}
OutPut:-
{a=aman, s=sanam, j=jana, k=kamal}
joining() Method
It returns a String which is the concatenation of all elements of the Stream , with the delimiter passed as input inserted between concatenated elements.
Program of joining():-
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class Test
{
public static void main(String args[])
{
String concat = Stream.of("aman", "singh").collect(Collectors.joining());
System.out.println(concat);
}
}
OutPut:-
amansingh
filtering() Method
filter() method which can be used to filter out some elements from collection based on a particular condition.
Program of filtering():-
import java.util.*;
class one
{
public static void main(String args[])
{
ArrayList li = new ArrayList<>();
li.add("Aman");
li.add("Kamal");
li.add("Love");
li.add("Ekloveya");
li.add("Saheb");
//Selecting names containing more than 4 characters
li.stream().filter((String name) -> name.length() > 4).forEach(System.out::println);
}
}
OutPut:-
Kamal
Ekloveya
Saheb
minBy() Method
It returns a Collector that returns the minimum element based on the given comparator.
Program of minBy():-
import java.util.stream.*;
import java.util.stream.Collectors;
import java.util.*;
class Test
{
public static void main(String args[])
{
Optional obj = Stream.of(10, 20, 30, 4, 50).collect(Collectors.minBy((x, y) -> x - y));
System.out.println(obj);
}
}
OutPut:-
Optional[4]
maxBy() Method
It returns a Collector that returns the maximum element based on the given comparator.
Program of maxBy():-
import java.util.stream.*;
import java.util.stream.Collectors;
import java.util.*;
class Test
{
public static void main(String args[])
{
Optional obj = Stream.of(10, 20, 30, 4, 50).collect(Collectors.maxBy((x, y) -> x - y));
System.out.println(obj);
}
}
OutPut:-
Optional[50]