Skip to content

Commit 67b3e7a

Browse files
committed
update local
1 parent cfce5b7 commit 67b3e7a

3 files changed

Lines changed: 102 additions & 23 deletions

File tree

src/main/java/lambdasinaction/chap11/standard/Resume.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package lambdasinaction.chap11.standard;
22

33
public class Resume {
4+
5+
private StandardService standardService = new StandardService();
6+
47
private String company;
58

69
private String school;
@@ -9,6 +12,17 @@ public class Resume {
912

1013
private String title;
1114

15+
public Resume(String company, String school, String skill, String title) {
16+
this.company = company;
17+
this.school = school;
18+
this.skill = skill;
19+
this.title = title;
20+
}
21+
22+
public Resume() {
23+
24+
}
25+
1226
public String getCompany() {
1327
return company;
1428
}
@@ -41,4 +55,33 @@ public void setTitle(String title) {
4155
this.title = title;
4256
}
4357

58+
public Resume stadardCompany() {
59+
this.company = standardService.stadardCompany(this.company);
60+
return this;
61+
}
62+
63+
public Resume standardSchool() {
64+
this.school = standardService.standardSchool(this.school);
65+
return this;
66+
}
67+
68+
public Resume standardSkill() {
69+
this.skill = standardService.standardSkill(this.skill);
70+
return this;
71+
}
72+
73+
public Resume standardTitle() {
74+
this.title = standardService.standardTitle(this.title);
75+
return this;
76+
}
77+
78+
@Override
79+
public String toString() {
80+
return "Resume{" +
81+
"company='" + company + '\'' +
82+
", school='" + school + '\'' +
83+
", skill='" + skill + '\'' +
84+
", title='" + title + '\'' +
85+
'}';
86+
}
4487
}

src/main/java/lambdasinaction/chap11/standard/StandardService.java

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
import java.util.ArrayList;
44
import java.util.List;
55
import java.util.Random;
6-
import java.util.concurrent.CompletableFuture;
7-
import java.util.concurrent.Executor;
8-
import java.util.concurrent.Executors;
9-
import java.util.concurrent.ThreadFactory;
6+
import java.util.concurrent.*;
7+
import java.util.function.Supplier;
8+
import java.util.stream.Collectors;
109

1110
public class StandardService {
1211

@@ -19,7 +18,6 @@ public Thread newThread(Runnable r) {
1918
}
2019
});
2120

22-
2321
public String stadardCompany(String company) {
2422
delay(100);
2523
return "standard " + company;
@@ -53,36 +51,69 @@ public String buildTag(String com, String school, String skill, String title) {
5351
return com + school + skill + title + "tag";
5452
}
5553

56-
public void oldFun(List<Resume> resumes) {
54+
public List<Resume> oldFun(List<Resume> resumes) {
55+
List<Resume> result = new ArrayList<>();
5756
for (Resume resume : resumes) {
58-
long start = System.nanoTime();
5957
String company = this.stadardCompany(resume.getCompany());
6058
String school= this.standardSchool(resume.getSchool());
6159
String skill = this.standardSkill(resume.getSkill());
6260
String title = this.standardTitle(resume.getTitle());
63-
System.out.println(this.buildTag(company, school, skill, title));
64-
long retrivalTime = ((System.nanoTime() - start) / 1_000_000);
65-
System.out.println(retrivalTime);
61+
result.add(new Resume(company, school, skill, title));
6662
}
63+
System.out.println(result);
64+
return result;
6765
}
6866

69-
public void newFun(List<Resume> resumes) {
70-
// resumes.stream()
71-
// .map(resume -> CompletableFuture.supplyAsync(() -> stadardCompany(resume.getCompany()), executor))
72-
// .map(task -> task
73-
// .thenCompose(resume -> CompletableFuture.supplyAsync(()->standardSchool(resume.), executor)))
74-
// .map(task -> task
75-
// .thenCompose(resume -> CompletableFuture.supplyAsync(()->standardSchool(resume.gets), executor)));
67+
// public List<Resume> futureFun(List<Resume> resumes) {
68+
// List<Future>
69+
// }
70+
71+
public List<Resume> newFun(List<Resume> resumes) {
72+
List<CompletableFuture<Resume>> completableFutures = resumes.stream()
73+
.map(resume -> CompletableFuture.supplyAsync(resume::stadardCompany, executor))
74+
.map(task -> task
75+
.thenCompose(resume -> CompletableFuture.supplyAsync(resume::standardSchool, executor)))
76+
.map(task -> task
77+
.thenCompose(resume -> CompletableFuture.supplyAsync(resume::standardSkill, executor)))
78+
.map(task -> task
79+
.thenCompose(resume -> CompletableFuture.supplyAsync(resume::standardTitle, executor)))
80+
.collect(Collectors.toList());
7681

82+
List<Resume> collect = completableFutures.stream().map(CompletableFuture::join).collect(Collectors.toList());
83+
System.out.println(collect);
84+
return collect;
85+
}
86+
87+
public static void execute(String msg, Supplier<List<Resume>> supplier) {
88+
long s = System.nanoTime();
89+
supplier.get();
90+
long e = System.nanoTime();
91+
System.out.println(msg + " done in " + (e - s) / 1_000_000 + " mesc");
7792
}
7893

7994
public static void main(String[] args) {
80-
Resume resume = new Resume();
81-
resume.setCompany("A");
82-
resume.setSchool("B");
83-
resume.setSkill("C");
84-
resume.setTitle("D");
95+
List<Resume> resumes = new ArrayList<Resume>(){{
96+
add(new Resume("A1", "B1", "C1", "D1"));
97+
add(new Resume("A2", "B2", "C2", "D2"));
98+
add(new Resume("A3", "B3", "C3", "D3"));
99+
add(new Resume("A4", "B4", "C4", "D4"));
100+
add(new Resume("A5", "B5", "C5", "D5"));
101+
add(new Resume("A6", "B6", "C6", "D6"));
102+
add(new Resume("A7", "B7", "C7", "D7"));
103+
add(new Resume("A8", "B8", "C8", "D8"));
104+
add(new Resume("A9", "B9", "C9", "D9"));
105+
add(new Resume("A0", "B0", "C0", "D0"));
106+
}};
107+
108+
List<Resume> resumes2 = new ArrayList<>();
109+
110+
for (int i = 0; i < 1000; i++) {
111+
resumes2.add(new Resume("A"+i, "B0", "C0", "D0"));
112+
}
113+
85114
StandardService standardService = new StandardService();
86-
// standardService.oldFun(new ArrayList<Resume>(){{add(resume)}});
115+
// execute("oldFun",()->standardService.oldFun(resumes) );
116+
execute("newFun",()->standardService.newFun(resumes) );
87117
}
118+
88119
}

src/main/java/lambdasinaction/chap14/Currying.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ static double converter(double x, double y, double z) {
2222
return x * y + z;
2323
}
2424

25+
26+
static DoubleUnaryOperator curriedConverter2(double f, double b) {
27+
return operand -> operand * f + b;
28+
}
29+
2530
static DoubleUnaryOperator curriedConverter(double y, double z) {
2631
return (double x) -> x * y + z;
2732
}

0 commit comments

Comments
 (0)