Skip to content

Commit b2b4333

Browse files
committed
Add Employee class and RemoveDuplicatesBasedOnName class to manage employee data and remove duplicates based on department
1 parent 77855c6 commit b2b4333

3 files changed

Lines changed: 590 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.topfifty;
2+
3+
public class Employee {
4+
int id;
5+
String name;
6+
String department;
7+
8+
@Override
9+
public String toString() {
10+
return "Employee{" +
11+
"id=" + id +
12+
", name='" + name + '\'' +
13+
", department='" + department + '\'' +
14+
'}';
15+
}
16+
17+
public int getId() {
18+
return id;
19+
}
20+
21+
public void setId(int id) {
22+
this.id = id;
23+
}
24+
25+
public String getName() {
26+
return name;
27+
}
28+
29+
public void setName(String name) {
30+
this.name = name;
31+
}
32+
33+
public String getDepartment() {
34+
return department;
35+
}
36+
37+
public void setDepartment(String department) {
38+
this.department = department;
39+
}
40+
41+
public Employee(int id, String name, String department) {
42+
this.id = id;
43+
this.name = name;
44+
this.department = department;
45+
}
46+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.topfifty;
2+
3+
import java.util.Arrays;
4+
import java.util.Collection;
5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.stream.Collectors;
8+
9+
public class RemoveDuplicatesBasedOnName {
10+
public static void main(String[] args) {
11+
List<Employee> employees = Arrays.asList(
12+
new Employee(1, "Alice", "Engineering"),
13+
new Employee(2, "Bob", "Sales"),
14+
new Employee(3, "Charlie", "Engineering"), // same department as Alice
15+
new Employee(4, "David", "Sales"), // same department as Bob
16+
new Employee(5, "Eve", "HR")
17+
);
18+
List<Employee> result = employees.stream().collect(Collectors.groupingBy(Employee::getDepartment)).values().stream()
19+
.map(x -> x.get(0)).toList();
20+
System.out.println(result);
21+
Collection<Employee> resultList = employees.stream()
22+
.collect(Collectors.toMap(Employee::getDepartment, e -> e, (x, y) -> x))
23+
.values();
24+
System.out.println(resultList);
25+
}
26+
}

0 commit comments

Comments
 (0)