Skip to content

Commit 9a6afcf

Browse files
committed
Initial commit
0 parents  commit 9a6afcf

File tree

13 files changed

+396
-0
lines changed

13 files changed

+396
-0
lines changed

.gitignore

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
HELP.md
2+
target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
!**/src/main/**
5+
!**/src/test/**
6+
7+
### STS ###
8+
.apt_generated
9+
.classpath
10+
.factorypath
11+
.project
12+
.settings
13+
.springBeans
14+
.sts4-cache
15+
16+
### IntelliJ IDEA ###
17+
.idea
18+
*.iws
19+
*.iml
20+
*.ipr
21+
22+
### NetBeans ###
23+
/nbproject/private/
24+
/nbbuild/
25+
/dist/
26+
/nbdist/
27+
/.nb-gradle/
28+
build/
29+
30+
### VS Code ###
31+
.vscode/

pom.xml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.2.0.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.wbs</groupId>
12+
<artifactId>java8-lambda</artifactId>
13+
<version>1.0.0-SNAPSHOT</version>
14+
<name>java8-lambda</name>
15+
<description>Learning java8 demo</description>
16+
17+
<properties>
18+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
20+
<java.version>1.8</java.version>
21+
</properties>
22+
23+
<dependencies>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-web</artifactId>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>org.projectlombok</groupId>
31+
<artifactId>lombok</artifactId>
32+
<optional>true</optional>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-test</artifactId>
37+
<scope>test</scope>
38+
<exclusions>
39+
<exclusion>
40+
<groupId>org.junit.vintage</groupId>
41+
<artifactId>junit-vintage-engine</artifactId>
42+
</exclusion>
43+
</exclusions>
44+
</dependency>
45+
</dependencies>
46+
47+
<build>
48+
<plugins>
49+
<plugin>
50+
<groupId>org.springframework.boot</groupId>
51+
<artifactId>spring-boot-maven-plugin</artifactId>
52+
</plugin>
53+
</plugins>
54+
</build>
55+
56+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.wbs.java8lambda;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Java8LambdaApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Java8LambdaApplication.class, args);
11+
}
12+
13+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.wbs.java8lambda.chapter1;
2+
3+
import com.wbs.java8lambda.entity.Apple;
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
import java.util.function.Predicate;
9+
10+
/**
11+
* 第一章示例
12+
*
13+
* @author: wangbingshuai
14+
* @create: 2019-10-28 15:06
15+
**/
16+
public class FilteringApples {
17+
public static void main(String[] args) {
18+
List<Apple> apples = Arrays.asList(new Apple(80, "green"),
19+
new Apple(155, "green"),
20+
new Apple(120, "red"));
21+
22+
List<Apple> apples1 = filterGreenApples(apples);
23+
System.out.println(apples1);
24+
25+
List<Apple> apples2 = filterHeavyApples(apples);
26+
System.out.println(apples2);
27+
28+
List<Apple> greenApples = filterApples(apples, FilteringApples::isGreenApple);
29+
System.out.println(greenApples);
30+
31+
List<Apple> heavyApples = filterApples(apples, FilteringApples::isHeavyApple);
32+
System.out.println(heavyApples);
33+
34+
List<Apple> greenApples2 = filterApples(apples, (Apple a) -> "green".equals(a.getColor()));
35+
System.out.println(greenApples2);
36+
37+
List<Apple> heavyApples2 = filterApples(apples, a -> a.getWeight() > 150);
38+
System.out.println(heavyApples2);
39+
40+
List<Apple> weirdApples = filterApples(apples, a -> a.getWeight() < 80 || "brown".equals(a.getColor()));
41+
System.out.println(weirdApples);
42+
}
43+
44+
private static boolean isGreenApple(Apple apple) {
45+
return "green".equals(apple.getColor());
46+
}
47+
48+
private static boolean isHeavyApple(Apple apple) {
49+
return apple.getWeight() > 150;
50+
}
51+
52+
private static List<Apple> filterApples(List<Apple> list, Predicate<Apple> p) {
53+
List<Apple> result = new ArrayList<>();
54+
for (Apple apple : list) {
55+
if (p.test(apple)) {
56+
result.add(apple);
57+
}
58+
}
59+
return result;
60+
}
61+
62+
private static List<Apple> filterGreenApples(List<Apple> list) {
63+
List<Apple> result = new ArrayList<>();
64+
for (Apple apple : list) {
65+
if ("green".equals(apple.getColor())) {
66+
result.add(apple);
67+
}
68+
}
69+
return result;
70+
}
71+
72+
private static List<Apple> filterHeavyApples(List<Apple> list) {
73+
List<Apple> result = new ArrayList<>();
74+
for (Apple apple : list) {
75+
if (apple.getWeight() > 150) {
76+
result.add(apple);
77+
}
78+
}
79+
return result;
80+
}
81+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.wbs.java8lambda.chapter2;
2+
3+
import com.wbs.java8lambda.entity.Apple;
4+
5+
/**
6+
* TODO
7+
*
8+
* @author: wangbingshuai
9+
* @create: 2019-10-28 16:57
10+
**/
11+
public class AppleColorPredicate implements ApplePredicate {
12+
@Override
13+
public boolean test(Apple apple) {
14+
return "green".equals(apple.getColor());
15+
}
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.wbs.java8lambda.chapter2;
2+
3+
import com.wbs.java8lambda.entity.Apple;
4+
5+
/**
6+
* TODO
7+
*
8+
* @author: wangbingshuai
9+
* @create: 2019-10-28 16:55
10+
**/
11+
public interface ApplePredicate {
12+
boolean test(Apple apple);
13+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.wbs.java8lambda.chapter2;
2+
3+
import com.wbs.java8lambda.entity.Apple;
4+
5+
/**
6+
* TODO
7+
*
8+
* @author: wangbingshuai
9+
* @create: 2019-10-28 16:58
10+
**/
11+
public class AppleRedAndHeavyPredicate implements ApplePredicate {
12+
@Override
13+
public boolean test(Apple apple) {
14+
return "red".equals(apple.getColor()) && apple.getWeight() > 150;
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.wbs.java8lambda.chapter2;
2+
3+
import com.wbs.java8lambda.entity.Apple;
4+
5+
/**
6+
* TODO
7+
*
8+
* @author: wangbingshuai
9+
* @create: 2019-10-28 16:56
10+
**/
11+
public class AppleWeightPredicate implements ApplePredicate {
12+
@Override
13+
public boolean test(Apple apple) {
14+
return apple.getWeight() > 150;
15+
}
16+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.wbs.java8lambda.chapter2;
2+
3+
import com.wbs.java8lambda.entity.Apple;
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
/**
10+
* 第二章示例
11+
*
12+
* @author: wangbingshuai
13+
* @create: 2019-10-28 16:48
14+
**/
15+
public class FilteringApples {
16+
public static void main(String[] args) {
17+
List<Apple> appleList = Arrays.asList(new Apple(80, "green"),
18+
new Apple(155, "green"),
19+
new Apple(120, "red"));
20+
21+
List<Apple> greenApples = filterAppleByColor(appleList, "green");
22+
System.out.println(greenApples);
23+
24+
List<Apple> redApples = filterAppleByColor(appleList, "red");
25+
System.out.println(redApples);
26+
27+
List<Apple> greenApples2 = filter(appleList, new AppleColorPredicate());
28+
System.out.println(greenApples2);
29+
30+
List<Apple> heavyApples = filter(appleList, new AppleWeightPredicate());
31+
System.out.println(heavyApples);
32+
33+
List<Apple> redAndHeavyApples = filter(appleList, new AppleRedAndHeavyPredicate());
34+
System.out.println(redAndHeavyApples);
35+
36+
List<Apple> redApples2 = filter(appleList, new ApplePredicate() {
37+
@Override
38+
public boolean test(Apple apple) {
39+
return apple.getColor().equals("red");
40+
}
41+
});
42+
System.out.println(redApples2);
43+
}
44+
45+
private static List<Apple> filterAppleByColor(List<Apple> list, String color) {
46+
List<Apple> result = new ArrayList<>();
47+
for (Apple apple : list) {
48+
if (apple.getColor().equals(color)) {
49+
result.add(apple);
50+
}
51+
}
52+
return result;
53+
}
54+
55+
private static List<Apple> filter(List<Apple> list, ApplePredicate predicate) {
56+
List<Apple> result = new ArrayList<>();
57+
for (Apple apple : list) {
58+
if (predicate.test(apple)) {
59+
result.add(apple);
60+
}
61+
}
62+
return result;
63+
}
64+
65+
private static List<Apple> filterGreenApples(List<Apple> list) {
66+
List<Apple> result = new ArrayList<>();
67+
for (Apple apple : list) {
68+
if ("green".equals(apple.getColor())) {
69+
result.add(apple);
70+
}
71+
}
72+
return result;
73+
}
74+
75+
private static List<Apple> filterApplesByWeight(List<Apple> list, int weight) {
76+
List<Apple> result = new ArrayList<>();
77+
for (Apple apple : list) {
78+
if (apple.getWeight() > weight) {
79+
result.add(apple);
80+
}
81+
}
82+
return result;
83+
}
84+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.wbs.java8lambda.chapter2;
2+
3+
/**
4+
* TODO
5+
*
6+
* @author: wangbingshuai
7+
* @create: 2019-10-28 17:10
8+
**/
9+
public class MeaningOfThis {
10+
public static int value = 4;
11+
12+
public void doIt() {
13+
int value = 6;
14+
Runnable runnable = new Runnable() {
15+
public final int value = 5;
16+
17+
@Override
18+
public void run() {
19+
int value = 10;
20+
System.out.println(this.value);
21+
}
22+
};
23+
runnable.run();
24+
}
25+
26+
public static void main(String[] args) {
27+
MeaningOfThis meaningOfThis = new MeaningOfThis();
28+
meaningOfThis.doIt();
29+
}
30+
31+
}

0 commit comments

Comments
 (0)