📜 ⬆️ ⬇️

Lambda expressions in java 8

In the new version of Java 8, the long-awaited lambda expressions finally appeared. Perhaps this is the most important new feature of the latest version; they allow you to write faster and make the code more clear, and also open the door to the world of functional programming. In this article I will tell how it works.

Java was conceived as an object-oriented language in the 90s, when object-oriented programming was the main paradigm in application development. Long before that, there was object-oriented programming, there were functional programming languages, such as Lisp and Scheme, but their advantages were not appreciated outside the academic environment. Recently, functional programming has grown greatly in importance, because it is well suited for parallel programming and event-based programming (“reactive”). This does not mean that object orientation is bad. On the contrary, instead, the winning strategy is to mix object-oriented programming and functional. This makes sense even if you do not need parallelism. For example, collection libraries can get a powerful API if the language has a convenient syntax for functional expressions.

The main improvement in Java 8 is the addition of support for functional software constructs to its object-oriented framework. In this article, I will demonstrate the basic syntax and how to use it in several important contexts. Key points of the lambda concept:
')

Why do we need lambda?


A lambda expression is a block of code that can be transferred to another place, so it can be executed later, one or more times. Before delving into the syntax (and a curious name), let's take a step back and see where you used similar blocks of code in Java before.

If you want to perform actions in a separate thread, you put them in the run method of Runnable , like this:
 class MyRunner implements Runnable { public void run() { for (int i = 0; i < 1000; i++) doWork(); } ... } 

Then, when you want to execute this code, you create an instance of the MyRunner class. You can put an instance in the thread pool, or simply enter and start a new thread:
 MyRunner r = new MyRunner(); new Thread(r).start(); 

The key point is that the run method contains the code that needs to be executed in a separate thread.

Consider sorting using a custom comparator. If you want to sort the strings by length, not by default, you can pass the Comparator object to the sort method:
 class LengthStringComparator implements Comparator<String> { public int compare(String firstStr, String secondStr) { return Integer.compare(firstStr.length(),secondStr.length()); } } Arrays.sort(strings, new LengthStringComparator ()); 

The sort method still calls the compare method, rearranging the elements if they are not in order until the array is sorted. You provide a sort of code to the sort method for comparing elements, and this code is embedded in the rest of the sorting logic, which you probably do not need to override. Note that the call to Integer.compare (, ) returns zero if x and y are equal, a negative number if x <y, and a positive number if x> y. This static method was added to Java 7. You do not need to calculate x - y to compare x and y, because the calculation may cause overflow for large operands of the opposite sign.

As another example of deferred execution, consider a button callback. You place the callback action in a class method that implements the listener interface, create an instance, and register the instance. This is such a common script that many programmers use the syntax “anonymous instance of an anonymous class”:
 button.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent event) { System.out.println("The button has been clicked!"); } }); 

The code inside the handle method is important here. This code is executed whenever the button is pressed.

Since Java 8 positions JavaFX as a successor to the Swing GUI toolkit, I use JavaFX in these examples. Details do not matter. In each UI library, be it Swing, JavaFX, or Android, you give the button some code you want to run when the button is pressed.

In all three examples you saw the same approach. A block of code was passed to someone — the thread pool, the sort method, or the button. This code was called some time later.

Until now, passing code was not easy in Java. You could not just pass blocks of code anywhere. Java is an object-oriented language, so you had to create an object belonging to a class that has a method with the necessary code.
In other languages, you can work with code blocks directly. Java designers have resisted adding this feature for a long time. After all, the great power of Java is in its simplicity and consistency. A language can become extremely erratic if it includes all the functions that give a slightly shorter code. However, in those other languages, it is not just easier to spawn a stream or register a click handler; many of their APIs are simpler, more consistent and powerful. In Java, it would be possible to write similar interfaces that accept objects of classes that implement a specific function, but it would be inconvenient to use such APIs.

Recently, the question was not whether to extend Java for functional programming or not, but how to do it. It took several years of experimentation before it turned out that this is well suited for Java. In the next section, you will see how to work with code blocks in Java 8.

Lambda expression syntax


Consider the previous sorting example again. We pass code that checks which string is shorter. We calculate
 Integer.compare(firstStr.length(), secondStr.length()) 

What is firstStr and secondStr ? They are both lines! Java is a strongly typed language, and we must specify the types:
 (String firstStr, String secondStr) -> Integer.compare(firstStr.length(),secondStr.length()) 


You just saw your first lambda expression! Such an expression is simply a block of code along with the specification of any variables that must be passed to the code.

Why such a name? Many years ago, when there were no computers yet, the logician Alonzo Church wanted to formalize what it means for a mathematical function to be effectively calculated. (It is curious that there are functions that are known to exist, but no one knows how to calculate their values.) He used the Greek letter lambda (λ) to mark parameters. If he knew about the Java API, he would have written something not very similar to what you saw, most likely.

Why is the letter λ? Did Church use all the letters of the alphabet? In fact, the venerable labor Principia Mathematica uses the symbol ˆ to denote free variables that inspired Church to use the capital lambda (Λ) for parameters. But in the end, he switched to a lowercase version of the letter. Since then, an expression with variable parameters has been called a lambda expression.

You have just seen one form of lambda expressions in Java: parameters, the arrow -> and expression. If the code performs a calculation that does not fit into one expression, write it in the same way you would write a method: enclosed in {} and with explicit return expressions. For example,
 (String firstStr, String secondStr) -> { if (firstStr.length() < secondStr.length()) return -1; else if (firstStr.length() > secondStr.length()) return 1; else return 0; } 

If the lambda expression has no parameters, you still put empty brackets, just like with the method without parameters:
 () -> { for (int i = 0; i < 1000; i++) doWork(); } 

If the parameter types of lambda expressions can be inferred, you can omit them. For example,
 Comparator<String> comp = (firstStr, secondStr) // Same as (String firstStr, String secondStr) -> Integer.compare(firstStr.length(),secondStr.length()); 

Here the compiler can conclude that firstStr and secondStr must be strings, because the lambda expression is assigned to the string comparator. (We will look at this assignment more closely later.)

If the method has one parameter of the type being displayed, you can even omit the parentheses:
 EventHandler<ActionEvent> listener = event -> System.out.println("The button has been clicked!"); // Instead of (event) -> or (ActionEvent event) -> 


You can add annotations or a final modifier to lambda parameters in the same way as for method parameters:
 (final String var) -> ... (@NonNull String var) -> ... 

You never specify the result type of a lambda expression. It always turns out to be out of context. For example, the expression
 (String firstStr, String secondStr) -> Integer.compare(firstStr.length(), secondStr.length()) 

can be used in a context where an int result is expected.

Note that the lambda expression cannot return a value in some branches, and not return in others. For example, (int x) -> { if (x <= 1) return -1; } (int x) -> { if (x <= 1) return -1; } is not valid.

Functional Interfaces


As we have discussed, there are many existing interfaces in Java that encapsulate blocks of code, such as Runnable or Comparator . Lambda expressions are backward compatible with these interfaces.

You can put a lambda expression whenever an interface object with a single abstract method is expected. Such an interface is called a functional interface.

You may wonder why a functional interface should have a single abstract method. Are not all the methods in the interface abstract? In fact, it was always possible for the interface to override the methods of the Object class, for example, toString or clone , and these declarations do not make the methods abstract. (Some interfaces in the Java API override Object methods to attach javadoc comments. See the Comparator API for an example.) More importantly, as you will see shortly, in Java 8, interfaces can declare non-abstract methods.

To demonstrate the conversion to a functional interface, consider the Arrays.sort method. Its second parameter requires an instance of the Comparator , an interface with a single method. Just provide lambda:
 Arrays.sort(strs, (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length())); 

Behind the scenes, the Arrays.sort method gets an object of some class that implements the Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
 Comparator .   compare      -.         ,     -   ,     .     -   ,    ,  ,       . 

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
 Comparator .   compare      -.         ,     -   ,     .     -   ,    ,  ,       . 

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
 Comparator .   compare      -.         ,     -   ,     .     -   ,    ,  ,       . 

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.

Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.

Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
 Comparator .   compare      -.         ,     -   ,     .     -   ,    ,  ,       . 

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
 Comparator .   compare      -.         ,     -   ,     .     -   ,    ,  ,       . 

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
 Comparator .   compare      -.         ,     -   ,     .     -   ,    ,  ,       . 

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
 Comparator .   compare      -.         ,     -   ,     .     -   ,    ,  ,       . 

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.

Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.

Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
 Comparator .   compare      -.         ,     -   ,     .     -   ,    ,  ,       . 

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
 Comparator .   compare      -.         ,     -   ,     .     -   ,    ,  ,       . 

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
 Comparator .   compare      -.         ,     -   ,     .     -   ,    ,  ,       . 

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.

Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.

Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
 Comparator .   compare      -.         ,     -   ,     .     -   ,    ,  ,       . 

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
 Comparator .   compare      -.         ,     -   ,     .     -   ,    ,  ,       . 

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
 Comparator .   compare      -.         ,     -   ,     .     -   ,    ,  ,       . 

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
 Comparator .   compare      -.         ,     -   ,     .     -   ,    ,  ,       . 

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
 Comparator .   compare      -.         ,     -   ,     .     -   ,    ,  ,       . 

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
 Comparator .   compare      -.         ,     -   ,     .     -   ,    ,  ,       . 

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
 Comparator .   compare      -.         ,     -   ,     .     -   ,    ,  ,       . 

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
 Comparator .   compare      -.         ,     -   ,     .     -   ,    ,  ,       . 

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.

Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.

Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
 Comparator .   compare      -.         ,     -   ,     .     -   ,    ,  ,       . 

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
 Comparator .   compare      -.         ,     -   ,     .     -   ,    ,  ,       . 

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
 Comparator .   compare      -.         ,     -   ,     .     -   ,    ,  ,       . 

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
  1. Comparator . compare -. , - , . - , , , .

    – , - . . :
    button.setOnAction(event -> System.out.println("The button has been clicked!"));
    .

    , - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

    Java API java.util.function. , BiFunction <T, U, R> , U R. :
    BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
    , . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

    java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

    , , checked . - checked , . , :
    Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
    Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


    , , . , , event , . ,
    button.setOnAction(event -> System.out.println(event));
    , println setOnAction . :
    button.setOnAction(System.out::println);
    System.out::println , - x -> System.out.println(x) .

    , , . :
    Arrays.sort(strs, String::compareToIgnoreCase)
    :: . :

    object::instanceMethod Class::staticMethod Class::instanceMethod

    -, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

    , . , Math.max , int double . , , Math.max . , -, . .

    this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
    class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
    Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


    , , , new . , Button::new Button . ? . , . , , , :
    List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
    stream , map collect . , , map Button(String) . Button , , , , .

    . , int[]::new : . - x -> new int[x] .

    Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
    Object[] buttons = stream.toArray();
    . , . . Button[]::new toArray :
    Button[] buttons = stream.toArray(Button[]::new);
    toArray . .


    -. :
    public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
    :
    repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
    count text -. , -. , repeatText .

    , , . - repeatText . text count ?

    , , -. - :

    ; , ,
    - , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

    . - , , , Java . Java - . , . Java 8 .

    , - . Java, , , . - , . , :
    public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
    . - . , .
    int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
    , . matchCount++ , , , .

    . Java 8 final . -. final ; , , .

    , . . matchCount – , , .

    , , . ,
    List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
    , matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

    . . .

    , , - . 1, :
    int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
    , . , , , .

    - , . . , , .
    Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
    . , -. this - this , . , ,
    public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
    this.toString() toString Application , Runnable . this -. - doWork , this .


    . , , , . , :
    for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
    . forEach , .
    strList.forEach(System.out::println);
    , . Java , . Collection , forEach , , , Collection , , . Java.

    Java , ( ). . . Java 8 forEach Iterable , Collection , , .

    :
    interface Person { long getId(); default String getFirstName() { return "Jack"; } }
    : getId , , getFirstName . , Person , , , getId , , getFirstName .

    , , , Collection/AbstractCollection WindowListener/WindowAdapter . .
    , , ? C++ . , Java . :

    . , . . , ( ), .
    . getFirstName :
    interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
    , , ?
    class Student implements Person, Naming { ... }
    getFirstName , Person Naming . , Java . getFirstName Student . , :
    class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
    , Naming getFirstName :
    interface Naming { String getFirstName(); }
    Student Person ? , Java . , . , , .

    , -Java 8 . : . .

    . , , . , , Person Student :
    class Student extends Person implements Naming { ... }
    , . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


    - , - Java, - , . ? Java , . , , , . , , Java 8.
  2. Comparator . compare -. , - , . - , , , .

    – , - . . :
    button.setOnAction(event -> System.out.println("The button has been clicked!"));
    .

    , - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

    Java API java.util.function. , BiFunction <T, U, R> , U R. :
    BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
    , . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

    java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

    , , checked . - checked , . , :
    Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
    Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


    , , . , , event , . ,
    button.setOnAction(event -> System.out.println(event));
    , println setOnAction . :
    button.setOnAction(System.out::println);
    System.out::println , - x -> System.out.println(x) .

    , , . :
    Arrays.sort(strs, String::compareToIgnoreCase)
    :: . :

    object::instanceMethod Class::staticMethod Class::instanceMethod

    -, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

    , . , Math.max , int double . , , Math.max . , -, . .

    this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
    class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
    Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


    , , , new . , Button::new Button . ? . , . , , , :
    List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
    stream , map collect . , , map Button(String) . Button , , , , .

    . , int[]::new : . - x -> new int[x] .

    Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
    Object[] buttons = stream.toArray();
    . , . . Button[]::new toArray :
    Button[] buttons = stream.toArray(Button[]::new);
    toArray . .


    -. :
    public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
    :
    repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
    count text -. , -. , repeatText .

    , , . - repeatText . text count ?

    , , -. - :

    ; , ,
    - , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

    . - , , , Java . Java - . , . Java 8 .

    , - . Java, , , . - , . , :
    public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
    . - . , .
    int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
    , . matchCount++ , , , .

    . Java 8 final . -. final ; , , .

    , . . matchCount – , , .

    , , . ,
    List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
    , matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

    . . .

    , , - . 1, :
    int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
    , . , , , .

    - , . . , , .
    Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
    . , -. this - this , . , ,
    public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
    this.toString() toString Application , Runnable . this -. - doWork , this .


    . , , , . , :
    for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
    . forEach , .
    strList.forEach(System.out::println);
    , . Java , . Collection , forEach , , , Collection , , . Java.

    Java , ( ). . . Java 8 forEach Iterable , Collection , , .

    :
    interface Person { long getId(); default String getFirstName() { return "Jack"; } }
    : getId , , getFirstName . , Person , , , getId , , getFirstName .

    , , , Collection/AbstractCollection WindowListener/WindowAdapter . .
    , , ? C++ . , Java . :

    . , . . , ( ), .
    . getFirstName :
    interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
    , , ?
    class Student implements Person, Naming { ... }
    getFirstName , Person Naming . , Java . getFirstName Student . , :
    class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
    , Naming getFirstName :
    interface Naming { String getFirstName(); }
    Student Person ? , Java . , . , , .

    , -Java 8 . : . .

    . , , . , , Person Student :
    class Student extends Person implements Naming { ... }
    , . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


    - , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
 Comparator .   compare      -.         ,     -   ,     .     -   ,    ,  ,       . 

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
 Comparator .   compare      -.         ,     -   ,     .     -   ,    ,  ,       . 

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
 Comparator .   compare      -.         ,     -   ,     .     -   ,    ,  ,       . 

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
 Comparator .   compare      -.         ,     -   ,     .     -   ,    ,  ,       . 

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
 Comparator .   compare      -.         ,     -   ,     .     -   ,    ,  ,       . 

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.

Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.

Comparator . compare -. , - , . - , , , .

– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.

, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .

Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .

java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .

, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).


, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .

, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :

object::instanceMethod Class::staticMethod Class::instanceMethod

-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .

, . , Math.max , int double . , , Math.max . , -, . .

this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)


, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .

. , int[]::new : . - x -> new int[x] .

Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .


-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .

, , . - repeatText . text count ?

, , -. - :

; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)

. - , , , Java . Java - . , . Java 8 .

, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .

. Java 8 final . -. final ; , , .

, . . matchCount – , , .

, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .

. . .

, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .

- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .


. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.

Java , ( ). . . Java 8 forEach Iterable , Collection , , .

:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .

, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :

. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .

, -Java 8 . : . .

. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .


- , - Java, - , . ? Java , . , , , . , , Java 8.

Source: https://habr.com/ru/post/224593/


All Articles