Streams API in Java8 provides good number of Stream operations to operate on input provided. These operationsreduce lot of code to be written and increases readability.Some of the most important functions are described below with examples
- Collect(toList()) :- is an eager operation which generates list from a given stream
Example :- Below example describes the use of collect operation which gets the list of skills from employees
Note :- In above example @ 1 the out put is printed as below this is due to call of eager function count(...)
@ 2 there is no eager function called so no output is printed when executed.
In next post we will discuss about flatMap,reduce,sort etc. Stream operations
Happy learning
/**
package com.test;
import java.util.ArrayList;
/**
* @author sudheer
*
*/
public class Tester {
public Employee createEmployee(int employeeId, float experience, String firstName, String lastName, String skill) {
Employee emp = new Employee();
emp.setEmployeeId(employeeId);
emp.setExperience(experience);
emp.setFirstName(firstName);
emp.setLastName(lastName);
emp.setSkill(skill);
return emp;
}
public static void main(String[] args) {
Tester test = new Tester();
List<Employee> employees = new ArrayList<Employee>();
employees.add(test.createEmployee(1, 5, "Sudheer", "Reddy", "Java"));
employees.add(test.createEmployee(2, 4, "Keerthi", "Reddy", "Java"));
employees.add(test.createEmployee(3, 7, "Loknath", "Reddy", ".Net"));
employees.add(test.createEmployee(4, 5, "Satish", "Reddy", "C++"));
// Applying eager operation count
employees.stream().filter(employee -> {
System.out.println("Employee skill->" + employee.getSkill());
return "Java".equals(employee.getSkill());
}).count();
// Lazy operations .output is not printed on console ,because traversal
// of the filtered stream is not done until eager operation is called
employees.stream().filter(employee -> {
System.out.println("Employee skill->" + employee.getSkill());
return "Java".equals(employee.getSkill());
});
}
}
- map :-
- used to replace values in a stream with new values like converting stream of lowercase letter array to uppercase letter array
- Returns a stream consisting of the results of applying the given function to the elements of this stream. This is an intermediate operation.
- its a lazy operation which will return output as Stream object.
Example :- In the above example map is used to get/convert stream of employees into an intermediate stream of Skills
List<String> list = employees.stream().map(Employee::getSkill).collect(Collectors.toList());
- Filter :-
Example :- Below example describes the use of collect operation which gets the list of skills from employees
package com.test;
import java.util.ArrayList;
/**
* @author sudheer
*
*/
public class Tester {
public Employee createEmployee(int employeeId, float experience, String firstName, String lastName, String skill) {
Employee emp = new Employee();
emp.setEmployeeId(employeeId);
emp.setExperience(experience);
emp.setFirstName(firstName);
emp.setLastName(lastName);
emp.setSkill(skill);
return emp;
}
public static void main(String[] args) {
Tester test = new Tester();
List<Employee> employees = new ArrayList<Employee>();
employees.add(test.createEmployee(1, 5, "Sudheer", "Reddy", "Java"));
employees.add(test.createEmployee(2, 4, "Keerthi", "Reddy", "Java"));
employees.add(test.createEmployee(3, 7, "Loknath", "Reddy", ".Net"));
employees.add(test.createEmployee(4, 5, "Satish", "Reddy", "C++"));
// Applying eager operation count
employees.stream().filter(employee -> {
System.out.println("Employee skill->" + employee.getSkill());
return "Java".equals(employee.getSkill());
}).count();
// Lazy operations .output is not printed on console ,because traversal
// of the filtered stream is not done until eager operation is called
employees.stream().filter(employee -> {
System.out.println("Employee skill->" + employee.getSkill());
return "Java".equals(employee.getSkill());
});
}
}
Note :- In above example @ 1 the out put is printed as below this is due to call of eager function count(...)
Employee skill->Java
Employee skill->Java
Employee skill->.Net
Employee skill->C++
In next post we will discuss about flatMap,reduce,sort etc. Stream operations
Happy learning
No comments:
Post a Comment