package collectors;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class CollectorsExamples {
public static void main(String args[]){
List<Employee> employeeList = Employee.getEmployeesSalary ();
totalAgeOfEmployees(employeeList);
getMinAgeEmployees(employeeList );
getMaxAgeEmployees(employeeList);
getNamesInUpperCase(employeeList);
createEmployeeMap(employeeList );
createEmployeeNameString(employeeList );
createEmployeeMap2(employeeList );
partitioningByExample(employeeList );
groupinggByExample(employeeList );
groupinggByExample(employeeList );
grouping_mapping_By_Example(employeeList );
grouping_mapping_collectingAndThen_By_Example(employeeList );
countNames( employeeList );
grouping_counting_By_Example( employeeList );
grouping_counting_By_Example2( employeeList );
collectorByJoining_Example();
}
public static void totalAgeOfEmployees(List<Employee> employeeList ) {
System.out.println (" Total salary is :"+
employeeList
.stream ()
.map ( n-> n.getAge () )
.reduce ( 0,Integer::sum));
}
public static void getMinAgeEmployees(List<Employee> employeeList ) {
System.out.println (" Total salary is :"+employeeList.stream ()
.collect ( Collectors.minBy (
java.util.Comparator.comparing (Employee::getAge)) ));
}
public static void getMaxAgeEmployees(List<Employee> employeeList ) {
employeeList.stream ()
.collect ( Collectors.collectingAndThen (
Collectors.maxBy ( java.util.Comparator.comparing ( Employee::getAge ) ),
emp->emp.map ( Employee::getName ).orElse ( "" ) ));
}
public static void getMaxAgeEmployees1(List<Employee> employeeList ) {
employeeList.stream ()
.collect ( Collectors.maxBy (
java.util.Comparator.comparing (Employee::getAge)) );
}
public static void getNamesInUpperCase(List<Employee> employeeList ) {
employeeList
.stream ()
.map ( n-> n.getName() )
.map(String ::toUpperCase)
.collect ( Collectors.toList () ).forEach ( System.out::println );
}
public static void createEmployeeMap(List<Employee> employeeList ) {
Map<String,Employee> employeeMap =
employeeList
.stream ().collect ( java.util.stream.Collectors
.toMap ( Employee::getName, java.util.function.Function.identity () ) );
}
public static void createEmployeeNameString(List<Employee> employeeList ) {
String employeeNameString =
employeeList
.stream ().map ( Employee::getName )
.collect ( Collectors.joining ( "," ) );
}
public static void createEmployeeMap2(List<Employee> employeeList ) {
Map<String,Employee> employeeMap =
employeeList
.stream ()
.collect ( java.util.stream.Collectors
.toMap ( Employee::getName, java.util.function.Function.identity () ) );
}
public static void partitioningByExample(List<Employee> employeeList ) {
Map < Boolean, List < collectors.Employee > > employeeMap = employeeList
.stream ()
.collect ( Collectors.partitioningBy ( n->n.getSalary ()>40000 ) );
}
public static void groupinggByExample(List<Employee> employeeList ) {
Map < String, List < Employee > > employeeMap = employeeList
.stream ()
.collect ( Collectors.groupingBy ( n->n.getName () ) );
}
public static void grouping_mapping_By_Example(List<Employee> employeeList ) {
Map < Object, java.util.List < Object > > employeeMap = employeeList
.stream ()
.collect ( java.util.stream.Collectors.groupingBy ( n->n.getName () ,
java.util.stream.Collectors.mapping( n ->n.getAge (),
Collectors.toList ())) );
}
public static void grouping_mapping_collectingAndThen_By_Example(List<Employee> employeeList ) {
Map < Object, java.util.List < Object > > employeeMap = employeeList
.stream ()
.collect ( java.util.stream.Collectors.groupingBy ( n->n.getName () ,
Collectors.collectingAndThen(Collectors.mapping( n ->n.getAge (),
Collectors.toList ()),l -> l.stream ().map ( n->n*2 )
.collect(java.util.stream.Collectors.toList()))) );
}
public static void countNames(List<Employee> employeeList ) {
Map < Object, Long > employeeMap = employeeList
.stream ()
.collect ( Collectors.groupingBy ( Employee::getName, Collectors.counting() ) );
}
public static void grouping_counting_By_Example(List<Employee> employeeList ) {
Map < Object, Integer > employeeMap = employeeList
.stream ()
.collect ( Collectors.groupingBy ( Employee::getName,
Collectors.collectingAndThen(Collectors.counting() ,value ->value.intValue ())));
}
public static void grouping_counting_By_Example2(List<Employee> employeeList ) {
Map < Object, Integer > employeeMap = employeeList
.stream ()
.collect ( Collectors.groupingBy ( Employee::getName,
Collectors.collectingAndThen(Collectors.counting() ,Long :: intValue)));
}
public static void collectorByJoining_Example() {
int k = 2;
final int max = 35;
char[] charArray = java.util.stream.IntStream.rangeClosed(0, max)
.mapToObj ( n->String.valueOf (n) )
.collect ( Collectors.joining())
.toCharArray ();
System.out.println ( charArray.length ); int count = 0;
for(char ch : charArray){
if(ch == '2'){
count++;
}
}
System.out.println ( count );
}
}
What is GraphQL API Standard invented & open-sourced by Facebook Alternative to REST API enables declarative data fetching exposes single endpoint & responds to queries How it works? Why Graphql? Improvises performance by reducing the data that is to be transferred over the internet Variety of different frontend frameworks and platforms on client-side Fast development speed & expectation for rapid feature development Why Graphql is better than REST? Flexibility & efficient No more over /under fetching of data Over fetching : Under fetching: Insightful analytics Schema serves as contract between client and server CORE CONCEPTS : SDL :SCHEMA DEFINITION LANGUAGE Writing Data with mutations 3 kinds of mutations creating new data updating existing data deleting existing data

Comments
Post a Comment