Skip to content

Instantly share code, notes, and snippets.

@mcupak
Created January 5, 2018 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcupak/33b792ea0d2716ceff1af0e10d3791e7 to your computer and use it in GitHub Desktop.
Save mcupak/33b792ea0d2716ceff1af0e10d3791e7 to your computer and use it in GitHub Desktop.
Session notes from the Java 9, Episode 2 talk at Singapore Java User Group.
1+1
int x = 1+1
System.out.println(x)
Thread.sleep(2000)
/vars
/types
/list
/l
/help
Set<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(i -> i<5).forEach(System.out::println)
IntStream.range(0, 10).dropWhile(i -> i<5).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()
List<Integer> list = List.of(2,3,4,7,9,11)
list.stream().filter(i -> i >5).collect(Collectors.groupingBy(j -> j %2, Collectors.counting()))
list.stream().filter(i -> i >5).collect(Collectors.groupingBy(j -> j %2, Collectors.counting()))
list.stream().collect(Collectors.groupingBy(j -> j %2, Collectors.filtering(i -> i>5, Collectors.counting())))
Stream.of(entry(1, Set.of("a", "b")),entry(1, Set.of("a", "c")),entry(2, Set.of("d")))
Stream<Map.Entry<Integer,Set<String>>> entries = Stream.of(entry(1, Set.of("a", "b")),entry(1, Set.of("a", "c")),entry(2, Set.of("d")))
entries.collect(Collectors.groupingBy(e -> e.getKey(), Collectors.mapping(e -> e.getValue(), Collectors.toSet())))
entries.collect(Collectors.groupingBy(e -> e.getKey(), Collectors.flatMapping(e -> e.getValue().stream(), Collectors.toSet())))
Stream<Map.Entry<Integer,Set<String>>> entries = Stream.of(entry(1, Set.of("a", "b")),entry(1, Set.of("a", "c")),entry(2, Set.of("d")))
entries.collect(Collectors.groupingBy(e -> e.getKey(), Collectors.flatMapping(e -> e.getValue().stream(), Collectors.toSet())))
CompletableFuture<String> cf = new CompletableFuture<String>()
cf.complete("done")
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
cf.get()
CompletableFuture<String> cf = new CompletableFuture<String>()
cf.orTimeout(5, TimeUnit.SECONDS)
cf
cf
cf
cf
cf
cf
cf.get()
CompletableFuture<String> cf = new CompletableFuture<String>()
CompletableFuture<String> copy = cf.copy()
cf
copy
cf.complete("done")
cf
copy
copy.get()
CompletableFuture<String> cf = new CompletableFuture<String>()
CompletableFuture<String> copy = cf.copy()
copy.complete("done")
copy
cf
StackTraceElement[] sst = new Throwable().getStackTrace()
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.collect(Collectors.toList()))
StackWalker.getInstance(Option.RETAIN_CLASS_REFERENCE).walk(s -> s.map(f -> f.getDeclaringClass()).collect(Collectors.toList()))
StackWalker.getInstance(Option.RETAIN_CLASS_REFERENCE).walk(s -> s.map(f -> f.getDeclaringClass()).filter(c -> c.equals(jdk.jshell.execution.Util.class)).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()).forEach(System.out::println)
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 singapore";
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()
JShell js = JShell.create()
List<SnippetEvent> events = js.eval("1+1")
js.variables()
js.variables().forEach(System.out::println)
events.stream().map(e -> e.status()).forEach(System.out::println)
events.stream().map(e -> e.snippet().source()).forEach(System.out::println)
events.stream().map(e -> e.value()).forEach(System.out::println)
/save -history singajug.jsh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment