-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStreamInterviewJavaTechies.java
185 lines (153 loc) · 6.86 KB
/
StreamInterviewJavaTechies.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package InterviewProblems;
import java.util.*;
import java.util.stream.Collectors;
public class StreamInterviewJavaTechies {
static List<Employee> employeeList = new ArrayList<Employee>();
public static void main(String[] args) {
employeeList.add(new Employee(111, "Jiya Brein", 32, "Female", "HR", 2011, 25000.0));
employeeList.add(new Employee(122, "Paul Niksui", 25, "Male", "Sales And Marketing", 2015, 13500.0));
employeeList.add(new Employee(133, "Martin Theron", 29, "Male", "Infrastructure", 2012, 18000.0));
employeeList.add(new Employee(144, "Murali Gowda", 28, "Male", "Product Development", 2014, 32500.0));
employeeList.add(new Employee(155, "Nima Roy", 27, "Female", "HR", 2013, 22700.0));
employeeList.add(new Employee(166, "Iqbal Hussain", 43, "Male", "Security And Transport", 2016, 10500.0));
employeeList.add(new Employee(177, "Manu Sharma", 35, "Male", "Account And Finance", 2010, 27000.0));
employeeList.add(new Employee(188, "Wang Liu", 31, "Male", "Product Development", 2015, 34500.0));
employeeList.add(new Employee(199, "Amelia Zoe", 24, "Female", "Sales And Marketing", 2016, 11500.0));
employeeList.add(new Employee(200, "Jaden Dough", 38, "Male", "Security And Transport", 2015, 11000.5));
employeeList.add(new Employee(211, "Jasna Kaur", 27, "Female", "Infrastructure", 2014, 15700.0));
employeeList.add(new Employee(222, "Nitin Joshi", 25, "Male", "Product Development", 2016, 28200.0));
employeeList.add(new Employee(233, "Jyothi Reddy", 27, "Female", "Account And Finance", 2013, 21300.0));
employeeList.add(new Employee(244, "Nicolus Den", 24, "Male", "Sales And Marketing", 2017, 10700.5));
employeeList.add(new Employee(255, "Ali Baig", 23, "Male", "Infrastructure", 2018, 12700.0));
employeeList.add(new Employee(266, "Sanvi Pandey", 26, "Female", "Product Development", 2015, 28900.0));
employeeList.add(new Employee(277, "Anuj Chettiar", 31, "Male", "Product Development", 2012, 35700.0));
//find the total names starts with N
//method0();
System.out.println("\n");
//find the total number of male and female employee
//method1();
System.out.println("\n");
//method2();
System.out.println("\n");
//method3();
System.out.println("\n");
//method4();
System.out.println("\n");
//method5();
System.out.println("\n");
method00();
System.out.println("\n");
}
public static void method0() {
System.out.println("Query 2 : Print the name of all starts with J in the organization?");
employeeList.stream().filter(x->x.getName().startsWith("J")).distinct().forEach(System.out::println);
}
public static void method00() {
System.out.println("Query 5 : Get the names,age of all employees in ascending order");
// Optional<Employee> highestPaidEmployeeWrapper =
// employeeList.stream().collect(Collectors.maxBy(Comparator.comparingInt(Employee::getAge)));
Map<String, Long> noOfNameAgeEmployees = employeeList.stream()
.collect(Collectors.groupingBy(Employee::getGender, Collectors.counting()));
System.out.println(noOfNameAgeEmployees);
}
public static void method1() {
System.out.println("Query 1 : How many male and female employees are there in the organization?");
Map<String, Long> noOfMaleAndFemaleEmployees = employeeList.stream()
.collect(Collectors.groupingBy(Employee::getGender, Collectors.counting()));
System.out.println(noOfMaleAndFemaleEmployees);
}
public static void method2() {
System.out.println("Query 2 : Print the name of all departments in the organization?");
employeeList.stream().map(Employee::getDepartment).distinct().forEach(System.out::println);
// List<String> department = employeeList.stream().map(Employee::getDepartment).distinct().collect(Collectors.toList());
// System.out.println(department);
}
public static void method3() {
System.out.println("Query 3 : What is the average age of male and female employees?");
Map<String,Double> averageAgeFemaleAndMale = employeeList.stream()
.collect(Collectors.groupingBy(Employee::getGender, Collectors.averagingInt(Employee::getAge)));
System.out.println(averageAgeFemaleAndMale);
}
public static void method4() {
System.out.println("Query 4 : Get the details of highest paid employee in the organization?");
Optional<Employee> highestPaidEmployeeWrapper =
employeeList.stream().collect(Collectors.maxBy(Comparator.comparingDouble(Employee::getSalary)));
System.out.println(highestPaidEmployeeWrapper.get().name);
}
public static void method5() {
System.out.println("Query 5 : Get the names of all employees who have joined after 2015?");
employeeList.stream().filter(s-> s.yearOfJoining > 2015).map(Employee::getName).forEach(System.out::println);
}
}
class Employee{
int id;
String name;
int age;
String gender;
String department;
int yearOfJoining;
double salary;
public Employee(int id, String name, int age, String gender, String department, int yearOfJoining, double salary) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.department = department;
this.yearOfJoining = yearOfJoining;
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public int getYearOfJoining() {
return yearOfJoining;
}
public void setYearOfJoining(int yearOfJoining) {
this.yearOfJoining = yearOfJoining;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
", department='" + department + '\'' +
", yearOfJoining=" + yearOfJoining +
", salary=" + salary +
'}';
}
}