@@ -88,13 +88,13 @@ public void shelf() {
88
88
89
89
public static void predicateTest () {
90
90
Predicate <UserObj > predicate = userObj -> userObj .age <= 18 && Objects .equals (userObj .name , "kangxi" );
91
-
92
91
System .out .println (predicate .test (user1 ));
93
92
System .out .println (predicate .test (user2 ));
94
93
}
95
94
96
95
public static void SupplierTest () {
97
- Supplier <UserObj > supplier = () -> new UserObj (UUID .randomUUID ().toString (), Integer .valueOf ((Math .random () * 10 ) + "" ));
96
+ Supplier <UserObj > supplier = () -> new UserObj ("qianlong" , 60 );
97
+ Consumer <UserObj > consumer = System .out ::println ;
98
98
99
99
}
100
100
@@ -128,19 +128,17 @@ public static void streamTest() {
128
128
new UserObj ("b" , 25 ),
129
129
new UserObj ("c" , 17 ),
130
130
new UserObj ("d" , 13 )).collect (Collectors .toList ());
131
- List <UserObj > heis = Stream .of (
131
+ List <UserObj > heis1 = Stream .of (
132
132
new UserObj ("a" , 13 ),
133
133
new UserObj ("m1" , 30 )).collect (Collectors .toList ());
134
134
List <UserObj > heis2 = Stream .of (
135
135
new UserObj ("a" , 93 ),
136
136
new UserObj ("b" , 55 )).collect (Collectors .toList ());
137
137
//
138
- Stream <UserObj > userObjStream = uList .stream ().filter (userObj -> userObj .age < 24 );
139
- Stream <Map <String , Object >> mapStream = userObjStream .map (UserObj ::toMap );
138
+ Stream <Map <String , Object >> mapStream = uList .stream ().filter (userObj -> userObj .age < 24 ).map (UserObj ::toMap );
140
139
141
140
// flatmap这样就给两个流合并了
142
- Stream <List <UserObj >> uList1 = Stream .of (uList , heis , heis2 );
143
- Stream <UserObj > userObjStream1 = uList1 .flatMap (Collection ::stream );
141
+ Stream <UserObj > userObjStream1 = Stream .of (uList , heis1 , heis2 ).flatMap (Collection ::stream );
144
142
// 求个最大值
145
143
// UserObj userObj = userObjStream1.max(Comparator.comparing(st -> st.age)).get();
146
144
// System.out.println("count:" + userObj);
@@ -150,6 +148,7 @@ public static void streamTest() {
150
148
// return x;
151
149
// });
152
150
// System.out.println(reduce.get());
151
+ // System.out.println(reduce.get());
153
152
// 求平均年龄
154
153
// userObjStream1.collect()
155
154
@@ -162,12 +161,15 @@ public static void streamTest() {
162
161
// Map<String, List<UserObj>> collect = userObjStream1.collect(Collectors.groupingBy(UserObj::getName));
163
162
// System.out.println(collect);
164
163
165
- // grouping 分组聚合
164
+ // grouping 分组聚合1
166
165
// Map<String, Integer> collect = userObjStream1.collect(Collectors.groupingBy(UserObj::getName, Collectors.summingInt(UserObj::getAge)));
167
166
// System.out.println(collect);
167
+ // grouping 分组聚合2
168
+ Integer collect = userObjStream1 .collect (Collectors .collectingAndThen (Collectors .groupingBy (UserObj ::getName ), Map ::size ));
169
+ System .out .println (collect );
168
170
// 拼接
169
171
// Collectors.collectingAndThen(Collectors.toList(), Optional::ofNullable)
170
- String collect = userObjStream1 .map (UserObj ::getName ).collect (Collectors .joining ("," , " " , " " ));
172
+ // String collect = userObjStream1.map(UserObj::getName).collect(Collectors.joining(",", " ", " "));
171
173
System .out .println (collect );
172
174
}
173
175
@@ -185,16 +187,16 @@ public static void streamTestAdv() {
185
187
// return u;
186
188
// });
187
189
// 轻松转map
188
- // Map<String, UserObj> collect = uList.stream()
189
- //// .collect(Collectors.groupingBy(UserObj::getName, Collectors.summingInt(UserObj::getAge)))
190
- // .collect(Collectors.toMap(UserObj::getName, Function.identity()));
191
- // System.out.println(collect);
190
+ Map <String , UserObj > collect = uList .stream ()
191
+ // .collect(Collectors.groupingBy(UserObj::getName, Collectors.summingInt(UserObj::getAge)))
192
+ .collect (Collectors .toMap (UserObj ::getName , Function .identity ()));
193
+ System .out .println (collect );
192
194
193
195
194
- Map <Boolean , List <UserObj >> collect = uList .stream ().collect (
195
- Collectors .partitioningBy (
196
- u -> u .getAge () > 18 )
197
- );
196
+ // Map<Boolean, List<UserObj>> collect = uList.stream().collect(
197
+ // Collectors.partitioningBy(
198
+ // u -> u.getAge() > 18)
199
+ // );
198
200
System .out .println (collect );
199
201
}
200
202
@@ -236,7 +238,7 @@ public static void streamTestPar2() {
236
238
String [] intFrs = new String [] {"1" ,"2" ,"3" ,"4" ,"5" ,"6" ,"7" ,"8" ,"9" ,"10" };
237
239
// new
238
240
Stream .of (intFrs )
239
- .parallel ()
241
+ // .parallel()
240
242
.mapToLong (Long ::parseLong )
241
243
.mapToObj (fr -> {
242
244
AsOs key = new AsOs ();
@@ -277,10 +279,10 @@ public static void acc() {
277
279
public static void main (String [] args ) {
278
280
// predicateTest();
279
281
// consumerTest();
280
- // streamTest();
282
+ streamTest ();
281
283
// streamTestAdv();
282
284
// streamTestHM();
283
- streamTestPar2 ();
285
+ // streamTestPar2();
284
286
}
285
287
286
288
}
0 commit comments