Skip to content

Commit c3b9dc8

Browse files
committed
1. lambda表达式 案例
#bysocket
1 parent 87995be commit c3b9dc8

17 files changed

+537
-31
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Java核心技术学习代码兼测试案例<br>
3333
├── org.javacore.thread // Java 线程
3434
=================实战JAVA 8=================
3535
├── org.javacore.lambda // lambda表达式
36+
├── org.javacore.stream // Stream API 集合的流式操作
3637
3738
拼命更新!顶!d=====( ̄▽ ̄*)b
3839

src/org/javacore/lambda/LambdaTest01.java renamed to src/org/javacore/lambda/LambdaListCompare.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
11
package org.javacore.lambda;
22

3+
/*
4+
* Copyright [2015] [Jeff Lee]
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
319
import java.util.ArrayList;
420
import java.util.List;
521

622
/**
723
* List 比较器
8-
* <p/>
24+
*
925
* Created by bysocket on 16/7/12.
1026
*/
11-
public class LambdaTest01 {
27+
public class LambdaListCompare {
1228
public static void main(String[] args) {
1329
List<Integer> list = new ArrayList<>();
1430
list.add(4);
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.javacore.lambda;
2+
3+
/*
4+
* Copyright [2015] [Jeff Lee]
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import java.util.ArrayList;
20+
import java.util.Comparator;
21+
import java.util.List;
22+
23+
/**
24+
* Lambda 操作符
25+
*
26+
* Created by bysocket on 16/7/13.
27+
*/
28+
public class LambdaOperators {
29+
public static void main(String[] args) {
30+
List<String> list = new ArrayList<>();
31+
list.add("aaa");
32+
list.add("cccc");
33+
list.add("b");
34+
list.add("eeeee");
35+
36+
/**
37+
* :: 操作符格式 => 三种情况
38+
* 对象::实例方法
39+
* 类::静态方法
40+
* 类::实例方法 对于前两种情况,方法引用就是对参数执行该方法。比如下面两种方法
41+
*/
42+
// 按字符串大小排序忽略大小写
43+
list.sort(String::compareToIgnoreCase);
44+
// 打印 list 元素
45+
list.forEach(System.out::println);
46+
47+
System.out.println("======按字符大小排序======");
48+
list.sort(Comparator.comparing(String::length));
49+
list.forEach(System.out::println);
50+
}
51+
}

src/org/javacore/lambda/LambdaRunnable.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,40 @@
11
package org.javacore.lambda;
22

3+
/*
4+
* Copyright [2015] [Jeff Lee]
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
319
/**
20+
* Lambda - 启动线程
21+
*
422
* Created by bysocket on 16/7/13.
523
*/
624
public class LambdaRunnable {
725
static int b = 10;
826

927
public static void main(String[] args) {
28+
// 启动线程
29+
// Thread thread = new Thread(new Runnable() {
30+
// @Override
31+
// public void run() {
32+
// b++;
33+
// System.out.println(b);
34+
// }
35+
// });
36+
37+
// Lambda - 启动线程
1038
Thread thread = new Thread(() -> {
1139
b++;
1240
System.out.println(b);

src/org/javacore/lambda/LambdaTest02.java

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/org/javacore/scheduler/TimerTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
package org.javacore.scheduler; /*
1+
package org.javacore.scheduler;
2+
/*
23
* Copyright [2015] [Jeff Lee]
34
*
45
* Licensed under the Apache License, Version 2.0 (the "License");
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.javacore.stream;
2+
3+
/*
4+
* Copyright [2015] [Jeff Lee]
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import java.util.Arrays;
20+
import java.util.List;
21+
import java.util.stream.Collectors;
22+
23+
/**
24+
*
25+
* Created by bysocket on 16/7/14.
26+
*/
27+
public class CollectStreamTest {
28+
public static void main(String[] args) {
29+
List<Integer> list = Arrays.asList(1,2,3,4);
30+
Double result = list.stream().collect(Collectors.averagingDouble(d->d*2));
31+
System.out.println(result);
32+
}
33+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package org.javacore.stream;
2+
3+
/*
4+
* Copyright [2015] [Jeff Lee]
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
import java.util.stream.Collectors;
22+
import java.util.stream.Stream;
23+
24+
/**
25+
* Stream API 集合的流式操作
26+
*
27+
* Created by bysocket on 16/7/13.
28+
*/
29+
public class CollectionStreamTest {
30+
public static void main(String[] args) {
31+
List<String> list = new ArrayList<>();
32+
list.add("aa");
33+
list.add("cccc");
34+
list.add("bbb");
35+
36+
/**
37+
* Stream的使用:
38+
* 创建/获取流 -> 中间操作(过滤、转换等) -> 终止操作( 聚合、收集结果)
39+
*/
40+
list.stream().forEach(System.out::println);
41+
System.out.println();
42+
43+
/**
44+
* 过滤
45+
* collect语法 {@link StreamCollectTest}
46+
*/
47+
List list0 = list.stream().filter(str -> str.startsWith("cc")).collect(Collectors.toList());
48+
List list1 = list.stream().filter(str -> str.startsWith("aa")).collect(Collectors.toList());
49+
50+
list0.stream().forEach(System.out::println);
51+
list1.stream().forEach(System.out::println);
52+
System.out.println();
53+
54+
/**
55+
* 转换
56+
*/
57+
List list2 = list.stream().map(str -> str.replace("c","*")).collect(Collectors.toList());
58+
59+
list2.stream().forEach(System.out::println);
60+
System.out.println();
61+
62+
/**
63+
* 提取
64+
* 从skip开始至limit位置为止
65+
*/
66+
List list3 = list.stream().skip(0).limit(1).collect(Collectors.toList());
67+
68+
list3.stream().forEach(System.out::println);
69+
System.out.println();
70+
71+
/**
72+
* 组合
73+
*/
74+
List list4 = Stream.concat(list.stream(),list.stream()).collect(Collectors.toList());
75+
76+
list4.stream().forEach(System.out::println);
77+
System.out.println();
78+
}
79+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.javacore.stream;
2+
3+
/*
4+
* Copyright [2015] [Jeff Lee]
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import java.util.Arrays;
20+
import java.util.List;
21+
22+
/**
23+
* Stream API 集合的流式操作
24+
* <p/>
25+
* Created by bysocket on 16/7/13.
26+
*/
27+
public class CollectionStreamTest01 {
28+
public static void main(String[] args) {
29+
List<String> strList = Arrays.asList("a1", "a2", "c3", "c6", "c4");
30+
31+
strList
32+
.stream()
33+
.filter(str -> str.startsWith("c"))
34+
.map(String::toUpperCase)
35+
.sorted()
36+
.forEach(System.out::println);
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.javacore.stream;
2+
3+
/*
4+
* Copyright [2015] [Jeff Lee]
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import java.util.Arrays;
20+
import java.util.List;
21+
import java.util.stream.Collectors;
22+
import java.util.stream.Stream;
23+
24+
/**
25+
* 组合 - 流
26+
*
27+
* Created by bysocket on 16/7/14.
28+
*/
29+
public class ConcatStreamTest {
30+
public static void main(String[] args) {
31+
List<String> list1 = Arrays.asList("a","b","c");
32+
List<String> list2 = Arrays.asList("d","e","f");
33+
34+
// 组合list1和list2的流
35+
List result = Stream.concat(list1.stream(),list2.stream()).collect(Collectors.toList());
36+
result.stream().forEach(System.out::println);
37+
}
38+
}

0 commit comments

Comments
 (0)