From 995fc00ec5e09f909d508c92a7302461cb13c290 Mon Sep 17 00:00:00 2001 From: Eran Medan Date: Mon, 16 Nov 2015 11:01:09 -0500 Subject: [PATCH 01/18] just a tiny grammar thing --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 360a48ba..badb54a8 100644 --- a/README.md +++ b/README.md @@ -107,7 +107,7 @@ As you can see the code is much shorter and easier to read. But it gets even sho Collections.sort(names, (String a, String b) -> b.compareTo(a)); ``` -For one line method bodies you can skip both the braces `{}` and the `return` keyword. But it gets even more shorter: +For one line method bodies you can skip both the braces `{}` and the `return` keyword. But it gets even shorter: ```java names.sort((a, b) -> b.compareTo(a)); From 97e248905f238c32bef3779f425274fbd1b969a0 Mon Sep 17 00:00:00 2001 From: grijesh Date: Fri, 20 Nov 2015 01:25:32 +0530 Subject: [PATCH 02/18] BiConsumer Example --- .../java8/samples/lambda/Lambda5.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/com/winterbe/java8/samples/lambda/Lambda5.java diff --git a/src/com/winterbe/java8/samples/lambda/Lambda5.java b/src/com/winterbe/java8/samples/lambda/Lambda5.java new file mode 100644 index 00000000..68a311f4 --- /dev/null +++ b/src/com/winterbe/java8/samples/lambda/Lambda5.java @@ -0,0 +1,32 @@ +package com.winterbe.java8.samples.lambda; + +import java.util.HashMap; +import java.util.function.BiConsumer; + +/** + * Created by grijesh + */ +public class Lambda5 { + + //Pre-Defined Functional Interfaces + public static void main(String... args) { + + //BiConsumer Example + BiConsumer printKeyAndValue + = (key,value) -> System.out.println(key+"-"+value); + + printKeyAndValue.accept("One",1); + printKeyAndValue.accept("Two",2); + + System.out.println("##################"); + + //Java Hash-Map foreach supports BiConsumer + HashMap dummyValues = new HashMap<>(); + dummyValues.put("One", 1); + dummyValues.put("Two", 2); + dummyValues.put("Three", 3); + + dummyValues.forEach((key,value) -> System.out.println(key+"-"+value)); + + } +} From d6c972191da3f22531e59fb8775e870f9cf15994 Mon Sep 17 00:00:00 2001 From: Benjamin Winterberg Date: Fri, 18 Dec 2015 11:18:52 +0100 Subject: [PATCH 03/18] Fix link #20 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index badb54a8..99ac9a48 100644 --- a/README.md +++ b/README.md @@ -729,7 +729,7 @@ System.out.println(string); // Nov 03, 2014 - 07:13 Unlike `java.text.NumberFormat` the new `DateTimeFormatter` is immutable and **thread-safe**. -For details on the pattern syntax read [here](http://download.java.net/jdk8/docs/api/java/time/format/DateTimeFormatter.html). +For details on the pattern syntax read [here](https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html). ## Annotations From 3d878f673337ee05bd3cd3b98d3389b47783997e Mon Sep 17 00:00:00 2001 From: Benjamin Winterberg Date: Tue, 5 Jan 2016 09:22:45 +0100 Subject: [PATCH 04/18] Happy new year 2016 --- LICENSE | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/LICENSE b/LICENSE index 3e09223f..58e794c6 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2014 Benjamin Winterberg +Copyright (c) 2016 Benjamin Winterberg Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. \ No newline at end of file +SOFTWARE. From ab7e2d2cc09fcb357701fe5d5ee4067ae2c542c6 Mon Sep 17 00:00:00 2001 From: Aiden Scandella Date: Wed, 13 Jan 2016 17:17:57 -0800 Subject: [PATCH 05/18] Fix whitespace in Default Interface Methods --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 99ac9a48..7ee572f3 100644 --- a/README.md +++ b/README.md @@ -267,7 +267,7 @@ Remember the formula example from the first section? Interface `Formula` defines Default methods **cannot** be accessed from within lambda expressions. The following code does not compile: ```java -Formula formula = (a) -> sqrt( a * 100); +Formula formula = (a) -> sqrt(a * 100); ``` From 6dd0e09f79cb53fdfa11851ec2afbf0c1664c20c Mon Sep 17 00:00:00 2001 From: Benjamin Winterberg Date: Tue, 22 Mar 2016 11:37:08 +0100 Subject: [PATCH 06/18] Add link to stream tutorial --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7ee572f3..ad01eb89 100644 --- a/README.md +++ b/README.md @@ -357,7 +357,7 @@ optional.ifPresent((s) -> System.out.println(s.charAt(0))); // "b" A `java.util.Stream` represents a sequence of elements on which one or more operations can be performed. Stream operations are either _intermediate_ or _terminal_. While terminal operations return a result of a certain type, intermediate operations return the stream itself so you can chain multiple method calls in a row. Streams are created on a source, e.g. a `java.util.Collection` like lists or sets (maps are not supported). Stream operations can either be executed sequentially or parallely. -> You should also check out [Stream.js](https://github.com/winterbe/streamjs), a JavaScript port of the Java 8 Streams API. +> Streams are extremely powerful, so I wrote a separate [Java 8 Streams Tutorial](http://winterbe.com/posts/2014/07/31/java8-stream-tutorial-examples/). You should also check out [Stream.js](https://github.com/winterbe/streamjs), a JavaScript port of the Java 8 Streams API. Let's first look how sequential streams work. First we create a sample source in form of a list of strings: From 642ce1c298acbd637aa3375084eb7a105881daeb Mon Sep 17 00:00:00 2001 From: Sven Boekhoff Date: Tue, 5 Jul 2016 16:32:56 +0200 Subject: [PATCH 07/18] Use class instead of bean. A class should only be called 'bean', if: * all properties are private (use getters/setters) * it implements the Serializable interface * it has a public default constructor See: http://stackoverflow.com/questions/3295496/what-is-a-javabean-exactly --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ad01eb89..656cbd77 100644 --- a/README.md +++ b/README.md @@ -167,7 +167,7 @@ String converted = converter.convert("Java"); System.out.println(converted); // "J" ``` -Let's see how the `::` keyword works for constructors. First we define an example bean with different constructors: +Let's see how the `::` keyword works for constructors. First we define an example class with different constructors: ```java class Person { From 23839f740ee1ec2025827452408de45f7bb6c754 Mon Sep 17 00:00:00 2001 From: Benjamin Winterberg Date: Thu, 6 Oct 2016 17:12:13 +0200 Subject: [PATCH 08/18] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 656cbd77..b673fccc 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,8 @@ Welcome to my introduction to [Java 8](https://jdk8.java.net/). This tutorial gu This article was originally posted on [my blog](http://winterbe.com/posts/2014/03/16/java-8-tutorial/). You should [follow me on Twitter](https://twitter.com/winterbe_). +_If you like this project, please give me a star._ ★ + ## Table of Contents * [Default Methods for Interfaces](#default-methods-for-interfaces) From 85dae289bdc6903b651f35fe1010d387d78ff90f Mon Sep 17 00:00:00 2001 From: Benjamin Winterberg Date: Thu, 6 Oct 2016 17:12:29 +0200 Subject: [PATCH 09/18] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b673fccc..0d5e4ac7 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,13 @@ # Modern Java - A Guide to Java 8 +_If you like this project, please give me a star._ ★ + > [“Java is still not dead—and people are starting to figure that out.”](https://twitter.com/mreinhold/status/429603588525281280) Welcome to my introduction to [Java 8](https://jdk8.java.net/). This tutorial guides you step by step through all new language features. Backed by short and simple code samples you'll learn how to use default interface methods, lambda expressions, method references and repeatable annotations. At the end of the article you'll be familiar with the most recent [API](http://download.java.net/jdk8/docs/api/) changes like streams, functional interfaces, map extensions and the new Date API. **No walls of text, just a bunch of commented code snippets. Enjoy!** This article was originally posted on [my blog](http://winterbe.com/posts/2014/03/16/java-8-tutorial/). You should [follow me on Twitter](https://twitter.com/winterbe_). -_If you like this project, please give me a star._ ★ - ## Table of Contents * [Default Methods for Interfaces](#default-methods-for-interfaces) From b2cbf4f9b7752861eebf295b2f30dd8ba43ab639 Mon Sep 17 00:00:00 2001 From: Steve Sun Date: Wed, 4 Jan 2017 20:02:05 -0800 Subject: [PATCH 10/18] update License to 2017 --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 58e794c6..d82fe930 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2016 Benjamin Winterberg +Copyright (c) 2017 Benjamin Winterberg Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal From 7a076614461b6ef48e765441616bad0ffb446daa Mon Sep 17 00:00:00 2001 From: Benjamin Winterberg Date: Thu, 12 Oct 2017 18:18:27 +0200 Subject: [PATCH 11/18] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0d5e4ac7..d2ce7413 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ _If you like this project, please give me a star._ ★ Welcome to my introduction to [Java 8](https://jdk8.java.net/). This tutorial guides you step by step through all new language features. Backed by short and simple code samples you'll learn how to use default interface methods, lambda expressions, method references and repeatable annotations. At the end of the article you'll be familiar with the most recent [API](http://download.java.net/jdk8/docs/api/) changes like streams, functional interfaces, map extensions and the new Date API. **No walls of text, just a bunch of commented code snippets. Enjoy!** -This article was originally posted on [my blog](http://winterbe.com/posts/2014/03/16/java-8-tutorial/). You should [follow me on Twitter](https://twitter.com/winterbe_). +This article was originally posted on [my blog](http://winterbe.com/posts/2014/03/16/java-8-tutorial/). You should **[follow me on Twitter](https://twitter.com/winterbe_)** and check out my new project **[Sequency](https://github.com/winterbe/sequency)**. ## Table of Contents @@ -359,7 +359,7 @@ optional.ifPresent((s) -> System.out.println(s.charAt(0))); // "b" A `java.util.Stream` represents a sequence of elements on which one or more operations can be performed. Stream operations are either _intermediate_ or _terminal_. While terminal operations return a result of a certain type, intermediate operations return the stream itself so you can chain multiple method calls in a row. Streams are created on a source, e.g. a `java.util.Collection` like lists or sets (maps are not supported). Stream operations can either be executed sequentially or parallely. -> Streams are extremely powerful, so I wrote a separate [Java 8 Streams Tutorial](http://winterbe.com/posts/2014/07/31/java8-stream-tutorial-examples/). You should also check out [Stream.js](https://github.com/winterbe/streamjs), a JavaScript port of the Java 8 Streams API. +> Streams are extremely powerful, so I wrote a separate [Java 8 Streams Tutorial](http://winterbe.com/posts/2014/07/31/java8-stream-tutorial-examples/). **You should also check out [Sequency](https://github.com/winterbe/sequency) as a similiar library for the web.** Let's first look how sequential streams work. First we create a sample source in form of a list of strings: From 6ec91e6992e56b765b2ae2bcbf02733817acb844 Mon Sep 17 00:00:00 2001 From: Benjamin Winterberg Date: Thu, 12 Oct 2017 18:30:02 +0200 Subject: [PATCH 12/18] Update README.md --- README.md | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d2ce7413..08976bf0 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,17 @@ # Modern Java - A Guide to Java 8 - -_If you like this project, please give me a star._ ★ +_This article was originally posted on [my blog](http://winterbe.com/posts/2014/03/16/java-8-tutorial/)._ > [“Java is still not dead—and people are starting to figure that out.”](https://twitter.com/mreinhold/status/429603588525281280) Welcome to my introduction to [Java 8](https://jdk8.java.net/). This tutorial guides you step by step through all new language features. Backed by short and simple code samples you'll learn how to use default interface methods, lambda expressions, method references and repeatable annotations. At the end of the article you'll be familiar with the most recent [API](http://download.java.net/jdk8/docs/api/) changes like streams, functional interfaces, map extensions and the new Date API. **No walls of text, just a bunch of commented code snippets. Enjoy!** -This article was originally posted on [my blog](http://winterbe.com/posts/2014/03/16/java-8-tutorial/). You should **[follow me on Twitter](https://twitter.com/winterbe_)** and check out my new project **[Sequency](https://github.com/winterbe/sequency)**. +--- + +

+ ★★★ Like this project? Leave a star, follow on Twitter and check out my new project Sequency. Thanks! ★★★ +

+ +--- ## Table of Contents From 093d9a4af387eb4b713bfcb14c1baa686c27ac05 Mon Sep 17 00:00:00 2001 From: Benjamin Winterberg Date: Tue, 9 Jan 2018 10:04:59 +0100 Subject: [PATCH 13/18] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 08976bf0..10b63eeb 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Welcome to my introduction to [Java 8](https://jdk8.java.net/). This tutorial gu ---

- ★★★ Like this project? Leave a star, follow on Twitter and check out my new project Sequency. Thanks! ★★★ + ★★★ Like this project? Leave a star, follow on Twitter or donate to support my work. Thanks! ★★★

--- From 81a0fa3aa1d6ec2409e0226d3a6c2f5c2d19a41d Mon Sep 17 00:00:00 2001 From: Benjamin Winterberg Date: Tue, 4 Sep 2018 17:45:02 +0200 Subject: [PATCH 14/18] Java 11 local variable samples --- .../winterbe/java11/LocalVariableSyntax.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/com/winterbe/java11/LocalVariableSyntax.java diff --git a/src/com/winterbe/java11/LocalVariableSyntax.java b/src/com/winterbe/java11/LocalVariableSyntax.java new file mode 100644 index 00000000..e2a60906 --- /dev/null +++ b/src/com/winterbe/java11/LocalVariableSyntax.java @@ -0,0 +1,37 @@ +package com.winterbe.java11; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.function.Predicate; + +public class LocalVariableSyntax { + + public static void main(String[] args) { + var text = "Banana"; +// Incompatible types: +// text = 1; + + +// Cannot infer type: +// var a; +// var nothing = null; +// var bla = () -> System.out.println("Hallo"); +// var method = LocalVariableSyntax::someMethod; + + var list1 = new ArrayList<>(); // ArrayList + + var list2 = new ArrayList>>(); + + for (var current : list2) { + // current is of type: Map> + System.out.println(current); + } + + Predicate predicate1 = (@Deprecated var a) -> false; + + } + + void someMethod() {} + +} From 4b92b43d53298652446651f94254341614c7873f Mon Sep 17 00:00:00 2001 From: Benjamin Winterberg Date: Wed, 5 Sep 2018 10:35:12 +0200 Subject: [PATCH 15/18] Java 11 HttpClient examples --- .../winterbe/java11/HttpClientExamples.java | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/com/winterbe/java11/HttpClientExamples.java diff --git a/src/com/winterbe/java11/HttpClientExamples.java b/src/com/winterbe/java11/HttpClientExamples.java new file mode 100644 index 00000000..23a9e53f --- /dev/null +++ b/src/com/winterbe/java11/HttpClientExamples.java @@ -0,0 +1,77 @@ +package com.winterbe.java11; + +import java.io.IOException; +import java.net.Authenticator; +import java.net.PasswordAuthentication; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.time.Duration; + +public class HttpClientExamples { + + public static void main(String[] args) throws IOException, InterruptedException { +// syncRequest(); +// asyncRequest(); +// postData(); + basicAuth(); + } + + private static void syncRequest() throws IOException, InterruptedException { + var client = HttpClient.newHttpClient(); + var request = HttpRequest.newBuilder() + .uri(URI.create("https://winterbe.com")) + .build(); + HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + System.out.println(response.body()); + } + + private static void asyncRequest() { + var client = HttpClient.newHttpClient(); + var request = HttpRequest.newBuilder() + .uri(URI.create("https://winterbe.com")) + .build(); + client.sendAsync(request, HttpResponse.BodyHandlers.ofString()) + .thenApply(HttpResponse::body) + .thenAccept(System.out::println); + } + + private static void postData() throws IOException, InterruptedException { + var client = HttpClient.newHttpClient(); + var request = HttpRequest.newBuilder() + .uri(URI.create("https://postman-echo.com/post")) + .timeout(Duration.ofSeconds(30)) + .version(HttpClient.Version.HTTP_2) + .header("Content-Type", "text/plain") + .POST(HttpRequest.BodyPublishers.ofString("Hi there!")) + .build(); + var response = client.send(request, HttpResponse.BodyHandlers.ofString()); + System.out.println(response.statusCode()); // 200 + } + + private static void basicAuth() throws IOException, InterruptedException { + var client = HttpClient.newHttpClient(); + var request1 = HttpRequest.newBuilder() + .uri(URI.create("https://postman-echo.com/basic-auth")) + .build(); + var response1 = client.send(request1, HttpResponse.BodyHandlers.ofString()); + System.out.println(response1.statusCode()); // 401 + + var authClient = HttpClient + .newBuilder() + .authenticator(new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication("postman", "password".toCharArray()); + } + }) + .build(); + var request2 = HttpRequest.newBuilder() + .uri(URI.create("https://postman-echo.com/basic-auth")) + .build(); + var response2 = authClient.send(request2, HttpResponse.BodyHandlers.ofString()); + System.out.println(response2.statusCode()); // 200 + } + +} From f2d7b5b2ccf0ebd132cbcf162156706788e28cc3 Mon Sep 17 00:00:00 2001 From: Benjamin Winterberg Date: Wed, 5 Sep 2018 16:42:20 +0200 Subject: [PATCH 16/18] More Java 11 examples --- .../winterbe/java11/HttpClientExamples.java | 7 +- src/com/winterbe/java11/Misc.java | 69 +++++++++++++++++++ src/com/winterbe/java11/dummy.txt | 1 + 3 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 src/com/winterbe/java11/Misc.java create mode 100644 src/com/winterbe/java11/dummy.txt diff --git a/src/com/winterbe/java11/HttpClientExamples.java b/src/com/winterbe/java11/HttpClientExamples.java index 23a9e53f..7342b1de 100644 --- a/src/com/winterbe/java11/HttpClientExamples.java +++ b/src/com/winterbe/java11/HttpClientExamples.java @@ -19,26 +19,25 @@ public static void main(String[] args) throws IOException, InterruptedException } private static void syncRequest() throws IOException, InterruptedException { - var client = HttpClient.newHttpClient(); var request = HttpRequest.newBuilder() .uri(URI.create("https://winterbe.com")) .build(); + var client = HttpClient.newHttpClient(); HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); System.out.println(response.body()); } private static void asyncRequest() { - var client = HttpClient.newHttpClient(); var request = HttpRequest.newBuilder() .uri(URI.create("https://winterbe.com")) .build(); + var client = HttpClient.newHttpClient(); client.sendAsync(request, HttpResponse.BodyHandlers.ofString()) .thenApply(HttpResponse::body) .thenAccept(System.out::println); } private static void postData() throws IOException, InterruptedException { - var client = HttpClient.newHttpClient(); var request = HttpRequest.newBuilder() .uri(URI.create("https://postman-echo.com/post")) .timeout(Duration.ofSeconds(30)) @@ -46,12 +45,14 @@ private static void postData() throws IOException, InterruptedException { .header("Content-Type", "text/plain") .POST(HttpRequest.BodyPublishers.ofString("Hi there!")) .build(); + var client = HttpClient.newHttpClient(); var response = client.send(request, HttpResponse.BodyHandlers.ofString()); System.out.println(response.statusCode()); // 200 } private static void basicAuth() throws IOException, InterruptedException { var client = HttpClient.newHttpClient(); + var request1 = HttpRequest.newBuilder() .uri(URI.create("https://postman-echo.com/basic-auth")) .build(); diff --git a/src/com/winterbe/java11/Misc.java b/src/com/winterbe/java11/Misc.java new file mode 100644 index 00000000..e343dec7 --- /dev/null +++ b/src/com/winterbe/java11/Misc.java @@ -0,0 +1,69 @@ +package com.winterbe.java11; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class Misc { + + @Deprecated(forRemoval = true) + String foo; + + public static void main(String[] args) throws IOException { + collections(); + strings(); + optionals(); + inputStreams(); + streams(); + } + + private static void streams() { + System.out.println(Stream.ofNullable(null).count()); // 0 + System.out.println(Stream.of(1, 2, 3, 2, 1) + .dropWhile(n -> n < 3) + .collect(Collectors.toList())); // [3, 2, 1] + System.out.println(Stream.of(1, 2, 3, 2, 1) + .takeWhile(n -> n < 3) + .collect(Collectors.toList())); // [1, 2] + } + + private static void inputStreams() throws IOException { + var classLoader = ClassLoader.getSystemClassLoader(); + var inputStream = classLoader.getResourceAsStream("com/winterbe/java11/dummy.txt"); + var tempFile = File.createTempFile("dummy-copy", "txt"); + try (var outputStream = new FileOutputStream(tempFile)) { + inputStream.transferTo(outputStream); + } + System.out.println(tempFile.length()); + } + + private static void optionals() { + System.out.println(Optional.of("foo").orElseThrow()); // foo + System.out.println(Optional.ofNullable(null).or(() -> Optional.of("bar")).get()); // bar + System.out.println(Optional.of("foo").stream().count()); // 1 + } + + private static void strings() { + System.out.println(" ".isBlank()); + System.out.println(" Foo Bar ".strip()); // "Foo Bar" + System.out.println(" Foo Bar ".stripTrailing()); // " Foo Bar" + System.out.println(" Foo Bar ".stripLeading()); // "Foo Bar " + System.out.println("Java".repeat(3)); // "JavaJavaJava" + System.out.println("A\nB\nC".lines().count()); // 3 + } + + private static void collections() { + var list = List.of("A", "B", "C"); + var copy = List.copyOf(list); + System.out.println(list == copy); // true + + var map = Map.of("A", 1, "B", 2); + System.out.println(map); + } + +} diff --git a/src/com/winterbe/java11/dummy.txt b/src/com/winterbe/java11/dummy.txt new file mode 100644 index 00000000..5eced957 --- /dev/null +++ b/src/com/winterbe/java11/dummy.txt @@ -0,0 +1 @@ +Foobar \ No newline at end of file From faf9793524cd6956428fa5d79e7fdce3a3184b56 Mon Sep 17 00:00:00 2001 From: Benjamin Winterberg Date: Tue, 25 Sep 2018 09:38:27 +0200 Subject: [PATCH 17/18] Add link to Java 11 Tutorial --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 10b63eeb..e9caeed4 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Modern Java - A Guide to Java 8 _This article was originally posted on [my blog](http://winterbe.com/posts/2014/03/16/java-8-tutorial/)._ -> [“Java is still not dead—and people are starting to figure that out.”](https://twitter.com/mreinhold/status/429603588525281280) +> **You should also read my [Java 11 Tutorial](https://winterbe.com/posts/2018/09/24/java-11-tutorial/) (including new language and API features from Java 9, 10 and 11).** Welcome to my introduction to [Java 8](https://jdk8.java.net/). This tutorial guides you step by step through all new language features. Backed by short and simple code samples you'll learn how to use default interface methods, lambda expressions, method references and repeatable annotations. At the end of the article you'll be familiar with the most recent [API](http://download.java.net/jdk8/docs/api/) changes like streams, functional interfaces, map extensions and the new Date API. **No walls of text, just a bunch of commented code snippets. Enjoy!** From 9b79999be76274f0c11a5b1d4a4fff4e785dc774 Mon Sep 17 00:00:00 2001 From: winterbe Date: Thu, 27 Apr 2023 09:52:04 +0200 Subject: [PATCH 18/18] Update LICENSE --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index d82fe930..cc53a543 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2017 Benjamin Winterberg +Copyright (c) 2023 Benjamin Winterberg Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal