String
- strip
- stripLeading
- stripTrailing
jshell> var s=" text "
s ==> " text "
| modified variable s : String
| update overwrote variable s : String
jshell> s.strip()
$6 ==> "text"
jshell> s.stripLeading()
$7 ==> "text "
- isEmpty
jshell> s.isEmpty()
$9 ==> false
jshell> "".isEmpty()
$10 ==> true
- lines
jshell> "aaa\nbbbb\n\ncccc".lines().collect(Collectors.toList())
$12 ==> [aaa, bbbb, , cccc]
- repeat
jshell> "<".repeat(3) + "text" + ">".repeat(3)
$13 ==> "<<<text>>>"
Files
- Read/Write to file
jshell> var myFile=Path.of("tests.txt")
myFile ==> tests.txt
jshell> Files.writeString(myFile,"aaa\nbbb\nccc")
$16 ==> tests.txt
jshell> Files.readString(myFile)
$17 ==> "aaa\nbbb\nccc"
jshell> Files.isSameFile(myFile,myFile)
$18 ==> true
Regex and Streams
jshell> var p =Pattern.compile("^error.*$").asMatchPredicate()
jshell> List.of("error: errorA","error:errorB","debug:DebugA","error:ErrorC").stream().filter(p).toArray()
$35 ==> Object[3] { "error: errorA", "error:errorB", "error:ErrorC" }
var in lambdas
public interface Test{
...> public void testMethod(String s);
...> }
jshell> Test t= s -> System.out.println(s)
t ==> $Lambda$31/0x00000008000bd840@4b553d26
jshell> t.testMethod("aaa")
aaa
jshell> Test t= (var s) -> System.out.println(s)
t ==> $Lambda$31/0x00000008000bd840@4b553d26
jshell> t.testMethod("bbb")
bbb
Predicate.not
lines.stream().filter(not(String::isBlank))
MatchPredicate
jshell> var aaPredicate=Pattern.compile("aaa").asMatchPredicate()
aaPredicate ==> java.util.regex.Pattern$$Lambda$220/0x00000008001f4840@639c2c1d
jshell> aaPredicate.test("abc")
$46 ==> false
jshell> aaPredicate.test("aaa")
$47 ==> true
Nested Classes
http://openjdk.java.net/jeps/181
Why it is important :
In
- In generic specialization each specialized type could be created as a nestmate of the generic type.
- A safe and supported replacement for the
Unsafe.defineAnonymousClass()
API could create the new class as a nestmate of an existing class. - The concept of "sealed classes" could be effected by only allowing subclasses that are nestmates.
- Truly private nested types could be effected (presently private nested types are defined with package-access).
JEPS
- http://openjdk.java.net/jeps/330 - Single File Execution
public class HelloSingleFile {
public static void main(String[] args) {
System.out.println("Hello From Single File");
}
}
>java HelloSingleFile.java
Hello From Single File
#!/usr/bin/java --source 11
public class HelloWorld {
- http://openjdk.java.net/jeps/330 - java.net.http
var request = HttpRequest.newBuilder()
.uri(URI.create("https://winterbe.com"))
.GET()
.build();
var client = HttpClient.newHttpClient();
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
jshell> var uri= URI.create("http://www.pogodynka.pl")
uri ==> http://www.pogodynka.pl
jshell> var request=HttpRequest.newBuilder().uri(uri).GET().build()
request ==> http://www.pogodynka.pl GET
jshell> var client = HttpClient.newHttpClient()
var response = client.send(request, HttpResponse.BodyHandlers.ofString())
jshell> response.body()
Async
var response = client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
response.thenApply(HttpResponse::body).thenAccept(System.out::println)
var stringPublisher=HttpRequest.BodyPublishers.ofString("Hi There!")
stringPublisher ==> jdk.internal.net.http.RequestPublishers$StringPublisher@3023df74
jshell> var req=HttpRequest.newBuilder().uri(postmanURI).
...> header("Content-type","text/plain").
...> POST(stringPublisher).
...> build()
req ==> https://postman-echo.com/post POST
jshell> HttpResponse.BodyHandler
BodyHandler BodyHandlers
jshell> var stringHandler=HttpResponse.BodyHandlers.ofString()
stringHandler ==> java.net.http.HttpResponse$BodyHandlers$$Lambda$78/0x0000000800168840@2a65fe7c
jshell> var r=client.send(req,stringHandler)
r ==> (POST https://postman-echo.com/post) 200
jshell> r.statusCode()
$44 ==> 200
EMOJI
jshell> var sb=new StringBuilder()
sb ==>
jshell> Stream.iterate(127767,i->i+1).map(Character::toChars).limit(100).forEach(sb::append)
jshell> sb
sb ==> 🌗🌘🌙🌚🌛🌜🌝🌞🌟🌠🌡🌢🌣🌤🌥🌦🌧🌨🌩🌪🌫🌬🌭🌮🌯🌰🌱🌲🌳🌴🌵🌶🌷🌸🌹🌺🌻🌼🌽🌾🌿🍀🍁🍂🍃🍄🍅🍆🍇🍈🍉🍊🍋🍌🍍🍎🍏🍐🍑🍒🍓🍔🍕🍖🍗🍘🍙🍚🍛🍜🍝🍞🍟🍠🍡🍢🍣🍤🍥🍦🍧🍨🍩🍪🍫🍬🍭🍮🍯🍰🍱🍲🍳🍴🍵🍶🍷🍸🍹🍺
API
- https://www.azul.com/90-new-features-and-apis-in-jdk-11/
- check nested classes