Skip to content

Commit d4b5792

Browse files
committed
stream
stream
1 parent 28bcd4e commit d4b5792

10 files changed

Lines changed: 256 additions & 0 deletions

File tree

00-Fundamental-Practice/.idea/.gitignore

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

00-Fundamental-Practice/.idea/00-Fundamental-Practice.iml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

00-Fundamental-Practice/.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

00-Fundamental-Practice/.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

00-Fundamental-Practice/.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

00-Fundamental-Practice/Streams/Notes.txt

Whitespace-only changes.
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package demoApplication;
2+
3+
import java.util.List;
4+
import java.util.Arrays;
5+
import java.util.Map;
6+
import java.util.Set;
7+
import java.util.stream.Collectors;
8+
9+
public class DemoApplication {
10+
11+
static List<Employee> employeeList = Arrays.asList(
12+
13+
new Employee(1,"Abraham",32,"IT","Mumbai",20000,"Male"),
14+
new Employee(2,"Mary",21,"Sales","Chennai",25000,"Female"),
15+
new Employee(3,"Joe",32,"IT","Chennai",22000,"Male"),
16+
new Employee(4,"John",27,"Sales","Gurgaon",29000,"Male"),
17+
new Employee(5,"Liza",54,"Sales","Bangalore",32000,"Female"),
18+
new Employee(6,"Peter",33,"Admin","Mumbai",31500,"Male"),
19+
new Employee(7,"Harry",35,"Research","Kochi",21000,"Male"),
20+
new Employee(5,"Romit",25,"Research","Kanpur",21000,"Male")
21+
);
22+
23+
public static void main(String[] args) {
24+
System.out.println(" Here are the solution of the 15 questions : ");
25+
26+
//1.Find the list of employees whose name starts with "A".
27+
28+
List<Employee> employeeWhoseNameStartsWithA = employeeList.stream().filter(employee -> employee.getName().startsWith("A")).collect(Collectors.toList());
29+
30+
System.out.println("Employees whose name starts with A \n" +employeeWhoseNameStartsWithA);
31+
32+
33+
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
34+
35+
36+
//2.Group the employees by Department names.
37+
38+
Map<String, List<Employee>> employeesWithDeptName = employeeList.stream().collect(Collectors.groupingBy(Employee::getDapartName));
39+
40+
System.out.println("List of employees with department names ");
41+
employeesWithDeptName.forEach((dept,empList) -> {
42+
System.out.println(dept);
43+
empList.forEach(emp -> {
44+
System.out.println(emp.getId());;
45+
System.out.println(emp.getName());;
46+
System.out.println(emp.getAge());;
47+
System.out.println(emp.getDapartName());;
48+
System.out.println( emp.getAddress());;
49+
System.out.println(emp.getSalary());
50+
System.out.println(emp.getGender());
51+
});
52+
53+
System.out.println("-----------------------------------------------------------------------------");
54+
});
55+
56+
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
57+
58+
59+
//3.Find the total count of employees using stream.
60+
61+
long count = employeeList.stream().count();
62+
System.out.println("Total number of employees : " + count);
63+
64+
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
65+
66+
//4.Find the max age of employees.
67+
68+
int maxAge = employeeList.stream().mapToInt(Employee::getAge).max().getAsInt();
69+
System.out.println("Max Age : " + maxAge);
70+
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
71+
72+
//5.Find all department names.
73+
Set<String> allDeptNames = employeeList.stream().map(Employee::getDapartName).collect(Collectors.toSet());
74+
System.out.println("All department names : " +allDeptNames);
75+
76+
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
77+
//6.Find the count of employees in each department.
78+
Map<String, Long> employeeCountByDept = employeeList.stream().collect(Collectors.groupingBy(Employee::getDapartName, Collectors.counting()));
79+
System.out.println("Employee count by department");
80+
employeeCountByDept.forEach((dept,value) ->{
81+
System.out.println(dept +" : "+ value);
82+
} );
83+
84+
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
85+
//7.Find the list of employees whose age is less than 30.
86+
Set<Employee> ageLessThan30 = employeeList.stream().filter(employee -> employee.getAge() < 30).collect(Collectors.toSet());
87+
88+
System.out.println("Employees having age less than 30"+ageLessThan30);
89+
90+
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
91+
//8.Find the list of employees whose age is between 26 and 31
92+
93+
List<Employee> ageBetween26and31 = employeeList.stream().filter(employee -> employee.getAge() < 31 && employee.getAge() > 26).collect(Collectors.toList());
94+
System.out.println("Employees having age between 26 and 31"+ageBetween26and31);
95+
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
96+
//9.Find the average age of male and female employees.
97+
Map<String, Double> avgAgeOfMaleAndFemale = employeeList.stream().collect(Collectors.groupingBy(Employee::getGender, Collectors.averagingDouble(Employee::getAge)));
98+
System.out.println("Average age of Male and Female employees :");
99+
avgAgeOfMaleAndFemale.forEach((gender,age) ->{
100+
System.out.println(gender +" : "+ age );
101+
});
102+
103+
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
104+
//10.Find the department who is having the maximum number of employees.
105+
Map.Entry<String, Long> deptMaxEmp = employeeList.stream().collect(Collectors.groupingBy(Employee::getDapartName, Collectors.counting())).entrySet().stream().max(Map.Entry.comparingByValue()).get();
106+
107+
System.out.println("Department having maximum number of employees is \n"+ deptMaxEmp.getKey() +" \nand the employees number is "+deptMaxEmp.getValue());
108+
109+
110+
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
111+
//11.Find the Employee who stays in Delhi and sort them by their names.
112+
113+
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
114+
115+
//12.Find the average salary in all department.
116+
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
117+
//13.Find the highest salary in each department.
118+
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
119+
//14.Find the list of employee and sort them by their salary.
120+
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
121+
//15.Find the employee who as second highest salary.
122+
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
123+
124+
125+
126+
}
127+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package demoApplication;
2+
3+
public class Employee{
4+
private int id;
5+
private String name;
6+
private int age;
7+
private String dapartName;
8+
private String address;
9+
private double salary;
10+
private String gender;
11+
12+
public Employee(int id, String name, int age, String dapartName, String address, double salary, String gender) {
13+
this.id = id;
14+
this.name = name;
15+
this.age = age;
16+
this.dapartName = dapartName;
17+
this.address = address;
18+
this.salary = salary;
19+
this.gender = gender;
20+
}
21+
22+
@java.lang.Override
23+
public java.lang.String toString() {
24+
return "Employee{" +
25+
"id=" + id +
26+
", name='" + name + '\'' +
27+
", age=" + age +
28+
", dapartName='" + dapartName + '\'' +
29+
", address='" + address + '\'' +
30+
", salary=" + salary +
31+
", gender='" + gender + '\'' +
32+
'}';
33+
}
34+
35+
public Employee() {
36+
}
37+
38+
public int getId() {
39+
return id;
40+
}
41+
42+
public void setId(int id) {
43+
this.id = id;
44+
}
45+
46+
public String getName() {
47+
return name;
48+
}
49+
50+
public void setName(String name) {
51+
this.name = name;
52+
}
53+
54+
public int getAge() {
55+
return age;
56+
}
57+
58+
public void setAge(int age) {
59+
this.age = age;
60+
}
61+
62+
public String getDapartName() {
63+
return dapartName;
64+
}
65+
66+
public void setDapartName(String dapartName) {
67+
this.dapartName = dapartName;
68+
}
69+
70+
public String getAddress() {
71+
return address;
72+
}
73+
74+
public void setAddress(String address) {
75+
this.address = address;
76+
}
77+
78+
public double getSalary() {
79+
return salary;
80+
}
81+
82+
public void setSalary(double salary) {
83+
this.salary = salary;
84+
}
85+
86+
public String getGender() {
87+
return gender;
88+
}
89+
90+
public void setGender(String gender) {
91+
this.gender = gender;
92+
}
93+
}
Binary file not shown.

0 commit comments

Comments
 (0)