Skip to content

Commit 6cc5c3b

Browse files
committed
initial commit
0 parents  commit 6cc5c3b

File tree

7 files changed

+441
-0
lines changed

7 files changed

+441
-0
lines changed

chap1/FilteringApples.java

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import java.util.*;
2+
import java.util.function.Predicate;
3+
public class FilteringApples{
4+
5+
public static void main(String ... args){
6+
7+
List<Apple> inventory = Arrays.asList(new Apple(80,"green"), new Apple(155, "green"), new Apple(120, "red"));
8+
9+
// [Apple{color='green', weight=80}, Apple{color='green', weight=155}]
10+
List<Apple> greenApples = filterApples(inventory, FilteringApples::isGreenApple);
11+
System.out.println(greenApples);
12+
13+
// [Apple{color='green', weight=155}]
14+
List<Apple> heavyApples = filterApples(inventory, FilteringApples::isHeavyApple);
15+
System.out.println(heavyApples);
16+
17+
// [Apple{color='green', weight=80}, Apple{color='green', weight=155}]
18+
List<Apple> greenApples2 = filterApples(inventory, (Apple a) -> "green".equals(a.getColor()));
19+
System.out.println(greenApples2);
20+
21+
// [Apple{color='green', weight=155}]
22+
List<Apple> heavyApples2 = filterApples(inventory, (Apple a) -> a.getWeight() > 150);
23+
System.out.println(heavyApples2);
24+
25+
// []
26+
List<Apple> weirdApples = filterApples(inventory, (Apple a) -> a.getWeight() < 80 ||
27+
"brown".equals(a.getColor()));
28+
System.out.println(weirdApples);
29+
30+
31+
32+
}
33+
34+
public static List<Apple> filterGreenApples(List<Apple> inventory){
35+
List<Apple> result = new ArrayList<>();
36+
for (Apple apple: inventory){
37+
if ("green".equals(apple.getColor())) {
38+
result.add(apple);
39+
}
40+
}
41+
return result;
42+
}
43+
44+
public static List<Apple> filterHeavyApples(List<Apple> inventory){
45+
List<Apple> result = new ArrayList<>();
46+
for (Apple apple: inventory){
47+
if (apple.getWeight() > 150) {
48+
result.add(apple);
49+
}
50+
}
51+
return result;
52+
}
53+
54+
public static boolean isGreenApple(Apple apple) {
55+
return "green".equals(apple.getColor());
56+
}
57+
public static boolean isHeavyApple(Apple apple) {
58+
return apple.getWeight() > 150;
59+
}
60+
61+
62+
63+
public static List<Apple> filterApples(List<Apple> inventory, Predicate<Apple> p){
64+
List<Apple> result = new ArrayList<>();
65+
for(Apple apple : inventory){
66+
if(p.test(apple)){
67+
result.add(apple);
68+
}
69+
}
70+
return result;
71+
}
72+
73+
public static class Apple {
74+
private int weight = 0;
75+
private String color = "";
76+
77+
public Apple(int weight, String color){
78+
this.weight = weight;
79+
this.color = color;
80+
}
81+
82+
public Integer getWeight() {
83+
return weight;
84+
}
85+
86+
public void setWeight(Integer weight) {
87+
this.weight = weight;
88+
}
89+
90+
public String getColor() {
91+
return color;
92+
}
93+
94+
public void setColor(String color) {
95+
this.color = color;
96+
}
97+
98+
public String toString() {
99+
return "Apple{" +
100+
"color='" + color + '\'' +
101+
", weight=" + weight +
102+
'}';
103+
}
104+
}
105+
106+
}

chap2/FilteringApples.java

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import java.util.*;
2+
public class FilteringApples{
3+
4+
public static void main(String ... args){
5+
6+
List<Apple> inventory = Arrays.asList(new Apple(80,"green"), new Apple(155, "green"), new Apple(120, "red"));
7+
8+
// [Apple{color='green', weight=80}, Apple{color='green', weight=155}]
9+
List<Apple> greenApples = filterApplesByColor(inventory, "green");
10+
System.out.println(greenApples);
11+
12+
// [Apple{color='red', weight=120}]
13+
List<Apple> redApples = filterApplesByColor(inventory, "red");
14+
System.out.println(redApples);
15+
16+
// [Apple{color='green', weight=80}, Apple{color='green', weight=155}]
17+
List<Apple> greenApples2 = filter(inventory, new AppleColorPredicate());
18+
System.out.println(greenApples2);
19+
20+
// [Apple{color='green', weight=155}]
21+
List<Apple> heavyApples = filter(inventory, new AppleWeightPredicate());
22+
System.out.println(heavyApples);
23+
24+
// []
25+
List<Apple> redAndHeavyApples = filter(inventory, new AppleRedAndHeavyPredicate());
26+
System.out.println(redAndHeavyApples);
27+
28+
// [Apple{color='red', weight=120}]
29+
List<Apple> redApples2 = filter(inventory, new ApplePredicate() {
30+
public boolean test(Apple a){
31+
return a.getColor().equals("red");
32+
}
33+
});
34+
System.out.println(redApples2);
35+
36+
}
37+
38+
public static List<Apple> filterGreenApples(List<Apple> inventory){
39+
List<Apple> result = new ArrayList<>();
40+
for(Apple apple: inventory){
41+
if("green".equals(apple.getColor())){
42+
result.add(apple);
43+
}
44+
}
45+
return result;
46+
}
47+
48+
public static List<Apple> filterApplesByColor(List<Apple> inventory, String color){
49+
List<Apple> result = new ArrayList<>();
50+
for(Apple apple: inventory){
51+
if(apple.getColor().equals(color)){
52+
result.add(apple);
53+
}
54+
}
55+
return result;
56+
}
57+
58+
public static List<Apple> filterApplesByWeight(List<Apple> inventory, int weight){
59+
List<Apple> result = new ArrayList<>();
60+
for(Apple apple: inventory){
61+
if(apple.getWeight() > weight){
62+
result.add(apple);
63+
}
64+
}
65+
return result;
66+
}
67+
68+
69+
public static List<Apple> filter(List<Apple> inventory, ApplePredicate p){
70+
List<Apple> result = new ArrayList<>();
71+
for(Apple apple : inventory){
72+
if(p.test(apple)){
73+
result.add(apple);
74+
}
75+
}
76+
return result;
77+
}
78+
79+
public static class Apple {
80+
private int weight = 0;
81+
private String color = "";
82+
83+
public Apple(int weight, String color){
84+
this.weight = weight;
85+
this.color = color;
86+
}
87+
88+
public Integer getWeight() {
89+
return weight;
90+
}
91+
92+
public void setWeight(Integer weight) {
93+
this.weight = weight;
94+
}
95+
96+
public String getColor() {
97+
return color;
98+
}
99+
100+
public void setColor(String color) {
101+
this.color = color;
102+
}
103+
104+
public String toString() {
105+
return "Apple{" +
106+
"color='" + color + '\'' +
107+
", weight=" + weight +
108+
'}';
109+
}
110+
}
111+
112+
interface ApplePredicate{
113+
public boolean test(Apple a);
114+
}
115+
116+
static class AppleWeightPredicate implements ApplePredicate{
117+
public boolean test(Apple apple){
118+
return apple.getWeight() > 150;
119+
}
120+
}
121+
static class AppleColorPredicate implements ApplePredicate{
122+
public boolean test(Apple apple){
123+
return "green".equals(apple.getColor());
124+
}
125+
}
126+
127+
static class AppleRedAndHeavyPredicate implements ApplePredicate{
128+
public boolean test(Apple apple){
129+
return "red".equals(apple.getColor())
130+
&& apple.getWeight() > 150;
131+
}
132+
}
133+
}

chap2/MeaningOfThis.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public class MeaningOfThis
2+
{
3+
public final int value = 4;
4+
public void doIt()
5+
{
6+
int value = 6;
7+
Runnable r = new Runnable(){
8+
public final int value = 5;
9+
public void run(){
10+
int value = 10;
11+
System.out.println(this.value);
12+
}
13+
};
14+
r.run();
15+
}
16+
public static void main(String...args)
17+
{
18+
MeaningOfThis m = new MeaningOfThis();
19+
m.doIt(); // ???
20+
}
21+
}

chap3/ExecuteAround.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.io.*;
2+
public class ExecuteAround {
3+
4+
public static void main(String ...args) throws IOException{
5+
String oneLine = processFile((BufferedReader b) -> b.readLine());
6+
System.out.println(oneLine);
7+
8+
String twoLines = processFile((BufferedReader b) -> b.readLine() + b.readLine());
9+
System.out.println(twoLines);
10+
11+
}
12+
13+
public static String processFile(BufferedReaderProcessor p) throws IOException {
14+
try(BufferedReader br = new BufferedReader(new FileReader("data.txt"))){
15+
return p.process(br);
16+
}
17+
18+
}
19+
20+
public interface BufferedReaderProcessor{
21+
public String process(BufferedReader b) throws IOException;
22+
23+
}
24+
}

chap3/Lambdas.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import java.util.*;
2+
public class Lambdas {
3+
public static void main(String ...args){
4+
5+
// Simple example
6+
Runnable r = () -> System.out.println("Hello!");
7+
r.run();
8+
9+
// Filtering with lambdas
10+
List<Apple> inventory = Arrays.asList(new Apple(80,"green"), new Apple(155, "green"), new Apple(120, "red"));
11+
12+
// [Apple{color='green', weight=80}, Apple{color='green', weight=155}]
13+
List<Apple> greenApples = filter(inventory, (Apple a) -> "green".equals(a.getColor()));
14+
System.out.println(greenApples);
15+
16+
17+
Comparator<Apple> c = (Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight());
18+
19+
// [Apple{color='green', weight=80}, Apple{color='red', weight=120}, Apple{color='green', weight=155}]
20+
inventory.sort(c);
21+
System.out.println(inventory);
22+
}
23+
24+
public static List<Apple> filter(List<Apple> inventory, ApplePredicate p){
25+
List<Apple> result = new ArrayList<>();
26+
for(Apple apple : inventory){
27+
if(p.test(apple)){
28+
result.add(apple);
29+
}
30+
}
31+
return result;
32+
}
33+
34+
public static class Apple {
35+
private int weight = 0;
36+
private String color = "";
37+
38+
public Apple(int weight, String color){
39+
this.weight = weight;
40+
this.color = color;
41+
}
42+
43+
public Integer getWeight() {
44+
return weight;
45+
}
46+
47+
public void setWeight(Integer weight) {
48+
this.weight = weight;
49+
}
50+
51+
public String getColor() {
52+
return color;
53+
}
54+
55+
public void setColor(String color) {
56+
this.color = color;
57+
}
58+
59+
public String toString() {
60+
return "Apple{" +
61+
"color='" + color + '\'' +
62+
", weight=" + weight +
63+
'}';
64+
}
65+
}
66+
67+
interface ApplePredicate{
68+
public boolean test(Apple a);
69+
}
70+
}

0 commit comments

Comments
 (0)