File tree Expand file tree Collapse file tree 3 files changed +41
-3
lines changed
src/main/java/com/xu/java8 Expand file tree Collapse file tree 3 files changed +41
-3
lines changed Original file line number Diff line number Diff line change @@ -33,6 +33,8 @@ public static void main(String[] args) {
33
33
System .out .println (optional .orElse ("fallback" ));
34
34
optional .ifPresent (System .out ::println );
35
35
optional2 .ifPresent (System .out ::println );
36
+
37
+ System .out .println (Optional .ofNullable (null ).isPresent ());
36
38
}
37
39
}
38
40
Original file line number Diff line number Diff line change 1
1
package com .xu .java8 .Stream ;
2
2
3
- import java .util .ArrayList ;
4
- import java .util .List ;
5
- import java .util .Optional ;
3
+ import java .util .*;
6
4
7
5
/**
8
6
* Class java8-Stream接口
Original file line number Diff line number Diff line change
1
+ package com .xu .java8 .Stream ;
2
+
3
+ import java .util .HashMap ;
4
+ import java .util .LinkedHashMap ;
5
+ import java .util .Map ;
6
+
7
+ /**
8
+ * Created by xu on 2016/9/13.
9
+ */
10
+ public class Streams6 {
11
+ public static void main (String args []) {
12
+ Map <String , Integer > unsortMap = new HashMap <>();
13
+ unsortMap .put ("z" , 10 );
14
+ unsortMap .put ("b" , 5 );
15
+ unsortMap .put ("a" , 6 );
16
+ unsortMap .put ("c" , 20 );
17
+ unsortMap .put ("d" , 1 );
18
+ unsortMap .put ("e" , 7 );
19
+ unsortMap .put ("y" , 8 );
20
+ unsortMap .put ("n" , 99 );
21
+ unsortMap .put ("j" , 50 );
22
+ unsortMap .put ("m" , 2 );
23
+ unsortMap .put ("f" , 9 );
24
+ System .out .println (sortByValue (unsortMap ));
25
+ }
26
+
27
+ /**
28
+ * 使用stream类来对map的value排序(并行排序,逆序)
29
+ */
30
+ public static <K , V extends Comparable <? super V >> Map <K , V >
31
+ sortByValue (Map <K , V > map ) {
32
+ Map <K , V > result = new LinkedHashMap <>();
33
+ map .entrySet ().parallelStream ().sorted ((o1 , o2 ) -> (o2 .getValue ()).compareTo (o1 .getValue ())).forEachOrdered (x ->
34
+ result .put (x .getKey (), x
35
+ .getValue ()));
36
+ return result ;
37
+ }
38
+ }
You can’t perform that action at this time.
0 commit comments