Skip to main content

Java 8 Collectors at a Go







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 );
}


}












Comments

Popular posts from this blog

The BattleGround

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

WHERE THE MIND IS WITHOUT FEAR !

(Painting By Takshila Vidushi) "Where the mind is without fear & the head is held high Where knowledge is free Where the world hasn't been broken up into fragments By narrow domestic walls Where words come out from the depth of truth Where tireless striving stretches its arms towards perfection ! Where the clear stream of reason has not lost its way Into the dreary desert sand of dead habit Where the mind is led forward by thee Into ever-widening thought and action Into that heaven of freedom, my Father, let my country awake." By Guru RabindraNath Tagore (Ever since childhood this poem of RabindraNath Tagore has been synonmous to supreme level of perfection for me.Its incredibl...

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