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 are microservices? Microservices - also known as the microservice architecture - is an architectural style that structures an application as a collection of services that are Highly maintainable and testable Loosely coupled Independently deployable Organized around business capabilities Owned by a small team The microservice architecture enables the rapid, frequent and reliable delivery of large, complex applications. It also enables an organization to evolve its technology stack. You are developing a server-side enterprise application. It must support a variety of different clients including desktop browsers, mobile browsers and native mobile applications. The application might also expose an API for 3rd parties to consume. It might also integrate with other applications via either web services or a message broker. The application handles requests (HTTP requests and messages) by executing business logic; accessing a database; exchanging messages with other systems; and returni...

Comments
Post a Comment