Added examples for collectors
This commit is contained in:
94
Java/Collectors/CollectorsExamples.java
Normal file
94
Java/Collectors/CollectorsExamples.java
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
import java.util.*;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
public class CollectorsExamples {
|
||||||
|
|
||||||
|
public static void main(String[] args){
|
||||||
|
System.out.println("*****Examples of different Collectors methods*****");
|
||||||
|
|
||||||
|
System.out.println("Example of Collectors.toList : ");
|
||||||
|
System.out.println(Stream.of(4,3,2,7,1).collect(Collectors.toList()));
|
||||||
|
|
||||||
|
System.out.println("***********************");
|
||||||
|
System.out.println("Example of Collectors.toSet : ");
|
||||||
|
System.out.println(Stream.of(1,7,9,2,3,1).collect(Collectors.toSet()));
|
||||||
|
|
||||||
|
System.out.println("***********************");
|
||||||
|
System.out.println("Example of Collectors.toCollection : ");
|
||||||
|
Stream<Integer> stream = Stream.of(1,7,9,2,3,1);
|
||||||
|
|
||||||
|
Set<Integer> set = stream.collect(Collectors
|
||||||
|
.toCollection(TreeSet::new));
|
||||||
|
System.out.println(set);
|
||||||
|
|
||||||
|
System.out.println("***********************");
|
||||||
|
System.out.println("Example of toMap and toConcurrentMap : ");
|
||||||
|
Stream<String> wordStream = Stream.of("one", "hello", "opera", "hi",
|
||||||
|
"music");
|
||||||
|
HashMap<Character, String> m = wordStream.collect(
|
||||||
|
Collectors.toMap(word -> word.charAt(0),
|
||||||
|
word -> word,
|
||||||
|
(word1, word2) -> word1 +","+ word2,
|
||||||
|
HashMap::new));
|
||||||
|
System.out.println(m);
|
||||||
|
|
||||||
|
System.out.println("***********************");
|
||||||
|
System.out.println("Example of summingInt,summingLong and summingDouble : ");
|
||||||
|
Stream<Integer> intStream = Stream.of(1,2,3,4,5);
|
||||||
|
System.out.println(intStream.collect(Collectors.summingInt(Integer::intValue)));
|
||||||
|
|
||||||
|
System.out.println("***********************");
|
||||||
|
System.out.println("Example of averagingInt,averagingLong and averagingDouble : ");
|
||||||
|
Stream<Integer> intStream1 = Stream.of(5,5,4,6,4);
|
||||||
|
System.out.println(intStream1.collect(Collectors.averagingInt(Integer::intValue)));
|
||||||
|
|
||||||
|
System.out.println("***********************");
|
||||||
|
System.out.println("Example of counting, maxBy, minBy : ");
|
||||||
|
Stream<String> strStream = Stream.of("1","2","3","4","5");
|
||||||
|
System.out.println(strStream.collect(Collectors.counting()));
|
||||||
|
|
||||||
|
Stream<Integer> intStream2 = Stream.of(4,2,6,12,33,44);
|
||||||
|
System.out.println(intStream2.collect(Collectors.maxBy(Comparator.naturalOrder())));
|
||||||
|
|
||||||
|
System.out.println("***********************");
|
||||||
|
System.out.println("Example of groupingBy and partitioningBy : ");
|
||||||
|
Stream<String> strStream2 = Stream.of("one","two","b","d","three");
|
||||||
|
System.out.println(strStream2.collect(Collectors.groupingBy(String::length)));
|
||||||
|
|
||||||
|
Stream<Integer> intStream3 = Stream.of(1, 4, 5, 2, 7);
|
||||||
|
System.out.println(intStream3.collect(Collectors.partitioningBy(e -> e % 2 == 0)));
|
||||||
|
|
||||||
|
System.out.println("***********************");
|
||||||
|
System.out.println("Example of summarizingInt,summarizingDouble and summarizingLong : ");
|
||||||
|
Stream<Integer> intStream4 = Stream.of(5, 4, 3, 2, 1);
|
||||||
|
Map<Integer, IntSummaryStatistics> map = intStream4.collect(Collectors.groupingBy(Integer::intValue, Collectors.summarizingInt(Integer::intValue)));
|
||||||
|
|
||||||
|
System.out.println(map);
|
||||||
|
|
||||||
|
System.out.println("***********************");
|
||||||
|
System.out.println("Example of joining : ");
|
||||||
|
Stream<String> strStream3 = Stream.of("one","two","three","four","five");
|
||||||
|
System.out.println(strStream3.collect(Collectors.joining()));
|
||||||
|
|
||||||
|
Stream<String> strStream4 = Stream.of("one","two","three","four","five");
|
||||||
|
System.out.println(strStream4.collect(Collectors.joining("-")));
|
||||||
|
|
||||||
|
Stream<String> strStream5 = Stream.of("one","two","three","four","five");
|
||||||
|
System.out.println(strStream5.collect(Collectors.joining("-","start:",":end")));
|
||||||
|
|
||||||
|
System.out.println("***********************");
|
||||||
|
System.out.println("Example of mapping,flatMapping and reducing : ");
|
||||||
|
Stream<String> strStream6 = Stream.of("one","two","three","four");
|
||||||
|
System.out.println(strStream6.collect(Collectors.groupingBy(String::length,Collectors.mapping(String::toUpperCase,Collectors.toSet()))));
|
||||||
|
|
||||||
|
Stream<String> strStream7 = Stream.of("one","two","three","four");
|
||||||
|
System.out.println(strStream7.collect(Collectors.reducing((str1,str2) -> str1 + str2)).orElse("-"));
|
||||||
|
|
||||||
|
System.out.println("***********************");
|
||||||
|
System.out.println("Example of collectingAndThen : ");
|
||||||
|
Stream<String> strStream8 = Stream.of("sunday","monday","tuesday","wednesday");
|
||||||
|
List<String> strList = strStream8.collect(Collectors.collectingAndThen(Collectors.toList(),Collections::unmodifiableList));
|
||||||
|
System.out.println(strList);
|
||||||
|
}
|
||||||
|
}
|
||||||
61
Java/Collectors/CustomCollector.java
Normal file
61
Java/Collectors/CustomCollector.java
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
import java.util.*;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
import java.util.function.BinaryOperator;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
import java.util.stream.Collector;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import static java.util.stream.Collector.Characteristics.IDENTITY_FINISH;
|
||||||
|
|
||||||
|
class Student {
|
||||||
|
int marks;
|
||||||
|
|
||||||
|
Student() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Student(int i) {
|
||||||
|
this.marks = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
Student addMarks(Student s) {
|
||||||
|
return new Student(this.marks = this.marks + s.marks);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class StudentCollector implements Collector<Student, Student, Student> {
|
||||||
|
@Override
|
||||||
|
public Supplier<Student> supplier() {
|
||||||
|
return Student::new;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BiConsumer<Student, Student> accumulator() {
|
||||||
|
return Student::addMarks;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BinaryOperator<Student> combiner() {
|
||||||
|
return Student::addMarks;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Function<Student, Student> finisher() {
|
||||||
|
return Function.identity();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<Characteristics> characteristics() {
|
||||||
|
return EnumSet.of(IDENTITY_FINISH);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class CustomCollector {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Stream<Student> studentStream = Stream.of(new Student(30), new Student(40), new Student(50), new Student(80));
|
||||||
|
System.out.println(studentStream.collect(new StudentCollector()).marks);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user