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 );
}
}
Last Sunday while zapping the channels of my idiot box I chanced upon to watch one of the greatest epic of our times –MAHABHARAT. It did’nt take me much time to recall the waves that it created across the nation- when it was telecasted for the first time.I was too young to comprehend it,yet I used to watch it with paramount interest,sitting next to dad & imitating his stimuli to what character speaks Later in my teens ,unlike most of my friends who used to enjoy watching top tens & horror shows , I was inundated to discover that zee was going to re telecast Mahabharat .This time, I was grown up enough to understand it,so I religiously watched all the episodes with same interest. Watching such an epic in formative years has great influence on one’s approach towards life .Today to me ,Mahabharat isn’t just another epic but it is a thinking process, where the mind is the battleground, THE KURUSHETRA ,where all kind of thoughts hover, the state of mind that we...

Comments
Post a Comment