using java 8 stream to process data in java

Solutions on MaxInterview for using java 8 stream to process data in java by the best coders in the world

showing results for - "using java 8 stream to process data in java"
Sana
09 Oct 2017
1public class Employee {
2
3	private int employeeID;
4	private String employeeName;
5	private String employeeGender;
6	private String employeeCountry;
7	private String employeeState;
8	private String employeeCity;
9	
10	public Employee() {
11		// TODO Auto-generated constructor stub
12	}
13
14	public Employee(int employeeID, String employeeName, String employeeGender, String employeeCountry,
15			String employeeState, String employeeCity) {
16		super();
17		this.employeeID = employeeID;
18		this.employeeName = employeeName;
19		this.employeeGender = employeeGender;
20		this.employeeCountry = employeeCountry;
21		this.employeeState = employeeState;
22		this.employeeCity = employeeCity;
23	}
24
25	public int getEmployeeID() {
26		return employeeID;
27	}
28
29	public void setEmployeeID(int employeeID) {
30		this.employeeID = employeeID;
31	}
32
33	public String getEmployeeName() {
34		return employeeName;
35	}
36
37	public void setEmployeeName(String employeeName) {
38		this.employeeName = employeeName;
39	}
40
41	public String getEmployeeGender() {
42		return employeeGender;
43	}
44
45	public void setEmployeeGender(String employeeGender) {
46		this.employeeGender = employeeGender;
47	}
48
49	public String getEmployeeCountry() {
50		return employeeCountry;
51	}
52
53	public void setEmployeeCountry(String employeeCountry) {
54		this.employeeCountry = employeeCountry;
55	}
56
57	public String getEmployeeState() {
58		return employeeState;
59	}
60
61	public void setEmployeeState(String employeeState) {
62		this.employeeState = employeeState;
63	}
64
65	public String getEmployeeCity() {
66		return employeeCity;
67	}
68
69	public void setEmployeeCity(String employeeCity) {
70		this.employeeCity = employeeCity;
71	}
72
73	@Override
74	public String toString() {
75		// TODO Auto-generated method stub
76		return "[Employee ID : " + employeeID + ", Employee Name : " + employeeName + ", Employee Gender : "
77				+ employeeGender + ", Employee Country : " + employeeCountry + ", Employee State : " + employeeState
78				+ ", Employee City : " + employeeCity + "]";
79	}
80	
81	public static void main(String[] args) {
82		ArrayList<Employee> employees=new ArrayList<Employee>();
83		
84		employees.add(new Employee(101, "John", "M", "United States", "California", "Los Angeles"));
85		employees.add(new Employee(91, "Jacob", "M", "United States", "California", "Los Angeles"));
86		employees.add(new Employee(111, "Lisa", "F", "United States", "California", "Los Angeles"));
87		employees.add(new Employee(97, "Mary", "F", "United States", "California", "Sacramento"));
88		employees.add(new Employee(76, "Christine", "F", "United States", "California", "Sacramento"));
89		employees.add(new Employee(114, "David", "M", "United States", "California", "San Jose"));
90		employees.add(new Employee(103, "Kevin", "M", "United States", "California", "Oakland"));
91		employees.add(new Employee(109, "Joe", "M", "United States", "California", "Oakland"));
92		employees.add(new Employee(119, "Mathew", "M", "United States", "California", "San Jose"));
93		employees.add(new Employee(99, "Angelina", "F", "United States", "California", "San Diego"));
94		employees.add(new Employee(98, "Tom", "M", "United States", "California", "San Diego"));
95		employees.add(new Employee(116, "Curl", "M", "United States", "California", "Los Angeles"));
96		employees.add(new Employee(66, "Christopher", "M", "United States", "California", "Oakland"));
97		employees.add(new Employee(56, "Chelse", "F", "United States", "California", "Oakland"));
98		employees.add(new Employee(88, "Murali", "M", "United States", "California", "San Jose"));
99		employees.add(new Employee(87, "Daisy", "F", "United States", "California", "Sacramento"));
100		employees.add(new Employee(85, "Niza", "F", "United States", "Virginia", "Richmond"));
101		employees.add(new Employee(86, "Chris", "M", "United States", "Virginia", "Fairfax"));
102		employees.add(new Employee(90, "Andrew", "M", "United States", "Virginia", "Reston"));
103		
104	}
105
106}
107
108Operations:
109
1101. Get list of all the employees from "California"; Return a List
1112. Count the number of Females; Return a Count
1123. Add 10 to the ID of each Employee; Return the updated List
1134. Sort in the Descending order by employee name (z-a); Return the List
1145. Get the details of the second highest employee ID; Return the employee
115
116Solution:
117
118System.out.println(employees.stream().filter(employee->employee.getEmployeeState().equals("California")).collect(Collectors.toList()));
119System.out.println(employees.stream().map(emp->emp.getEmployeeID()+10).collect(Collectors.toList()));
120System.out.println(employees.stream().filter(employee->employee.getEmployeeGender().equalsIgnoreCase("f")).count());
121System.out.println(employees.stream().sorted((e1,e2)->e2.getEmployeeName().compareTo(e1.getEmployeeName())).collect(Collectors.toList()));
122Collections.sort(employees, (s1,s2)-> s2.getEmployeeID() - s1.getEmployeeID());
123System.out.println(employees.get(1));