Skip to content

Instantly share code, notes, and snippets.

@mcupak
Created March 3, 2020 22:05
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/07d776a42cf910a7645ff426333a20bd to your computer and use it in GitHub Desktop.
Save mcupak/07d776a42cf910a7645ff426333a20bd to your computer and use it in GitHub Desktop.
Export of my JShell session from The Good, the Bad and the Ugly of Java API design talk at DevNexus 2020.
Set<Integer> set = new HashSet<Integer>()
set.add(1)
set.add(2)
set.add(3)
set = Collections.unmodifiableSet(set)
Collections.unmodifiableSet(new HashSet<>Arrays.asList(1,2,3)))
Collections.unmodifiableSet(new HashSet<>Arrays.asList(1,2,3))
Collections.unmodifiableSet(new HashSet<>(Arrays.asList(1,2,3)))
Collections.unmodifiableSet(new HashSet<Integer>() {{ add(1); add(2); add(3); }})
ImmutableSet.of(1,2,3)
Set<Integer> set2 = new HashSet<Integer>(set)
set2.add(4)
Set<Integer> set2 = Collections.unmodifiableSet(new HashSet<Integer>(set))
set2.add(4)
Set.of(1,2,3)
Map.ofEntries(entry(1, "hello"))
Set<Integer> set2 = Set.copyOf(set)
set2.add(4)
List.copyOf(set)
set.stream().collect(toUnmodifiableList())
StackTraceElement[] st = new Throwable().getStackTrace()
Thread.currentThread().getStackTrace()
StackWalker.getInstance().walk(s -> s.collect(toList()
))
StackWalker.getInstance().walk(s -> s.limit(3).collect(toList()))
StackWalker.getInstance(Option.RETAIN_CLASS_REFERENCE).walk(s -> s.map(f -> f.getDeclaringClass()).collect(toList()))
new ProcessBuilder().command("jps").start()
new DefaultExecutor().execute(CommandLine.parse("jps"))
ProcessHandle.current().pid()
ProcessHandle.current().info()
ProcessHandle.current().info().commandLine()
ProcessHandle.allProcesses().map(p -> p.info().command()).collect(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
pipeline.get(1).getInputStream().transferTo(System.out)
HttpHandler handler = he -> {
String body = "hello devnexus";
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 = URI.create("http://localhost:8000/hello")
HttpURLConnection c = (HttpURLConnection) uri.toURL().openConnection()
c.setRequestMethod("GET")
c.getResponseCode()
new BufferedReader(new InputStreamReader(c.getInputStream())).readLine()
CloseableHttpClient client = HttpClients.createDefault()
HttpGet request = new HttpGet(uri)
CloseableHttpResponse response = client.execute(request)
response.getProtocolVersion()
response.getStatusLine().getStatusCode()
EntityUtils.toString(response.getEntity())
HttpClient client = HttpClient.newHttpClient()
client.version()
HttpRequest request = HttpRequest.newBuilder().uri(uri).GET().build()
HttpResponse<String> response = client.send(request, BodyHandlers.ofString())
response.statusCode()
response.body()
CompletableFuture<HttpResponse<String>> response = client.sendAsync(request, BodyHandlers.ofString())
response.get().body()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment