Table of Contents
Examples:
Counting:
Counting is used to count number of elements in the stream.It returns Collector instance which can be accepted by collect method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package org.arpit.java2blog; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class Java8CollectorsExample { public static void main(String[] args) { List intList=Arrays.asList(10,20,30,40,50); // Counting long count = intList.stream().collect(Collectors.counting()); System.out.println(count); } } |
When you run above code, you will get below output:
1 2 3 |
5 |
AveragingInt :
AveragingInt is used to find average of stream elements as int datatype. It returns Collector instance which can be accepted by collect method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package org.arpit.java2blog; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class Java8CollectorsExample { public static void main(String[] args) { List intList=Arrays.asList(10,20,30,40,50); // Averaging int Double result1 = intList.stream().collect(Collectors.averagingInt(v->v)); System.out.println(result1); Double result2 = intList.stream().collect(Collectors.averagingInt(v->v*v)); System.out.println(result2); } } |
When you run above code, you will get below output:
1100.0
Lets understand how did you get 30 for case 1 :
(10+20+30+40+50/5)= 150/5 =30.0Â
Now you must wondering how did we get 1100 for 2nd case:
(10*10 + 20*20 + 30*30 + 40*40 + 50*50)/5=5500/5 = 1100.0Â
If you want to understand more about v-> v*v , you can go through Java 8 lambda expressions Similarly we have different function for different data types such as AveragingDouble, AveragingLong.
joining
Joining method is used to concatenate with delimiter, you can also pass prefix and suffix.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package org.arpit.java2blog; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class Java8CollectorsExample { public static void main(String[] args) { List NameList=Arrays.asList("Arpit","John","Martin"); // Counting String stringWithHyphen = NameList.stream().collect(Collectors.joining("-")); System.out.println("String with hyphen : "+stringWithHyphen); String stringWithHyphenAndPrefixAndSuffix = NameList.stream().collect(Collectors.joining("-","==","==")); System.out.println("String with hyphen , suffix and prefix : "+stringWithHyphenAndPrefixAndSuffix); } } |
When you run above code, you will get below output:
String with hyphen , suffix and prefix : ==Arpit-John-Martin==
summingint:
summingInt is used to find sum of stream elements as int datatype. It returns Collector instance which can be accepted by collect method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package org.arpit.java2blog; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class Java8CollectorsExample { public static void main(String[] args) { List intList=Arrays.asList(10,20,30,40,50); // Averaging int Double result1 = intList.stream().collect(Collectors.summingInt(v->v)); System.out.println(result1); Double result2 = intList.stream().collect(Collectors.summingInt(v->v*v)); System.out.println(result2); } } |
When you run above code, you will get below output:
5500
Similarly we have different function for different data types such as summingDouble, summingLong.
collectingAndThen:
collectingAndThen: is used to get a Collector instance and perform finishing function on top of it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package org.arpit.java2blog; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class Java8CollectorsExample { public static void main(String[] args) { List intList=Arrays.asList(10,20,30,40,50); // collectingAndThen int result1 = intList.stream().collect(Collectors.collectingAndThen(Collectors.summingInt(v->(int)v),result->result/2)); System.out.println(result1); } } |
When you run above code, you will get below output:
Reference:Â
https://docs.oracle.com/javase/8/docs/api/java/util/stream/Collectors.html