forked from Java-Techie-jt/java8
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapReduceExample.java
More file actions
64 lines (45 loc) · 2.12 KB
/
MapReduceExample.java
File metadata and controls
64 lines (45 loc) · 2.12 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
package com.javatechie.map_reduce;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
public class MapReduceExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(3, 7, 8, 1, 5, 9);
List<String> words = Arrays.asList("corejava", "spring", "hibernate");
int sum = 0;
for (int no : numbers) {
sum = sum + no;
}
System.out.println(sum);
int sum1 = numbers.stream().mapToInt(i -> i).sum();
System.out.println(sum1);
Integer reduceSum = numbers.stream().reduce(0, (a, b) -> a + b);
System.out.println(reduceSum);
Optional<Integer> reduceSumWithMethodReference = numbers.stream().reduce(Integer::sum);
System.out.println(reduceSumWithMethodReference.get());
Integer mulResult = numbers.stream().reduce(1, (a, b) -> a * b);
System.out.println(mulResult);
Integer maxvalue = numbers.stream().reduce(0, (a, b) -> a > b ? a : b);
System.out.println(maxvalue);
Integer maxvalueWithMethodReference = numbers.stream().reduce(Integer::max).get();
System.out.println(maxvalueWithMethodReference);
String longestString = words.stream()
.reduce((word1, word2) -> word1.length() > word2.length() ? word1 : word2)
.get();
System.out.println(longestString);
//get employee whose grade A
//get salary
double avgSalary = EmployeeDatabase.getEmployees().stream()
.filter(employee -> employee.getGrade().equalsIgnoreCase("A"))
.map(employee -> employee.getSalary())
.mapToDouble(i -> i)
.average().getAsDouble();
System.out.println(avgSalary);
double sumSalary = EmployeeDatabase.getEmployees().stream()
.filter(employee -> employee.getGrade().equalsIgnoreCase("A"))
.map(employee -> employee.getSalary())
.mapToDouble(i -> i)
.sum();
System.out.println(sumSalary);
}
}