-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethRefStr.java
More file actions
55 lines (49 loc) · 1.7 KB
/
MethRefStr.java
File metadata and controls
55 lines (49 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// LDG-MethRefStr/MethRefStr.java
import java.util.Random;
import java.util.stream.DoubleStream;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class MethRefStr {
public static void main(String[] args) {
Random r = new Random(); // effectively final
System.out.println(
// Supplier of double's expected
DoubleStream.generate(r::nextGaussian)
// we want just ten million numbers
.limit(10_000_000)
// reduction to DoubleSummaryStatistics
.summaryStatistics()
// arithmetic average of all numbers
.getAverage());
System.out.println(
Stream.of(new Person("C"), new Person("A"),
new Person("D"), new Person("B"))
// Function<Person,otherType> expected
.map(Person::getName)
.sorted()
// Function<String,otherType> expected
.map(String::toLowerCase)
// reduction to a single String
.collect(Collectors.joining("-")));
Thread t = new Thread(MethRefStr::fibos);
t.start();
try {
t.join();
} catch(InterruptedException ignore) { }
}
public static void fibos() {
StringBuilder sb = new StringBuilder("0, 1");
int a = 0, b = 1;
for (int i = 0; i < 8; ++i) {
b += a;
a = b - a;
sb.append(", " + b);
}
System.out.println(sb);
}
}
class Person {
private String name;
public Person(String n) { name = n; }
public String getName() { return name; }
}