-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataOperation2.java
More file actions
81 lines (73 loc) · 2.35 KB
/
Copy pathDataOperation2.java
File metadata and controls
81 lines (73 loc) · 2.35 KB
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
package problemStatment.stream;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.io.*;
public class DataOperation2 {
public static Map<String,Integer> processData(ArrayList<String> array) {
List<Emp> empList = array.stream().map(s->toEmp.apply(s)).collect(Collectors.toList());
Map<String,Optional<Emp>> r = empList.stream().collect(
Collectors.groupingBy(Emp::getDepartment,Collectors.maxBy((e1,e2)-> e1.getID().compareTo(e2.getID()))));
Map<String,Integer> retVal = new HashMap<>();
r.forEach((k,v) -> retVal.put(k,v.get().getSalary()));
return retVal;
}
public static void main(String[] args) {
String fileName = "input.txt";
System.out.println(processData(getfileContent(fileName)));
}
private static ArrayList<String> getfileContent(final String fileName) {
URL url = DataOperation2.class.getClassLoader().getResource(fileName);
ArrayList<String> lines = new ArrayList<>(100);
try (Stream<String> stream = Files.lines(Paths.get(url.toURI()))) {
stream.forEach(lines::add);
} catch (IOException | URISyntaxException e) {
e.printStackTrace();
}
return lines;
}
static Function<String,Emp> toEmp = ln -> {
String[] empArr = ln.split(",");
Emp e = new Emp();
e.setID(Integer.valueOf(empArr[0].trim()));
e.setName(empArr[1]);
e.setDepartment(empArr[2]);
e.setSalary(Integer.valueOf(empArr[3].trim()));
return e;
};
}
class Emp{
private Integer ID;
private String Name;
private String Department;
private Integer Salary;
public Integer getID() {
return ID;
}
public void setID(Integer iD) {
ID = iD;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getDepartment() {
return Department;
}
public void setDepartment(String department) {
Department = department;
}
public Integer getSalary() {
return Salary;
}
public void setSalary(Integer salary) {
Salary = salary;
}
}