Skip to content

Commit 40b3105

Browse files
authored
map-reduce
1 parent c22cf6b commit 40b3105

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

map_reduce/Employee.java

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.javatechie.map_reduce;
2+
3+
public class Employee {
4+
private int id;
5+
private String name;
6+
private String grade;
7+
private double salary;
8+
9+
public Employee() {
10+
}
11+
12+
public Employee(int id, String name, String grade, double salary) {
13+
this.id = id;
14+
this.name = name;
15+
this.grade = grade;
16+
this.salary = salary;
17+
}
18+
19+
public int getId() {
20+
return id;
21+
}
22+
23+
public void setId(int id) {
24+
this.id = id;
25+
}
26+
27+
public String getName() {
28+
return name;
29+
}
30+
31+
public void setName(String name) {
32+
this.name = name;
33+
}
34+
35+
public String getGrade() {
36+
return grade;
37+
}
38+
39+
public void setGrade(String grade) {
40+
this.grade = grade;
41+
}
42+
43+
public double getSalary() {
44+
return salary;
45+
}
46+
47+
public void setSalary(double salary) {
48+
this.salary = salary;
49+
}
50+
51+
52+
}

map_reduce/EmployeeDatabase.java

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.javatechie.map_reduce;
2+
3+
import java.util.List;
4+
import java.util.stream.Collectors;
5+
import java.util.stream.Stream;
6+
7+
public class EmployeeDatabase {
8+
9+
10+
public static List<Employee> getEmployees(){
11+
return Stream.of(new Employee(101,"john","A",60000),
12+
new Employee(109,"peter","B",30000),
13+
new Employee(102,"mak","A",80000),
14+
new Employee(103,"kim","A",90000),
15+
new Employee(104,"json","C",15000))
16+
.collect(Collectors.toList());
17+
}
18+
}

map_reduce/MapReduceExample.java

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.javatechie.map_reduce;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.Optional;
6+
7+
public class MapReduceExample {
8+
9+
public static void main(String[] args) {
10+
11+
List<Integer> numbers = Arrays.asList(3, 7, 8, 1, 5, 9);
12+
13+
List<String> words = Arrays.asList("corejava", "spring", "hibernate");
14+
15+
int sum = 0;
16+
for (int no : numbers) {
17+
sum = sum + no;
18+
}
19+
System.out.println(sum);
20+
21+
int sum1 = numbers.stream().mapToInt(i -> i).sum();
22+
System.out.println(sum1);
23+
24+
Integer reduceSum = numbers.stream().reduce(0, (a, b) -> a + b);
25+
System.out.println(reduceSum);
26+
27+
Optional<Integer> reduceSumWithMethodReference = numbers.stream().reduce(Integer::sum);
28+
System.out.println(reduceSumWithMethodReference.get());
29+
30+
Integer mulResult = numbers.stream().reduce(1, (a, b) -> a * b);
31+
System.out.println(mulResult);
32+
33+
Integer maxvalue = numbers.stream().reduce(0, (a, b) -> a > b ? a : b);
34+
System.out.println(maxvalue);
35+
36+
Integer maxvalueWithMethodReference = numbers.stream().reduce(Integer::max).get();
37+
System.out.println(maxvalueWithMethodReference);
38+
39+
40+
String longestString = words.stream()
41+
.reduce((word1, word2) -> word1.length() > word2.length() ? word1 : word2)
42+
.get();
43+
System.out.println(longestString);
44+
45+
//get employee whose grade A
46+
//get salary
47+
double avgSalary = EmployeeDatabase.getEmployees().stream()
48+
.filter(employee -> employee.getGrade().equalsIgnoreCase("A"))
49+
.map(employee -> employee.getSalary())
50+
.mapToDouble(i -> i)
51+
.average().getAsDouble();
52+
53+
System.out.println(avgSalary);
54+
55+
double sumSalary = EmployeeDatabase.getEmployees().stream()
56+
.filter(employee -> employee.getGrade().equalsIgnoreCase("A"))
57+
.map(employee -> employee.getSalary())
58+
.mapToDouble(i -> i)
59+
.sum();
60+
System.out.println(sumSalary);
61+
}
62+
63+
64+
}

0 commit comments

Comments
 (0)