Skip to main content

Java 8 :Collectors


JAVA 8 :COLLECTORS

AVERAGING

public static  Collector averagingInt(ToIntFunction mapper)

Double avgAge = residents
        .stream()
        .collect(Collectors.averagingInt(Person::getAge));
}

public static  Collector averagingLong(ToLongFunction mapper)

Double avgAge = residents
        .stream()
        .collect(Collectors.averagingLong(Person::getAge));
}

public static  Collector averagingDouble(ToDoubleFunction mapper)

Double avgAge = residents
        .stream()
        .collect(Collectors.averagingDouble(Person::getAge));
}

GROUPING
 Map employeeMap
        = employeeList.stream().collect(Collectors.groupingBy(Employee::getDepartment));



GROUPING BY SPECIFYING COLLECTOR
Map employeeMap
    = employeeList.stream()
      .collect(Collectors.groupingBy(Employee::getDepartment, Collectors.toSet()));


PARTITIONING  BY PREDICATE
 Map employeeMap
        = employeeList
          .stream()
          .collect(Collectors.partitioningBy((Employee emp) -> emp.getAge() > 30));
    System.out.println("Employees partitioned based on Predicate - 'age > 30'");


PARTITIONING  BY COLLECTOR AS SECOND PARAMETER
Map employeeMapCount =
     employeeList.stream()
         .collect(Collectors.partitioningBy(
             (Employee emp) -> (emp.getAge() > 30),
             Collectors.counting()
         ));

COLLECTORS/MINBY

Optional minAgeEmp =    
            employeeList.stream()
            .collect(Collectors.minBy(Comparator.comparing(Employee::getAge)));
COLLECTORS/MAXBY

Optional minAgeEmp =    
            employeeList.stream()
            .collect(Collectors.maxBy(Comparator.comparing(Employee::getAge)));
COLLECTORS/JOINING

 Stream.iterate(new Integer(0), (Integer integer) -> integer + 1)
              .limit(5)
              .map(integer -> integer.toString())
              .collect(Collectors.joining());

COLLECTORS/SUMMARIZING(INT/LONG/DOUBLE)
The statistical attributes encapsulated by the summary statistics classes are – count of numeric value derived sum of values minimum value maximum value average of all values

IntSummaryStatistics intSummaryStatistics = employeeList
        .stream()
        .collect(Collectors.summarizingInt(Employee::getAge));

COLLECTION ENHANCEMENTS 
COLLECTION ENHANCEMENTS 


IntSummaryStatistics intSummaryStatistics = employeeList
        .stream()
        .collect(Collectors.summarizingInt(Employee::getAge));

Comments

Popular posts from this blog

Microservices Design patterns

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...

GraphQL

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

Jackson

<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId> jackson-core </artifactId> <version>2.9.6</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId> jackson-annotations </artifactId> <version>2.9.6</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId> jackson-databind </artifactId> <version>2.9.6</version> </dependency> CBOR encoded data with Jackson <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-cbor</artifactId> <version>2.9.6</version> </dependency> In order to read and write MessagePack encoded data <dependency> <groupId>org.msgpack</groupId> <artifactId>jackson-dataformat-msgp...