Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e85e3c9

Browse filesBrowse files
committed
lambda map
1 parent 87259bd commit e85e3c9
Copy full SHA for e85e3c9

File tree

Expand file treeCollapse file tree

4 files changed

+222
-13
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+222
-13
lines changed
Open diff view settings
Collapse file

‎JdkLearn/src/main/java/com/learnjava/lambda/LambdaComparatorDemo.java‎

Copy file name to clipboardExpand all lines: JdkLearn/src/main/java/com/learnjava/lambda/LambdaComparatorDemo.java
+27-13Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,36 @@
22

33
import lombok.AllArgsConstructor;
44
import lombok.Data;
5+
import sun.java2d.pipe.SpanShapeRenderer;
56

7+
import java.text.SimpleDateFormat;
68
import java.util.Arrays;
79
import java.util.Comparator;
10+
import java.util.Date;
811
import java.util.List;
12+
import java.util.function.Predicate;
913
import java.util.stream.Stream;
1014

1115
public class LambdaComparatorDemo {
1216

1317
public static String[] arrays = {"Milan", "london", "San Francisco", "Tokyo", "New Delhi"};
1418

19+
public static List<Employee> employees;
20+
21+
static {
22+
Employee e1 = new Employee(1,23,"M","Rick","Beethovan", "2021-04-01");
23+
Employee e2 = new Employee(2,13,"F","Martina","Hengis", "2021-04-02");
24+
Employee e3 = new Employee(3,43,"M","Ricky","Martin","2021-04-03" );
25+
Employee e4 = new Employee(4,26,"M","Jon","Lowman", "2021-04-04");
26+
Employee e5 = new Employee(5,19,"F","Cristine","Maria", "2021-04-05");
27+
Employee e6 = new Employee(6,15,"M","David","Feezor", "2021-04-06");
28+
Employee e7 = new Employee(7,68,"F","Melissa","Roy", "2021-04-07");
29+
Employee e8 = new Employee(8,79,"M","Alex","Gussin", "2021-04-08");
30+
Employee e9 = new Employee(9,15,"F","Neetu","Singh", "2021-04-09");
31+
Employee e10 = new Employee(10,45,"M","Naveen","Jain", "2021-04-10");
32+
employees = Arrays.asList(e1, e2, e3, e4, e5, e6, e7, e8, e9, e10);
33+
}
34+
1535
public static void main(String[] args) {
1636
// test01();
1737
// test02();
@@ -60,19 +80,6 @@ public static void test02() {
6080
* 对对象进行排序
6181
*/
6282
public static void test03() {
63-
Employee e1 = new Employee(1,23,"M","Rick","Beethovan");
64-
Employee e2 = new Employee(2,13,"F","Martina","Hengis");
65-
Employee e3 = new Employee(3,43,"M","Ricky","Martin");
66-
Employee e4 = new Employee(4,26,"M","Jon","Lowman");
67-
Employee e5 = new Employee(5,19,"F","Cristine","Maria");
68-
Employee e6 = new Employee(6,15,"M","David","Feezor");
69-
Employee e7 = new Employee(7,68,"F","Melissa","Roy");
70-
Employee e8 = new Employee(8,79,"M","Alex","Gussin");
71-
Employee e9 = new Employee(9,15,"F","Neetu","Singh");
72-
Employee e10 = new Employee(10,45,"M","Naveen","Jain");
73-
74-
List<Employee> employees = Arrays.asList(e1, e2, e3, e4, e5, e6, e7, e8, e9, e10);
75-
7683
// 根据employee的年龄进行自然排序
7784
employees.sort(Comparator.comparing(Employee::getAge));
7885
employees.forEach(System.out::println);
@@ -136,6 +143,7 @@ public static class Employee {
136143
private String gender;
137144
private String firstName;
138145
private String lastName;
146+
private String date;
139147

140148
@Override
141149
public String toString() {
@@ -145,7 +153,13 @@ public String toString() {
145153
", gender='" + gender + '\'' +
146154
", firstName='" + firstName + '\'' +
147155
", lastName='" + lastName + '\'' +
156+
", date='" + date + '\'' +
148157
'}';
149158
}
159+
160+
// 年龄大于70的谓语
161+
static Predicate<Employee> ageGreaterThan70 = e -> e.getAge() > 70;
162+
163+
static Predicate<Employee> ageLessThan18 = e -> e.getAge() < 18;
150164
}
151165
}
Collapse file
+42Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.learnjava.lambda;
2+
3+
import java.text.SimpleDateFormat;
4+
import java.util.Date;
5+
import java.util.List;
6+
import java.util.stream.Collectors;
7+
8+
/**
9+
* @author bruis
10+
* 操作Map
11+
*/
12+
public class LambdaMapDemo {
13+
14+
public static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
15+
16+
public static void main(String[] args) {
17+
test01();
18+
}
19+
20+
/**
21+
* 需求,将employee集合中的元素根据date进行排序
22+
*/
23+
public static void test01() {
24+
List<LambdaComparatorDemo.Employee> sortedByDate = LambdaComparatorDemo.employees
25+
.stream()
26+
.sorted((a, b) -> {
27+
int result;
28+
try {
29+
result = getDate(b.getDate()).compareTo(getDate(a.getDate()));
30+
} catch (Exception e) {
31+
result = 0;
32+
}
33+
return result;
34+
})
35+
.collect(Collectors.toList());
36+
System.out.println(sortedByDate);
37+
}
38+
39+
public static Date getDate(String date) throws Exception {
40+
return format.parse(date);
41+
}
42+
}
Collapse file
+104Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package com.learnjava.lambda;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.stream.Stream;
6+
7+
/**
8+
* @author bruis
9+
* 累加器
10+
*/
11+
public class LambdaReduceDemo {
12+
13+
public static void main(String[] args) {
14+
// test01();
15+
// test02();
16+
test03();
17+
}
18+
19+
/**
20+
* 对整形进行操作
21+
*/
22+
public static void test01() {
23+
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
24+
Integer reduce = numbers
25+
.stream()
26+
.reduce(0, LambdaReduceDemo::mySum);
27+
// .reduce(0, Integer::sum);
28+
// .reduce(0, (total, element) -> total + element);
29+
System.out.println(reduce);
30+
}
31+
32+
/**
33+
* 对字符串进行操作
34+
*/
35+
public static void test02() {
36+
List<String> letters = Arrays.asList("a", "b", "c", "d", "e");
37+
String reduce = letters
38+
.stream()
39+
.reduce("", String::concat);
40+
// .reduce("", (totol, element) -> totol.concat(element));
41+
System.out.println(reduce);
42+
}
43+
44+
/**
45+
* 操作Employee集合元素,将所有员工的年龄通过reduce累加起来
46+
*
47+
* U identity, BiFunction<U, ? super T, U> accumulator, BinaryOperator<U> combiner
48+
* reduce的三个参数:
49+
* 1)初始值;
50+
* 2)累加器(可自己实现逻辑)
51+
* 3) 合并器(parallelStream模式时的合并)
52+
*
53+
*/
54+
public static void test03() {
55+
// 将Employee集合元素转化为Integer集合元素(流)
56+
// map的操作就是将a类型元素转化为b类型元素
57+
Stream<Integer> integerStream = LambdaComparatorDemo
58+
.employees
59+
.stream()
60+
.map(LambdaComparatorDemo.Employee::getAge);
61+
62+
// 求所有员工的年龄
63+
Integer totalAge = integerStream.reduce(0, Integer::sum);
64+
System.out.println(totalAge);
65+
66+
// 数据量大的话,可以用并行计算
67+
// 先讲员工集合转化为“并行流”
68+
Stream<Integer> parallelStream = LambdaComparatorDemo
69+
.employees
70+
.parallelStream()
71+
.map(LambdaComparatorDemo.Employee::getAge);
72+
73+
// 相比于普通的单个流,parallelStream多了个合并器,将多个CPU计算的结果再合并到同一个流
74+
Integer reduce = parallelStream.reduce(0, Integer::sum, Integer::sum);
75+
System.out.println(reduce);
76+
77+
// 可以不用map将employee转化为Integer对象,可以直接reduce操作,最后通过Integer::sum这个合并器来将结果合并为Integer类型
78+
Integer total = LambdaComparatorDemo
79+
.employees
80+
.stream()
81+
.reduce(0, (subTotal, emp) -> subTotal + emp.getAge(), Integer::sum);
82+
System.out.println(total);
83+
84+
Integer total2 = LambdaComparatorDemo
85+
.employees
86+
.stream()
87+
.reduce(0, LambdaReduceDemo::mySum2, Integer::sum);
88+
System.out.println(total2);
89+
}
90+
91+
/**
92+
* 可作为BiFunction,传入到reduce作为入参
93+
* @param a
94+
* @param b
95+
* @return
96+
*/
97+
public static Integer mySum(int a, int b) {
98+
return a + b;
99+
}
100+
101+
public static Integer mySum2(int a, LambdaComparatorDemo.Employee employee) {
102+
return a + employee.getAge();
103+
}
104+
}
Collapse file
+49Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.learnjava.lambda;
2+
3+
import java.util.Optional;
4+
5+
/**
6+
* @author bruis
7+
*/
8+
public class StreamMatchDemo {
9+
10+
public static void main(String[] args) {
11+
// test01();
12+
test02();
13+
}
14+
15+
/**
16+
* 判断是否有年龄大于70的员工
17+
*/
18+
public static void test01() {
19+
boolean isExistAgeThan70 = LambdaComparatorDemo.employees
20+
.stream()
21+
// 使用了Employee的谓语语句(这种写法方便复用)
22+
.anyMatch(LambdaComparatorDemo.Employee.ageGreaterThan70);
23+
// .anyMatch(e -> e.getAge() > 70);
24+
System.out.println(isExistAgeThan70);
25+
26+
boolean isExistAgeLessThan18 = LambdaComparatorDemo.employees
27+
.stream()
28+
.noneMatch(LambdaComparatorDemo.Employee.ageLessThan18);
29+
30+
System.out.println(isExistAgeLessThan18);
31+
}
32+
33+
/**
34+
* 元素查找与Optional
35+
*/
36+
public static void test02() {
37+
38+
Optional<LambdaComparatorDemo.Employee> employeeOptional = LambdaComparatorDemo.employees
39+
.stream()
40+
.filter(e -> e.getAge() > 400)
41+
.findFirst();
42+
43+
// Optional#get 会报空
44+
// System.out.println(employeeOptional.get());
45+
LambdaComparatorDemo.Employee employee = employeeOptional.orElse(null);
46+
System.out.println(employee == null);
47+
}
48+
49+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.