Skip to content

Commit 2bd399c

Browse files
committed
[add] add module java8
1 parent d3e2afa commit 2bd399c

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

java8/pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>java-learning</artifactId>
7+
<groupId>com.brianway.learning.java</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>java8</artifactId>
13+
14+
</project>
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.brianway.learning.java8.lambda;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import java.util.function.Predicate;
7+
8+
/**
9+
* Created by brian on 16/12/26.
10+
*/
11+
public class FilteringApples {
12+
public static void main(String[] args) {
13+
List<Apple> inventory = Arrays.asList(new Apple(80, "green"),
14+
new Apple(155, "green"),
15+
new Apple(120, "red"));
16+
17+
List<Apple> greenApples = filter(inventory, FilteringApples::isGreenApple);
18+
System.out.println(greenApples);
19+
20+
List<Apple> greenApples2 = filter(inventory, (Apple a) -> "green".equals(a.getColor()));
21+
System.out.println(greenApples2);
22+
23+
List<Apple> heavyApples = filter(inventory, FilteringApples::isHeavyApple);
24+
System.out.println(heavyApples);
25+
26+
List<Apple> heavyApples2 = filter(inventory, (Apple a) -> a.getWeight() > 150);
27+
System.out.println(heavyApples2);
28+
29+
}
30+
31+
public static List<Apple> filter(List<Apple> inventory, Predicate<Apple> p) {
32+
List<Apple> result = new ArrayList<>();
33+
for (Apple apple : inventory) {
34+
if (p.test(apple)) {
35+
result.add(apple);
36+
}
37+
}
38+
return result;
39+
}
40+
41+
public static boolean isGreenApple(Apple apple) {
42+
return "green".equals(apple.getColor());
43+
}
44+
45+
public static boolean isHeavyApple(Apple apple) {
46+
return apple.getWeight() > 150;
47+
}
48+
49+
public static class Apple {
50+
private int weight = 0;
51+
private String color = "";
52+
53+
public Apple(int weight, String color) {
54+
this.weight = weight;
55+
this.color = color;
56+
}
57+
58+
public Integer getWeight() {
59+
return weight;
60+
}
61+
62+
public void setWeight(Integer weight) {
63+
this.weight = weight;
64+
}
65+
66+
public String getColor() {
67+
return color;
68+
}
69+
70+
public void setColor(String color) {
71+
this.color = color;
72+
}
73+
74+
public String toString() {
75+
return "Apple{" +
76+
"color='" + color + '\'' +
77+
", weight=" + weight +
78+
'}';
79+
}
80+
}
81+
}

pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
<properties>
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1515
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
16+
<maven.compiler.source>1.8</maven.compiler.source>
17+
<maven.compiler.target>1.8</maven.compiler.target>
1618
<junit.version>4.11</junit.version>
1719
</properties>
1820

@@ -48,6 +50,7 @@
4850
<module>java-base</module>
4951
<module>java-container</module>
5052
<module>java-io</module>
53+
<module>java8</module>
5154
</modules>
5255

5356
<dependencyManagement>

0 commit comments

Comments
 (0)