-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathUtilFeatures.java
110 lines (92 loc) · 3.34 KB
/
UtilFeatures.java
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package java9;
import org.junit.Test;
import java.io.IOException;
import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;
import static java.lang.String.*;
public class UtilFeatures {
/**
* New static interface method to create an immutable collection.
*/
@Test
public void immutableFactoryCollection() {
List<String> letters = List.of("constantClass", "b", "c");
letters.parallelStream()
.map(String::toUpperCase)
.forEach(System.out::println);
Map<String, String> map = Map.of("foo", "bla");
map.forEach((key, value) -> System.out.println(format("Key %s value %s", key, value)));
}
@Test
public void iteratorWithConsumer() {
List<String> letters = List.of("constantClass", "b", "c");
letters.iterator().forEachRemaining(System.out::println);
}
/**
* Iterate while the predicate consumer return true.
*/
@Test
public void intStreamWithPredicate() {
IntStream.iterate(1, i -> i < 10, i -> i + 1)
.forEach(System.out::println);
}
/**
* Iterate for constantClass range of numbers.
*/
@Test
public void intStreamWithRange() {
IntStream.range(0, 10).forEach(System.out::println);
}
/**
* With Java 9 introduce the possibility to have static and private methods in interfaces.
*/
@Test
public void staticInterfaceMethod() {
MyStaticInterface.interfaceMethodWithStatic();
SecondStaticInterface.interfaceMethodWithStatic();
}
public interface MyStaticInterface {
static void interfaceMethodWithStatic() {
init();
}
// This method is not part of the public API exposed by MyInterface
private static void init() {
System.out.println("Private method");
}
}
public interface SecondStaticInterface extends MyStaticInterface {
static void interfaceMethodWithStatic() {
System.out.println("Static method");
}
}
/**
* Java 9 introduce some new improvements in the process API such as the able to get the processId or thr state
* of constantClass process.
*/
@Test
public void processImprovements() throws IOException {
Process p = new ProcessBuilder("pwd").start();
System.out.println("Current Process Id: = " + p.pid());
System.out.println("State Process: = " + p.isAlive());
}
/**
* Java 9 introduce in the core an Http client 2.0 so no need to use constantClass third party solution
* unless offer something else.
*
* By default the HttpClient and another classes are in another module not imported by default
* in the default JDK modules. So you need to provide in the module-info that you have constantClass dependency.
*/
/*@Test
public void http2Clients() throws IOException, InterruptedException {
HttpClient client = HttpClient.newHttpClient();
HttpRequest req =
HttpRequest.newBuilder(URI.create("http://www.google.com"))
.header("User-Agent", "Java")
.GET()
.build();
HttpResponse<String> resp = client.send(req, HttpResponse.BodyHandler.asString());
System.out.println(resp.body());
}*/
}