JAVA 8 :COLLECTORS
AVERAGING
public staticCollector averagingInt(ToIntFunction mapper)
Double avgAge = residents .stream() .collect(Collectors.averagingInt(Person::getAge)); }
public staticCollector averagingLong(ToLongFunction mapper)
Double avgAge = residents .stream() .collect(Collectors.averagingLong(Person::getAge)); }
public staticCollector 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
MapemployeeMap = employeeList.stream() .collect(Collectors.groupingBy(Employee::getDepartment, Collectors.toSet()));
PARTITIONING BY PREDICATE
MapemployeeMap = 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
MapCOLLECTORS/MINBYemployeeMapCount = employeeList.stream() .collect(Collectors.partitioningBy( (Employee emp) -> (emp.getAge() > 30), Collectors.counting() ));
COLLECTORS/MAXBYOptional minAgeEmp = employeeList.stream() .collect(Collectors.minBy(Comparator.comparing(Employee::getAge)));
COLLECTORS/JOININGOptional minAgeEmp = employeeList.stream() .collect(Collectors.maxBy(Comparator.comparing(Employee::getAge)));
Stream.iterate(new Integer(0), (Integer integer) -> integer + 1) .limit(5) .map(integer -> integer.toString()) .collect(Collectors.joining());
COLLECTORS/SUMMARIZING(INT/LONG/DOUBLE)
IntSummaryStatistics intSummaryStatistics = employeeList .stream() .collect(Collectors.summarizingInt(Employee::getAge));
COLLECTION ENHANCEMENTS
COLLECTION ENHANCEMENTS
IntSummaryStatistics intSummaryStatistics = employeeList .stream() .collect(Collectors.summarizingInt(Employee::getAge));
No comments:
Post a Comment