Big thanks to Oracle for providing me with a platform to speak and share my favourite APIs in JDK 9.
In the talk, we covered smaller APIs, which don’t get a lot of emphasis, or are less known in the shadow of modularity. Special focus was put on APIs that enable us to write cleaner code. We talked about convenience factory methods for collections, Stream API enhancements, CompletableFuture API improvements, Stack-walking API, Process API updates, and the HTTP 2 client. This was a live demonstration using JShell, Java’s implementation of REPL.
If you’ve missed the talk at JavaOne, you can watch it here:
Feel free to browse the slides (download here):
Use this session export to try the commands yourself:
ls | |
1+1 | |
int x = 1+1 | |
System.out.println(x) | |
/vars | |
/types | |
/list | |
/l | |
/help | |
HashSet<String> set = new HashSet<String>() | |
set.add("a") | |
set.add("b") | |
set.add("c") | |
Collections.unmodifiableSet(set) | |
List<Integer> list = List.of(1,2,3) | |
list.add(4) | |
list.getClass() | |
Map.ofEntries(entry(1, "hello")) | |
IntStream.range(0, 10).forEach(System.out::println) | |
IntStream.range(0, 10).limit(5).forEach(System.out::println) | |
IntStream.range(0, 10).skip(5).forEach(System.out::println) | |
IntStream.range(0, 10).takeWhile(x -> x<5).forEach(System.out::println) | |
IntStream.range(0, 10).dropWhile(x -> x<5).forEach(System.out::println) | |
IntStream.iterate(0, i -> i+2).forEach(System.out::println) | |
IntStream.iterate(0, i -> i+2).filter(j -> j<100).forEach(System.out::println) | |
IntStream.iterate(0, i ->i<100,i -> i+2).forEach(System.out::println) | |
Stream.of(1) | |
Stream.of(null) | |
Stream.ofNullable(null) | |
Stream.ofNullable(null).count() | |
CompletableFuture<String> cf = new CompletableFuture<String>() | |
cf.complete("done") | |
cf | |
cf.get() | |
CompletableFuture<String> cf = new CompletableFuture<String>() | |
cf.get() | |
cf.completeExceptionally(new IllegalStateException()) | |
cf.get() | |
CompletableFuture<String> cf = new CompletableFuture<String>() | |
cf.completeOnTimeout("timed out", 5, TimeUnit.SECONDS) | |
cf | |
cf | |
cf | |
cf.get() | |
CompletableFuture<String> cf = new CompletableFuture<String>() | |
cf.orTimeout(5, TimeUnit.SECONDS) | |
cf.get() | |
CompletableFuture<String> cf = new CompletableFuture<String>() | |
CompletableFuture<String> copy = cf.copy() | |
cf.complete("done") | |
cf | |
copy | |
CompletableFuture<String> cf = new CompletableFuture<String>() | |
CompletableFuture<String> copy = cf.copy() | |
copy.complete("done") | |
copy | |
cf | |
StackTraceElement[] st = new Throwable().getStackTrace() | |
st | |
StackWalker.getInstance().walk(s -> s.collect(Collectors.toList())) | |
StackWalker.getInstance().walk(s -> s.limit(3).collect(Collectors.toList())) | |
StackWalker.getInstance(Option.RETAIN_CLASS_REFERENCE).walk(s -> s.map(f -> f.getDeclaringClass()).collect(Collectors.toList())) | |
new ProcessBuilder().command("jps").start() | |
ProcessHandle.current().pid() | |
ProcessHandle.current().info() | |
ProcessHandle.current().info().commandLine() | |
ProcessHandle.allProcesses().map(p -> p.info().command()).collect(Collectors.toList()) | |
new ProcessBuilder().command("sleep", "3").start().toHandle().onExit().thenAccept(System.out::println) | |
ProcessBuilder jps = new ProcessBuilder().command("jps") | |
ProcessBuilder grep = new ProcessBuilder().command("grep", "JShell") | |
List<Process> pipeline = ProcessBuilder.startPipeline(List.of(jps, grep)) | |
pipeline | |
new BufferedReader(new InputStreamReader(pipeline.get(1).getInputStream())).readLine() | |
HttpHandler handler = he -> { | |
String body = "hello javaone"; | |
he.sendResponseHeaders(200, body.length()); | |
try (OutputStream os = he.getResponseBody()) { | |
os.write(body.getBytes()); | |
} | |
} | |
/l | |
HttpServer hs = HttpServer.create(new InetSocketAddress(8000), 0) | |
hs.createContext("/hello", handler) | |
hs.start() | |
URI uri = new URI("http://localhost:8000/hello") | |
HttpURLConnection c = (HttpURLConnection) uri.toURL().openConnection() | |
c.setRequestMethod("GET") | |
c.getResponseCode() | |
new BufferedReader(new InputStreamReader(c.getInputStream())).readLine() | |
HttpClient client = HttpClient.newHttpClient() | |
HttpRequest request = HttpRequest.newBuilder().uri(uri).GET().build() | |
HttpResponse<String> response = client.send(request, BodyHandler.asString()) | |
response.statusCode() | |
response.body() | |
CompletableFuture<HttpResponse<String>> response = client.sendAsync(request, BodyHandler.asString()) | |
CompletableFuture<HttpResponse<String>> response = client.sendAsync(request, BodyHandler.asString()) | |
response.get().body() | |
/save -history backup.jsh |
Comments