Java 8 lambda表達式引入詳解及實例
eclipse 下載安裝
Help -> EclipseMarketplace -> 搜索Java 8 Kepler ->Java 8 support for eclipse Kepler SR2 安裝完成后需要重啟
Android Studio
在project的build.gradle文件中添加
1
2
3
4
5
|
buildscript { dependencies { classpath 'me.tatarka:gradle-retrolambda:3.2.5' } } |
在app的build.gradle文件中添加
1
2
3
4
5
6
7
8
9
10
|
apply plugin: 'me.tatarka.retrolambda' android { compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } |
使用
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.Optional; import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.Stream; import java.util.stream.Stream.Builder; public class LambdaTest { public static void main(String[] args) { String[] str = new String[] { "Lambdas" , "Lambdas" , "Default Method" , "Stream API" , "Date and Time API" }; List<String> strList = Arrays.asList(str); System.out.println( "----------------------------> 默認遍歷" ); strList.stream().forEach(item -> { System.out.println(item); }); System.out.println( "----------------------------> 默認遍歷簡化寫法" ); strList.stream().forEach(System.out::println); // limit輸出指定個數(shù) System.out.println( "limit ---------------------------->" ); strList.stream().limit( 2 ).forEach(System.out::println); // 去掉重復(fù)數(shù)據(jù) System.out.println( "distinct ---------------------------->" ); strList.stream().distinct().forEach(System.out::println); // filter過濾器,篩選出符合條件的值 System.out.println( "filter ---------------------------->" ); Predicate<String> contain = item -> item.contains( "API" ); // 只是用于匹配條件的如int可以用條件運算符等 strList.stream().filter(contain).forEach(System.out::println); System.out.println( "filter簡化寫法 ---------------------------->" ); strList.stream().filter(item -> item.contains( "API" )).forEach(System.out::println); System.out.println( "AND ---------------------------->" ); Predicate<String> contain1 = item -> item.contains( "API" ); Predicate<String> contain2 = item -> item.contains( "Time" ); strList.stream().filter(contain1.and(contain2)).forEach(System.out::println); System.out.println( "OR ---------------------------->" ); strList.stream().filter(contain1.or(contain2)).forEach(System.out::println); // 向每個字符后追加 System.out.println( "map ---------------------------->" ); // 對Stream中包含的元素使用給定的轉(zhuǎn)換函數(shù)進行轉(zhuǎn)換操作,生成的Stream只包含轉(zhuǎn)換生成的元素。 // mapToInt,mapToLong和mapToDouble是對int、long、double進行操作的 strList.stream().map(item -> item + String.valueOf( 1 )).forEach(System.out::println); // 向每個字符后追加 System.out.println( "flatMap ---------------------------->" ); // flatMap:和map類似,不同的是其每個元素轉(zhuǎn)換得到的是Stream對象,會把子Stream中的元素壓縮到父集合 strList.stream().flatMap(item -> getCharacter(item)).forEach(System.out::println); System.out.println( "peek ---------------------------->" ); // peek 需調(diào)用collect strList.stream().map(String::toUpperCase).peek(System.out::println).collect(Collectors.toList()); System.out.println( "skip ---------------------------->" ); // 丟棄原Stream的前N個元素后剩下元素組成的新Stream strList.stream().skip( 3 ).forEach(System.out::println); // 統(tǒng)計個數(shù) System.out.println( "count ---------------------------->" + strList.stream().count()); // allMatch:是不是Stream中的所有元素都滿足給定的匹配條件 boolean allMatch1 = strList.stream().allMatch(item -> item.contains( "a" )); System.out.println( "allMatch --------------> " + allMatch1); boolean allMatch2 = strList.stream().allMatch(item -> item.contains( "API" )); System.out.println( "allMatch --------------> " + allMatch2); // anyMatch:Stream中是否存在任何一個元素滿足匹配條件 boolean anyMatch1 = strList.stream().anyMatch(item -> item.contains( "Stream API" )); System.out.println( "anyMatch --------------> " + anyMatch1); boolean anyMatch2 = strList.stream().anyMatch(item -> item.contains( "Stream API1" )); System.out.println( "anyMatch --------------> " + anyMatch2); // findFirst: 返回Stream中的第一個元素,如果Stream為空,返回空Optional Optional<String> findFirst = strList.stream().findFirst(); // isPresent方法用來檢查Optional實例是否有值。 if (findFirst.isPresent()) { // 調(diào)用get()返回Optional值。如果Optional沒有值調(diào)和則拋出NoSuchElementException。 System.out.println( "findFirst --------------> " + findFirst.get()); } System.out.print( "findFirst簡化寫法 --------------> " ); // 如果存在值,則使用該值調(diào)用,否則不執(zhí)行任何操作。 strList.stream().findFirst().ifPresent(System.out::println); // noneMatch:是不是Stream中的所有元素都不滿足給定的匹配條件 boolean noneMatch1 = strList.stream().noneMatch(item -> item.contains( "Stream API" )); System.out.println( "noneMatch --------------> " + noneMatch1); boolean noneMatch2 = strList.stream().noneMatch(item -> item.contains( "zzzzz" )); System.out.println( "noneMatch --------------> " + noneMatch2); System.out.println( "newStrList ---------------------------->" ); List<String> newStrList = strList.stream().filter(item -> item != null ) .collect(() -> new ArrayList<String>(), (list, item) -> list.add(item), (list1, list2) -> list1.addAll(list2)); newStrList.stream().forEach(System.out::println); System.out.println( "newStrList簡化寫法 ---------------------------->" ); List<String> newStrList1 = strList.stream().filter(item -> item != null ).collect(Collectors.toList()); newStrList1.stream().forEach(System.out::println); System.out.println( "sorted 排序---------------------------->" ); // strList.stream().sorted(); strList.stream().sorted(Comparator.comparing(String::length)).forEach(System.out::println); ; // max和min:使用給定的比較器(Operator),返回Stream中的最大|最小值 Integer[] ints = new Integer[] { 7 , 2 , 3 , 10 , 5 , 1 , 6 , 8 , 9 , 4 }; List<Integer> intList = new ArrayList<Integer>(); intList = Arrays.asList(ints); System.out.print( "max --------------> " ); intList.stream().max((o1, o2) -> o1.compareTo(o2)).ifPresent(System.out::println); System.out.print( "max簡化寫法 --------------> " ); // Comparable<Integer> Integer.compare(int arg0, int arg1) intList.stream().max(Integer::compare).ifPresent(System.out::println); System.out.print( "min --------------> " ); intList.stream().min((o1, o2) -> o1.compareTo(o2)).ifPresent(System.out::println); System.out.print( "min簡化寫法 --------------> " ); // Comparable<Integer> Integer.compare(int arg0, int arg1) intList.stream().min(Integer::compare).ifPresent(System.out::println); System.out.println( "reduce單參數(shù) ---------------------------->" ); System.out.println(intList.stream().reduce((result, element) -> result = result + element)); System.out.println( "reduce雙參數(shù) ---------------------------->" ); System.out.println(intList.stream().reduce( 0 , (result, element) -> result = result + element)); System.out.println( "generate ---------------------------->" ); // 生成一個無限長度的Stream,其中值是隨機的。這個無限長度Stream是懶加載,一般這種無限長度的Stream都會配合Stream的limit()方法來用。 Stream.generate(Math::random).limit( 2 ).forEach(System.out::println); System.out.println( "iterate ---------------------------->" ); // 也是生成無限長度的Stream,和generator不同的是,其元素的生成是重復(fù)對給定的種子值,調(diào)用用戶指定函數(shù)來生成的 Stream.iterate( 12 , item -> item + 1 ).limit( 2 ).forEach(System.out::println); } public static Stream<String> getCharacter(String s) { Builder<String> builder = Stream.builder(); builder.add(s); builder.accept( "1" ); return builder.build(); } } |
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!