Skip to content

Commit 84557ad

Browse files
author
javaplug
committed
java8 series
0 parents  commit 84557ad

19 files changed

Lines changed: 566 additions & 0 deletions
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.eight.pipeline;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.Map;
6+
import java.util.stream.Collectors;
7+
8+
public class CollectorPartitioning {
9+
10+
public static void main(String[] args) {
11+
List<String> list = Arrays.asList("coini", "double", "trip", "tesiiit");
12+
13+
Map<Boolean, List<String>> partialResult = list.stream().collect(Collectors.partitioningBy(hasI -> hasI.contains("i")));
14+
15+
System.out.println(partialResult.get(Boolean.TRUE)
16+
.stream()
17+
.map(count -> count.chars().filter(hasI -> hasI == 'i').count())
18+
.collect(Collectors.toList())
19+
);
20+
21+
System.out.println(partialResult.get(Boolean.FALSE));
22+
}
23+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.eight.pipeline;
2+
3+
import java.util.function.BiFunction;
4+
import java.util.function.Function;
5+
6+
public class DynamicFunctionCall {
7+
8+
public static void main(String[] args) {
9+
// TODO: get the department id or department name
10+
11+
Employee input = new Employee("Ram", 25, "Computer Science", "CS");
12+
13+
Function<Employee, String> getDeptName = Employee::getDepartName;
14+
BiFunction<Employee, String, String> sample = (a, b) -> a.getDepartId().concat(b);
15+
System.out.println("Print dept name");
16+
System.out.println(getDynamic(input, getDeptName));
17+
18+
System.out.println("Print dept Id");
19+
System.out.println(getDynamic(input, Employee::getDepartId));
20+
}
21+
22+
23+
private static String getDynamic(Employee input, Function<Employee, String> target) {
24+
// many lines
25+
return target.apply(input);
26+
}
27+
28+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.eight.pipeline;
2+
3+
public class Employee {
4+
private String name;
5+
private Integer age;
6+
private String departName;
7+
private String departId;
8+
9+
public Employee(String name, Integer age) {
10+
this.name = name;
11+
this.age = age;
12+
}
13+
14+
public Employee(String name, Integer age, String departName, String departId) {
15+
this.name = name;
16+
this.age = age;
17+
this.departName = departName;
18+
this.departId = departId;
19+
}
20+
21+
public String getName() {
22+
return name;
23+
}
24+
25+
public void setName(String name) {
26+
this.name = name;
27+
}
28+
29+
public Integer getAge() {
30+
return age;
31+
}
32+
33+
public void setAge(Integer age) {
34+
this.age = age;
35+
}
36+
37+
public String getDepartName() {
38+
return departName;
39+
}
40+
41+
public void setDepartName(String departName) {
42+
this.departName = departName;
43+
}
44+
45+
public String getDepartId() {
46+
return departId;
47+
}
48+
49+
public void setDepartId(String departId) {
50+
this.departId = departId;
51+
}
52+
53+
@Override
54+
public String toString() {
55+
return "Employee [name=" + name + ", age=" + age + ", departName=" + departName + ", departId=" + departId
56+
+ "]";
57+
}
58+
59+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.eight.pipeline;
2+
3+
import java.util.Arrays;
4+
import java.util.Collections;
5+
import java.util.Comparator;
6+
import java.util.List;
7+
8+
public class EmployeeData {
9+
10+
public static void main(String[] args) {
11+
/* List<String> input = Arrays.asList("B", "C", "A");
12+
System.out.println(input);
13+
Collections.sort(input);
14+
System.out.println(input);*/
15+
16+
List<Employee> employee = getEmployeeDetails();
17+
System.out.println(employee);
18+
//Collections.sort(employee, (e1, e2) -> e1.getName().compareTo(e2.getName()));
19+
employee.sort(Comparator.comparing(Employee::getName).reversed()
20+
.thenComparing(Comparator.comparing(Employee::getAge)));
21+
System.out.println(employee);
22+
23+
/*System.out.println(employee);
24+
Collections.sort(employee, (e1, e2) -> e1.getAge().compareTo(e2.getAge()));
25+
System.out.println(employee);*/
26+
}
27+
28+
private static List<Employee> getEmployeeDetails() {
29+
return Arrays.asList(new Employee("Ram", 10),
30+
new Employee("Raj", 12),
31+
new Employee("Raj", 9));
32+
}
33+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.eight.pipeline;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
7+
public class EmployeeGroup {
8+
9+
public static void main(String[] args) {
10+
// TODO Group Employees based on their Department
11+
12+
System.out.println(
13+
getEmployeeDetails().stream()
14+
.collect(Collectors.groupingBy(emp -> emp.getDepartId(),
15+
Collectors.counting()))
16+
);
17+
}
18+
19+
private static List<Employee> getEmployeeDetails() {
20+
return Arrays.asList(new Employee("Mahesh", 30, "Software Engineer", "SE"),
21+
new Employee("Siva", 32, "Managers", "MG"),
22+
new Employee("Prabhu", 39, "Software Engineer", "SE"));
23+
}
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.eight.pipeline;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
7+
public class EmployeeNames {
8+
9+
public static void main(String[] args) {
10+
// TODO Print all the Employee names
11+
12+
System.out.println(
13+
getEmployeeDetails().stream()
14+
.map(Employee::getName)
15+
.collect(Collectors.joining()));
16+
17+
}
18+
19+
private static List<Employee> getEmployeeDetails() {
20+
return Arrays.asList(new Employee("Mahesh", 30),
21+
new Employee("Siva", 32),
22+
new Employee("Prabhu", 39));
23+
}
24+
25+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.eight.pipeline;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Iterator;
6+
import java.util.List;
7+
8+
public class EmployeeRemove {
9+
10+
public static void main(String[] args) {
11+
// TODO: Remove an Employee from the collection
12+
13+
List<Employee> input = getEmployeeDetails();
14+
String target = "Somu";
15+
16+
System.out.println(input);
17+
18+
/*for (Iterator iterator = input.iterator(); iterator.hasNext();) {
19+
Employee employee = (Employee) iterator.next();
20+
if(target.equals(employee.getName())) {
21+
iterator.remove();
22+
}
23+
}*/
24+
25+
input.removeIf(employee -> target.equals(employee.getName()));
26+
System.out.println(input);
27+
}
28+
29+
private static List<Employee> getEmployeeDetails() {
30+
List<Employee> sample = new ArrayList<>();
31+
sample.add(new Employee("Suresh", 27));
32+
sample.add(new Employee("Melvin", 29));
33+
sample.add(new Employee("Somu", 30));
34+
sample.add(new Employee("Deepan", 31));
35+
return sample;
36+
/* return Arrays.asList(new Employee("Suresh", 27),
37+
new Employee("Melvin", 29),
38+
new Employee("Somu", 30),
39+
new Employee("Deepan", 31));*/
40+
}
41+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.eight.pipeline;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
public class EmployeeSearch {
7+
8+
public static void main(String[] args) {
9+
// TODO: find the an employee
10+
11+
String requiredPerson = "Mahessh";
12+
13+
List<Employee> input = getEmployeeDetails();
14+
15+
System.out.println(input.stream()
16+
.filter(employee -> requiredPerson.equals(employee.getName()))
17+
.findAny()
18+
.orElseThrow()
19+
);
20+
}
21+
22+
private static List<Employee> getEmployeeDetails() {
23+
return Arrays.asList(new Employee("Mahesh", 30),
24+
new Employee("Siva", 32),
25+
new Employee("Prabhu", 39));
26+
}
27+
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.eight.pipeline;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
7+
public class EmployeeStatistics {
8+
9+
public static void main(String[] args) {
10+
// TODO find unique employee names
11+
System.out.println(
12+
getEmployeeDetails().stream()
13+
.map(Employee::getName)
14+
.distinct()
15+
//.skip(2) 0 1 * 10 2*10
16+
//.limit(4) 10 10 10
17+
.collect(Collectors.toList()));
18+
}
19+
20+
private static List<Employee> getEmployeeDetails() {
21+
return Arrays.asList(new Employee("Mahesh", 30, "Software Engineer", "SE"),
22+
new Employee("Siva", 32, "Managers", "MG"),
23+
new Employee("Prabhu", 32, "Software Engineer", "SE"),
24+
new Employee("Ramesh", 32, "Managers", "MG"),
25+
new Employee("Siva", 32, "Software Engineer", "SE"),
26+
new Employee("Prabhu", 39, "Software Engineer", "SE"));
27+
}
28+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.eight.pipeline;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.function.Predicate;
6+
import java.util.stream.Collectors;
7+
8+
public class EmployeeYounger {
9+
10+
public static void main(String[] args) {
11+
// TODO: filter employees who all are less than 30
12+
13+
List<Employee> input = getEmployeeDetails();
14+
Predicate<Employee> findYounger = e -> e.getAge() < 30;
15+
//Predicate<Employee> findElder = e -> e.getAge() >= 30;
16+
17+
System.out.println(findEmployeeGroup(input, findYounger));
18+
19+
}
20+
21+
private static List<Employee> findEmployeeGroup(List<Employee> input, Predicate<Employee> empGroup) {
22+
return input.stream()
23+
.filter(isYoung -> empGroup.test(isYoung))
24+
.collect(Collectors.toList());
25+
}
26+
27+
private static List<Employee> getEmployeeDetails() {
28+
return Arrays.asList(new Employee("Dinesh", 25),
29+
new Employee("Raju", 29),
30+
new Employee("Partha", 32));
31+
}
32+
}

0 commit comments

Comments
 (0)