-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionalProgramming.java
More file actions
68 lines (58 loc) · 1.87 KB
/
FunctionalProgramming.java
File metadata and controls
68 lines (58 loc) · 1.87 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
import java.util.*;
import java.util.stream.*;
//class
class Employees{
int id;
String Name;
String department;
int Salary;
//constructors
Employees(int id , String Name, String department, int Salary){
this.id = id;
this.Name = Name;
this.department = department;
this.Salary = Salary;
}
public String toString(){
return id + " " + Name + " " + department + " "+ Salary;
}
}
//main function
public class FunctionalProgramming{
public static void main(String[] args){
List<Employees>employees = Arrays.asList(
new Employees(1,"Renuka","IT",100000),
new Employees(2,"Shiva","IT", 500000),
new Employees(3,"Kamala","HR",70000),
new Employees(4,"Karthik","Finance",600000)
);
// filter //Stream
employees.stream()
.filter(e->e.department.equals("IT"))
.forEach(System.out::println);
//Sort byy salary
employees.stream()
.sorted((e1,e2)->Double.compare(e1.Salary,e2.Salary))
//method refernce
.forEach(System.out::println);
//map:transforms names to employees
employees.stream()
.map(e->e.Name.toUpperCase())
.forEach(System.out::println);
//reduce
int totalsalary = employees.stream()
.map(e->e.Salary)
.reduce(0,(a,b)->a+b);
System.out.println("total salary" +totalsalary);
//collect
List<Employees> itEmployees = employees.stream()
.filter(e -> e.department.equals("IT"))
.collect(Collectors.toList());
System.out.println("IT Employees: " + itEmployees);
}
}
// we can aslo perfrom at a time like
//classname stream()
//.filter(condition)
//.map()
//.sorted()