-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
305 lines (233 loc) · 8.12 KB
/
Copy pathMain.java
File metadata and controls
305 lines (233 loc) · 8.12 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package be.dog.d.steven;
import be.dog.d.steven.placeholders.FooClass;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.*;
import java.util.concurrent.Future;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
/** Java 9 **/
/** PROCESS API **/
// More control / methods for operating-system processes
try {
Process p = Runtime.getRuntime().exec("help");
Future<Integer> exitValue = p.onExit().thenApply(Process::exitValue);
} catch (IOException e) {
e.printStackTrace();
}
/** COLLECTION UTILS **/
List<Integer> list = List.of(1, 2, 3);
Set<String> set = Set.of("test1", "test2");
Map<Integer, String> map = Map.of(1,"test",2,"test");
/** OPTIONAL::STREAM **/
List<Optional<String>> listOfOptionals = Arrays.asList(
Optional.empty(), Optional.of("test"), Optional.empty(), Optional.of("test"));
// OLD
List<String> filteredList = listOfOptionals.stream()
.flatMap(o -> o.isPresent() ? Stream.of(o.get()) : Stream.empty())
.collect(Collectors.toList());
System.out.println(filteredList);
// NEW
filteredList = listOfOptionals.stream()
.flatMap(Optional::stream)
.collect(Collectors.toList());
System.out.println(filteredList);
/** DIAMOND OPERATOR EXTENSION **/
FooClass<Integer> fc = new FooClass<>(1) {
// anonymous inner class
};
FooClass<? extends Integer> fc0 = new FooClass<>(1) {
// anonymous inner class
};
FooClass<?> fc1 = new FooClass<>(1) {
// anonymous inner class
};
/** INTERFACE PRIVATE METHOD **/
interface InterfaceWithPrivateMethods {
private static String staticPrivate() {
return "static private";
}
private String instancePrivate() {
return "instance private";
}
default void check() {
String result = staticPrivate();
InterfaceWithPrivateMethods pvt = new InterfaceWithPrivateMethods() {
// anonymous class
};
result = pvt.instancePrivate();
}
}
/** Java 10 **/
/** COPY OF **/
List<Integer> copyList = List.copyOf(list);
// copyList.add(4); // Immutable
/** TO UNMODIFIABLE **/
List<Integer> evenList = list.stream()
.filter(i -> i % 2 == 0)
.collect(Collectors.toUnmodifiableList());
// evenList.add(4); // Immutable
/** OR ELSE THROW **/
Integer firstEven = list.stream()
.filter(i -> i % 2 == 0)
.findFirst()
.orElseThrow();
System.out.println(firstEven);
/** Java 11 **/
/** STRINGS **/
var example1 = " testing...";
// Repeat
System.out.println(example1.repeat(3));
// Strip (unicode standard) ~ Trim
System.out.println(example1.strip().repeat(3));
// Blank check
System.out.println(example1.isBlank());
var example2 = "Tests:\ntesting...\rtesting...\n...";
// Lines iterator
System.out.println(example2.lines().count());
example2.lines().filter(l -> !l.contains("T")).forEach(System.out::println);
/** LAMBDAS WITH ANNOTATIONS **/
Consumer<BigDecimal> moneyConsumer = (@NotNull var money) -> System.out.printf("I have %.2f $\n", money);
/** HTTP CLIENT **/
HttpClient client = HttpClient.newBuilder().build();
HttpRequest request = HttpRequest.newBuilder()
.GET()
.uri(URI.create("https://www.google.com"))
.build();
HttpResponse<String> response = null;
try {
response = client.send(request, HttpResponse.BodyHandlers.ofString());
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
assert response != null;
System.out.println(response.body());
/** UNICODE 10 **/
System.out.println("\u20BF");
/** Java 12 **/
/** STRINGS **/
// Indent
System.out.println("Java 12".indent(30));
/** SWITCH STATEMENT **/
// OLD
String test = "c";
String answer;
switch (test){
case "a":
answer = "The variable was 'a'";
break;
case "b":
answer = "The variable was 'b'";
break;
case "c":
answer = "The variable was 'c'";
break;
default:
answer = "The variable was neither a, b or c";
}
System.out.println(answer);
// NEW
answer = switch(test){
case "a" -> "The variable was 'a'";
case "b" -> "The variable was 'b'";
case "c" -> "The variable was 'c'";
default -> "The variable was neither a, b or c";
};
System.out.println(answer);
/** Java 13 **/
/** SWITCH STATEMENT **/
// NEW
answer = switch (test) {
case "a" -> {
System.out.println("I am not just yielding!");
yield "The variable was 'a'";
}
case "b" -> {
System.out.println("Me neither.");
yield "The variable was 'b'";
}
case "c" -> {
System.out.println("Me neither.");
yield "The variable was 'c'";
}
default -> {
System.out.println("OK");
yield "The variable was neither a, b or c";
}
};
System.out.println(answer);
/** PATTERN MATCHING **/
// OLD
Object obj = "Java 13";
if (obj instanceof String){
String string = (String) obj;
System.out.println(string.toUpperCase());
}
//NEW
if(obj instanceof String s){
System.out.println(s.toUpperCase());
}
/** TEXT BLOCKS **/
// OLD
String textBlock =
"This is how\n" +
"TEXT BLOCKS\n" +
"had to be concatenated\n" +
"before this feature";
System.out.println(textBlock);
// NEW
textBlock = """
This is how we can
create TEXT BLOCKS
using this new \
feature.""";
System.out.println(textBlock);
/** Java 14 **/
/** Records **/
// OLD
class Student{
private String first;
private String last;
private int age;
public Student(String first, String last, int age) {
this.first = first;
this.last = last;
this.age = age;
}
public int getAge() {
return age;
}
public String getFirst() {
return first;
}
public String getLast() {
return last;
}
}
Student s = new Student("Steven", "D'Hondt", 31);
System.out.println(s.first);
System.out.println(s.getFirst());
// NEW
record Programmer(String first, String last, int age){}
Programmer p = new Programmer("Steven", "D'Hondt",31);
System.out.println(p.first);
System.out.println(p.first());
/** Java 15 **/
}
/** SEALED CLASSES **/
sealed interface Command permits LoginCommand, LogoutCommand{
//...
}
public final class LoginCommand implements Command{
}
public final class LogoutCommand implements Command{
}
}