-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLambda.java
More file actions
113 lines (90 loc) · 3.65 KB
/
Lambda.java
File metadata and controls
113 lines (90 loc) · 3.65 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.wangms;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toCollection;
/**
* @author: wangms
* @date: 2020-08-28 14:36
*/
public class Lambda {
public static void main(String[] args) {
Map<String, String> map = new HashMap();
map.put("1", "张三");
map.put("2", "李四");
map.put("3", "王五");
Collection<String> s1 = map.values();
System.out.println(s1);
for (String s : map.keySet()) {
System.out.println(s);
}
map.forEach((a, b) -> {
System.out.println(b);
});
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
list.forEach(v -> {
System.out.println(v);
});
String str = list.stream().findFirst().toString();
System.out.println(str);
Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println("1");
}
};
Runnable runnable1 = () -> System.out.println("1");
Stream stream = Stream.of("a", "b", "c");
System.out.println("limit:");
stream.limit(2).forEach(System.out::println);
List<Integer> list11 = new ArrayList<Integer>();
list11.add(4);
list11.add(3);
list11.add(2);
list11.add(3);
list11.stream().sorted(Comparator.reverseOrder()).forEach(System.out::println);
long count = list11.parallelStream().filter(a -> a == 3).count();
System.out.println("count:"+count);
list11.stream().distinct().forEach(a-> System.out.println("aa:"+a));
List<Integer> numbers = Arrays.asList(1, 5, 7, 3, 9);
IntSummaryStatistics stats = numbers.stream().mapToInt((x) -> x).summaryStatistics();
System.out.println("列表中最大的数 : " + stats.getMax());
System.out.println("列表中最小的数 : " + stats.getMin());
System.out.println("所有数之和 : " + stats.getSum());
System.out.println("平均数 : " + stats.getAverage());
System.out.println(LocalDate.now());
System.out.println(LocalDateTime.now());
System.out.println(new Date());
LocalDateTime ld = LocalDateTime.parse("2020-09-01T11:11:30.222Z");
LocalDateTime ld2 = LocalDateTime.parse("2020-09-01T11:11:30.111Z");
System.out.println("相差毫秒 : " + Duration.between(ld, ld2).toMillis());
System.out.println("相毫秒 : " + Duration.between(ld, ld2).getSeconds());
String[] strArray = new String[] { "a", "b", "c" };
stream = Stream.of(strArray);
stream = Arrays.stream(strArray);
//List<String> list = Arrays.asList(strArray);
// stream = list.stream();
try {
Stream<String> stream2 = Stream.of("a", "b", "c");
// 转换成 Array
String[] strArray1 = stream2.toArray(String[]::new);
// 转换成 Collection
List<String> list1 = stream2.collect(Collectors.toList());
List<String> list2 = stream2.collect(toCollection(ArrayList::new));
Set set1 = stream2.collect(Collectors.toSet());
Stack stack1 = stream2.collect(toCollection(Stack::new));
// 转换成 String
// String str = stream.collect(Collectors.joining()).toString();
} catch (Exception e) {
e.printStackTrace();
}
}
}