Skip to content

Commit 0a5ebe5

Browse files
authored
java 8 stream
1 parent f073aa8 commit 0a5ebe5

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-0
lines changed

demo/ForEachDemo.java

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.javatechie.stream.demo;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.function.Consumer;
8+
9+
public class ForEachDemo {
10+
11+
// filter----> conditional check
12+
13+
public static void main(String[] args) {
14+
15+
List<String> list = new ArrayList<>();
16+
list.add("Murrit");
17+
list.add("john");
18+
list.add("piter");
19+
list.add("marek");
20+
list.add("mac");
21+
22+
for (String s : list) {
23+
if (s.startsWith("m")) {
24+
System.out.println(s);
25+
}
26+
}
27+
28+
list.stream().filter(t -> t.startsWith("m")).forEach(t -> System.out.println(t));
29+
30+
Map<Integer, String> map = new HashMap<>();
31+
32+
map.put(1, "a");
33+
map.put(2, "b");
34+
map.put(3, "c");
35+
map.put(4, "d");
36+
37+
/*
38+
* map.forEach((key,value)->System.out.println(key+": "+value));*/
39+
40+
map.entrySet().stream().filter(k->k.getKey()%2==0).forEach(obj->System.out.println(obj));
41+
42+
43+
/*
44+
* Consumer<String> consumer=(t)->System.out.println(t); for(String s1:list) {
45+
* consumer.accept(s1); }
46+
*/
47+
48+
}
49+
}

example/DataBase.java

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.javatechie.stream.api.example;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
//DAO layer
6+
public class DataBase {
7+
8+
public static List<Employee> getEmployees() {
9+
List<Employee> list = new ArrayList<>();
10+
list.add(new Employee(176, "Roshan", "IT", 600000));
11+
list.add(new Employee(388, "Bikash", "CIVIL", 900000));
12+
list.add(new Employee(470, "Bimal", "DEFENCE", 500000));
13+
list.add(new Employee(624, "Sourav", "CORE", 400000));
14+
list.add(new Employee(176, "Prakash", "SOCIAL", 1200000));
15+
return list;
16+
}
17+
18+
}

example/Employee.java

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.javatechie.stream.api.example;
2+
3+
public class Employee {
4+
5+
private int id;
6+
private String name;
7+
private String dept;
8+
private long salary;
9+
10+
public int getId() {
11+
return id;
12+
}
13+
14+
public void setId(int id) {
15+
this.id = id;
16+
}
17+
18+
public String getName() {
19+
return name;
20+
}
21+
22+
public void setName(String name) {
23+
this.name = name;
24+
}
25+
26+
public String getDept() {
27+
return dept;
28+
}
29+
30+
public void setDept(String dept) {
31+
this.dept = dept;
32+
}
33+
34+
public long getSalary() {
35+
return salary;
36+
}
37+
38+
public void setSalary(long salary) {
39+
this.salary = salary;
40+
}
41+
42+
public Employee(int id, String name, String dept, long salary) {
43+
super();
44+
this.id = id;
45+
this.name = name;
46+
this.dept = dept;
47+
this.salary = salary;
48+
}
49+
50+
public Employee() {
51+
super();
52+
// TODO Auto-generated constructor stub
53+
}
54+
55+
@Override
56+
public String toString() {
57+
return "Employee [id=" + id + ", name=" + name + ", dept=" + dept + ", salary=" + salary + "]";
58+
}
59+
60+
}

example/TaxService.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.javatechie.stream.api.example;
2+
3+
import java.util.List;
4+
import java.util.stream.Collectors;
5+
6+
public class TaxService {
7+
8+
public static List<Employee> evaluateTaxUsers(String input) {
9+
return (input.equalsIgnoreCase("tax"))
10+
? DataBase.getEmployees().stream().filter(emp -> emp.getSalary() > 500000).collect(Collectors.toList())
11+
: DataBase.getEmployees().stream().filter(emp -> emp.getSalary() <= 500000)
12+
.collect(Collectors.toList());
13+
14+
}
15+
16+
public static void main(String[] args) {
17+
System.out.println(evaluateTaxUsers("tax"));
18+
}
19+
}

0 commit comments

Comments
 (0)