You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The formula is implemented as an anonymous object. The code is quite verbose: 6 lines of code for such a simple calucation of `sqrt(a * 100)`. As we'll see in the next section, there's a much nicer way of implementing single method objects in Java 8.
76
+
The formula is implemented as an anonymous object. The code is quite verbose: 6 lines of code for such a simple calculation of `sqrt(a * 100)`. As we'll see in the next section, there's a much nicer way of implementing single method objects in Java 8.
77
77
78
78
79
79
## Lambda expressions
@@ -107,7 +107,7 @@ As you can see the code is much shorter and easier to read. But it gets even sho
107
107
Collections.sort(names, (String a, String b) -> b.compareTo(a));
108
108
```
109
109
110
-
For one line method bodies you can skip both the braces `{}` and the `return` keyword. But it gets even more shorter:
110
+
For one line method bodies you can skip both the braces `{}` and the `return` keyword. But it gets even shorter:
111
111
112
112
```java
113
113
names.sort((a, b) -> b.compareTo(a));
@@ -118,7 +118,7 @@ List now has a `sort` method. Also the java compiler is aware of the parameter t
118
118
119
119
## Functional Interfaces
120
120
121
-
How does lambda expressions fit into Javas type system? Each lambda corresponds to a given type, specified by an interface. A so called _functional interface_ must contain **exactly one abstract method** declaration. Each lambda expression of that type will be matched to this abstract method. Since default methods are not abstract you're free to add default methods to your functional interface.
121
+
How does lambda expressions fit into Java's type system? Each lambda corresponds to a given type, specified by an interface. A so called _functional interface_ must contain **exactly one abstract method** declaration. Each lambda expression of that type will be matched to this abstract method. Since default methods are not abstract you're free to add default methods to your functional interface.
122
122
123
123
We can use arbitrary interfaces as lambda expressions as long as the interface only contains one abstract method. To ensure that your interface meet the requirements, you should add the `@FunctionalInterface` annotation. The compiler is aware of this annotation and throws a compiler error as soon as you try to add a second abstract method declaration to the interface.
Keep in mind that the code is also valid if the `@FunctionalInterface` annotation would be ommited.
140
+
Keep in mind that the code is also valid if the `@FunctionalInterface` annotation would be omitted.
141
141
142
142
143
143
## Method and Constructor References
@@ -239,7 +239,7 @@ Writing to `num` from within the lambda expression is also prohibited.
239
239
240
240
### Accessing fields and static variables
241
241
242
-
In constrast to local variables we have both read and write access to instance fields and static variables from within lambda expressions. This behaviour is well known from anonymous objects.
242
+
In contrast to local variables, we have both read and write access to instance fields and static variables from within lambda expressions. This behaviour is well known from anonymous objects.
243
243
244
244
```java
245
245
classLambda4 {
@@ -267,7 +267,7 @@ Remember the formula example from the first section? Interface `Formula` defines
267
267
Default methods **cannot** be accessed from within lambda expressions. The following code does not compile:
268
268
269
269
```java
270
-
Formula formula = (a) -> sqrt(a *100);
270
+
Formula formula = (a) -> sqrt(a *100);
271
271
```
272
272
273
273
@@ -316,7 +316,7 @@ personSupplier.get(); // new Person
316
316
317
317
### Consumers
318
318
319
-
Consumers represents operations to be performed on a single input argument.
319
+
Consumers represent operations to be performed on a single input argument.
Optionals are not functional interfaces, instead it's a nifty utility to prevent `NullPointerException`. It's an important concept for the next section, so let's have a quick look at how Optionals work.
342
+
Optionals are not functional interfaces, but nifty utilities to prevent `NullPointerException`. It's an important concept for the next section, so let's have a quick look at how Optionals work.
343
343
344
344
Optional is a simple container for a value which may be null or non-null. Think of a method which may return a non-null result but sometimes return nothing. Instead of returning `null` you return an `Optional` in Java 8.
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 sequential or parallel.
358
+
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.
359
359
360
-
> You should also check out [Stream.js](https://github.com/winterbe/streamjs), a JavaScript port of the Java 8 Streams API.
360
+
> 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.
361
361
362
362
Let's first look how sequential streams work. First we create a sample source in form of a list of strings:
As mentioned above streams can be either sequential or parallel. Operations on sequential streams are performed on a single thread while operations on parallel streams are performed concurrent on multiple threads.
485
+
As mentioned above streams can be either sequential or parallel. Operations on sequential streams are performed on a single thread while operations on parallel streams are performed concurrently on multiple threads.
486
486
487
487
The following example demonstrates how easy it is to increase the performance by using parallel streams.
488
488
@@ -535,7 +535,9 @@ As you can see both code snippets are almost identical but the parallel sort is
535
535
536
536
## Maps
537
537
538
-
As already mentioned maps don't support streams. Instead maps now support various new and useful methods for doing common tasks.
538
+
As already mentioned maps do not directly support streams. There's no `stream()` method available on the `Map` interface itself, however you can create specialized streams upon the keys, values or entries of a map via `map.keySet().stream()`, `map.values().stream()` and `map.entrySet().stream()`.
539
+
540
+
Furthermore maps support various new and useful methods for doing common tasks.
539
541
540
542
```java
541
543
Map<Integer, String> map =newHashMap<>();
@@ -600,7 +602,7 @@ Java 8 contains a brand new date and time API under the package `java.time`. The
600
602
601
603
### Clock
602
604
603
-
Clock provides access to the current date and time. Clocks are aware of a timezone and may be used instead of `System.currentTimeMillis()` to retrieve the current milliseconds. Such an instantaneous point on the time-line is also represented by the class `Instant`. Instants can be used to create legacy `java.util.Date` objects.
605
+
Clock provides access to the current date and time. Clocks are aware of a timezone and may be used instead of `System.currentTimeMillis()` to retrieve the current time in milliseconds since Unix EPOCH. Such an instantaneous point on the time-line is also represented by the class `Instant`. Instants can be used to create legacy `java.util.Date` objects.
LocalDate represents a distinct date, e.g. 2014-03-11. It's immutable and works exactly analog to LocalTime. The sample demonstrates how to calculate new dates by adding or substracting days, months or years. Keep in mind that each manipulation returns a new instance.
666
+
LocalDate represents a distinct date, e.g. 2014-03-11. It's immutable and works exactly analog to LocalTime. The sample demonstrates how to calculate new dates by adding or subtracting days, months or years. Keep in mind that each manipulation returns a new instance.
Unlike `java.text.NumberFormat` the new `DateTimeFormatter` is immutable and **thread-safe**.
729
731
730
-
For details on the pattern syntax read [here](http://download.java.net/jdk8/docs/api/java/time/format/DateTimeFormatter.html).
732
+
For details on the pattern syntax read [here](https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html).
731
733
732
734
733
735
## Annotations
@@ -763,7 +765,7 @@ class Person {}
763
765
classPerson {}
764
766
```
765
767
766
-
Using variant 2 the java compiler implicitly sets up the `@Hints` annotation under the hood. That's important for reading annotation informations via reflection.
768
+
Using variant 2 the java compiler implicitly sets up the `@Hints` annotation under the hood. That's important for reading annotation information via reflection.
767
769
768
770
```java
769
771
Hint hint =Person.class.getAnnotation(Hint.class);
0 commit comments