Skip to content

Instantly share code, notes, and snippets.

@mcupak
Created June 1, 2018 11:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcupak/9d816e4a90eb2aa91f642eb9180d50e3 to your computer and use it in GitHub Desktop.
Save mcupak/9d816e4a90eb2aa91f642eb9180d50e3 to your computer and use it in GitHub Desktop.
JShell transcript from the talk Writing clean code with Java 9+ at VoxxedDays Athens 2018.
1+1
int x = 1+1
System.out.println(x)
Thread.sleep(2000)
/vars
/methods
/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"))
List<Integer> listCopy = List.copyOf(list)
Set<Integer> set = Set.copyOf(list)
/l readFile
/ed readFile
readfile("/tmp/voxxed")
readFile("/tmp/voxxed")
new FileInputStream("/tmp/voxxed").transferTo(System.out)
/l MyInterface
/l MyClass
new MyClass().abstractMethod()
new MyClass().defaultMethod()
/ed MyInterface
new MyClass().abstractMethod()
new MyClass().defaultMethod()
new MyClass().privateMethod()
IntStream.iterate(0, i -> i+2).filter(j -> j<100).forEach(System.out::println)
IntStream.iterate(0, i -> i+2).limit(50).forEach(System.out::println)
IntStream.iterate(0, i -> i+2).takeWhile(j -> j<100).forEach(System.out::println)
IntStream.iterate(0, i -> i<100, i -> i+2).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()
Stream.ofNullable(null).collect(toList())
Stream.ofNullable(null).collect(toUnmodifiableList())
Pattern p = Pattern.compile("\\d")
Matcher m = p.matcher("1231adfas12321")
m.results().map(r -> r.group()).forEach(System.out::println)
Optional.empty()
Optional.of("something")
Optional.of("something").isPresent()
Optional.of("something").ifPresent(System.out::println)
Optional.of("something").ifPresentOrElse(System.out::println, () -> System.out.println("empty"))
Optional.empty().ifPresentOrElse(System.out::println, () -> System.out.println("empty"))
Optional.empty().orElse("empty")
Optional.empty().orElseGet(() -> "empty")
Optional.empty().or(() -> Optional.of("empty"))
Optional.empty().orElseThrow()
Optional.empty().stream().forEach(System.out::println)
/op handler.jsh
/l
HttpClient client = HttpClient.newHttpClient()
client.version()
HttpRequest request = HttpRequest.newBuilder().uri(new URI("http://localhost:8000/hello")).GET().build()
HttpResponse<String> response = client.send(request, BodyHandler.asString())
response.statusCode()
response.body()
/l helloSupplier
/ed helloSupplier
helloSupplier.get()
var list = new ArrayList<String>()
/vars
var x=0, y=0
int inc(int x) { return x+1; }
int inc(var x) { return x+1; }
var inc(int x) { return x+1; }
var x
var x = null
var x = (Integer) null
int[] x = {1,2}
var[] x = {1,2}
var x = {1,2}
var x = new int[]{1,2}
Supplier s =() -> ""
var s =() -> ""
var list = new ArrayList<>()
/v
var list = new ArrayList<>(Set.of(1))
/v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment