待望のラムダ式がついにJava 8の新しいバージョンに登場しました。 おそらく、これは最新バージョンの最も重要な新機能です。 より速く記述し、コードを明確にし、関数型プログラミングの世界への扉を開くことができます。 この記事では、この仕組みを説明します。
Javaは、オブジェクト指向プログラミングがアプリケーション開発の主なパラダイムであった90年代にオブジェクト指向言語として考案されました。 そのはるか前に、オブジェクト指向プログラミングがあり、LispやSchemeなどの関数型プログラミング言語がありましたが、それらの利点は学術環境以外では評価されませんでした。 関数型プログラミングは最近、並列プログラミングとイベントベースのプログラミング(「リアクティブ」)に適しているため、重要性が大幅に高まっています。 これは、オブジェクトの向きが悪いことを意味するものではありません。 それどころか、代わりに、勝利戦略はオブジェクト指向のプログラミングと機能を混合することです。 これは、並列処理を必要としない場合でも理にかなっています。 たとえば、言語に関数式の便利な構文がある場合、コレクションライブラリは強力なAPIを取得できます。
Java 8の主な改善点は、機能的なソフトウェア構成のサポートがオブジェクト指向ベースに追加されたことです。 
この記事では、基本的な構文と、いくつかの重要なコンテキストでそれを使用する方法を示します。 ラムダの概念のキーポイント:
- ラムダ式は、パラメーターを持つコードのブロックです。
 - 後でコードブロックを実行する場合は、ラムダ式を使用します。
 - ラムダ式は、機能的なインターフェースに変換できます。
 - ラムダ式は、スコープから最終変数にアクセスできます。
 - メソッドとコンストラクターの参照は、メソッドまたはコンストラクターを呼び出さずに参照します。
 - これで、特定の実装を提供するインターフェイスにデフォルトのメソッドと静的メソッドを追加できます。
 - 複数のインターフェイスからのデフォルトメソッド間の競合を解決する必要があります。
 
なぜラムダが必要ですか?
ラムダ式は、別の場所に渡すことができるコードのブロックであるため、後で1回以上実行できます。 構文(および好奇心)盛な名前)を掘り下げる前に、一歩戻って、以前Javaで同様のコードブロックを使用した場所を見てみましょう。
別のスレッドでアクションを実行する場合は、次のように
Runnableの
runメソッドにアクションを入れます。
 class MyRunner implements Runnable { public void run() { for (int i = 0; i < 1000; i++) doWork(); } ... } 
次に、このコードを実行するときに、 
MyRunnerクラスをインスタンス化します。 インスタンスをスレッドプールに配置するか、簡単に実行して新しいスレッドを開始できます。
 MyRunner r = new MyRunner(); new Thread(r).start(); 
重要な点は、 
runメソッドには別のスレッドで実行する必要があるコードが含まれていることです。
カスタムコンパレータを使用したソートを検討してください。 デフォルトではなく長さで行を並べ替える場合は、 
Comparatorオブジェクトを
sortメソッドに渡すことができます。
 class LengthStringComparator implements Comparator<String> { public int compare(String firstStr, String secondStr) { return Integer.compare(firstStr.length(),secondStr.length()); } } Arrays.sort(strings, new LengthStringComparator ()); 
sortメソッドは、配列が並べ替えられるまで要素の順序が正しくない場合、要素を並べ替えて
compareメソッドを呼び出します。 要素を比較するために必要なコードスニペットを
sortメソッドに提供し
sortこのコードは、おそらく再定義する必要のない残りの並べ替えロジックに埋め込まれます。 
Integer.compare (, )を呼び出すと、xとyが等しい場合はゼロ、x <yの場合は負の数、x> yの場合は正の数を返すことに注意してください。 この静的メソッドはJava 7で追加されました。xとyを比較するためにx-yを計算する必要はありません。計算により、反対符号の大きなオペランドのオーバーフローが発生する可能性があるためです。
遅延実行の別の例として、ボタンのコールバックを検討してください。 リスナーインターフェイスを実装するクラスのメソッドにコールバックアクションを配置し、インスタンスを作成して、インスタンスを登録します。 これは、多くのプログラマが「匿名クラスの匿名インスタンス」という構文を使用する一般的なシナリオです。
 button.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent event) { System.out.println("The button has been clicked!"); } }); 
ここでは、 
handleメソッド内のコードが重要です。 このコードは、ボタンが押されるたびに実行されます。
Java 8はJavaFXをSwing GUIツールキットの後継として位置付けているため、これらの例ではJavaFXを使用しています。 詳細は関係ありません。 Swing、JavaFX、Androidの各ユーザーインターフェイスライブラリでは、ボタンが押されたときに実行するコードをボタンに渡します。
3つの例すべてで、同じアプローチを見ました。 コードブロックが誰かに渡されました—スレッドプール、ソートメソッド、またはボタン。 このコードはしばらくしてから呼び出されました。
これまでのところ、Javaでコードを渡すことは簡単ではありませんでした。 どこでもコードブロックを渡すことはできません。 Javaはオブジェクト指向言語であるため、適切なコードを持つメソッドを持つクラスに属するオブジェクトを作成する必要がありました。
他の言語では、コードのブロックを直接操作できます。 Javaデザイナーは、長い間この機能を追加することに抵抗してきました。 結局のところ、Javaの大きな強みは、そのシンプルさと一貫性にあります。 言語は、もう少し簡潔なコードを提供するすべての機能が含まれていると、非常に煩雑になります。 ただし、これらの他の言語では、ストリームの生成やクリックボタンハンドラーの登録が簡単になるだけではありません。 APIの多くは、よりシンプルで一貫性があり、強力です。 Javaでは、特定の機能を実装するクラスオブジェクトを受け入れる同様のインターフェイスを作成できますが、そのようなAPIは使用するには不便です。
最近、問題は関数型プログラミングのためにJavaを拡張するかどうかではなく、どのように拡張するかです。 これがJavaでうまく機能することが明らかになるまで、数年の実験が必要でした。 次のセクションでは、Java 8でコードブロックを操作する方法について説明します。
ラムダ式の構文
前の並べ替えの例を再度検討してください。 どの行が短いかをチェックするコードを渡します。 計算する
 Integer.compare(firstStr.length(), secondStr.length()) 
firstStrおよび
secondStrとは何ですか? 彼らは両方の行です! Javaは強く型付けされた言語であり、型を指定する必要があります。
 (String firstStr, String secondStr) -> Integer.compare(firstStr.length(),secondStr.length()) 
最初のラムダ式を見ました! このような式は、コードに渡す必要のある変数の仕様を伴うコードのブロックです。
なぜそんな名前なの? 何年も前、コンピューターがまだなかったとき、論理学者のアロンゾ教会は、数学関数を効果的に計算するための意味を形式化したかったのです。 (奇妙なことに、存在することが知られている関数がありますが、その値を計算する方法を誰も知りません。)彼はギリシャ文字のラムダ(λ)を使用してパラメーターをマークしました。 もし彼がJava APIを知っていたら、おそらくあなたが見たものとあまり似ていない何かを書くでしょう。
なぜ文字はλですか? 教会はアルファベットのすべての文字を使用しましたか? 実際、Principia Mathematicaの由緒ある研究では、記号ˆを使用して、パラメータに大文字のラムダ(Λ)を使用するように教会を促した自由変数を示しています。 しかし、結局、彼は小文字の手紙に切り替えました。 それ以来、可変パラメータを持つ式は「ラムダ式」と呼ばれています。
Javaのラムダ式の1つの形式を見ました:パラメーター、矢印->および式。 コードが単一の式に収まらない計算を実行する場合は、メソッドを記述するのと同じ方法で記述します:{}で囲まれ、明示的な
returnます。 例えば
 (String firstStr, String secondStr) -> { if (firstStr.length() < secondStr.length()) return -1; else if (firstStr.length() > secondStr.length()) return 1; else return 0; } 
ラムダ式にパラメーターがない場合でも、パラメーターのないメソッドの場合と同様に、空のブラケットを配置します。
 () -> { for (int i = 0; i < 1000; i++) doWork(); } 
ラムダ式のパラメーター型を推測できる場合は、それらを省略できます。 例えば
 Comparator<String> comp = (firstStr, secondStr)  
ここで、コンパイラは、ラムダ式が文字列コンパレータに割り当てられているため、 
firstStrと
secondStrが文字列である必要があると結論付けることができます。 (この割り当てについては後で詳しく見ていきます。)
メソッドに表示されるタイプのパラメーターが1つある場合は、括弧を省略することもできます。
 EventHandler<ActionEvent> listener = event -> System.out.println("The button has been clicked!");  
メソッドパラメータの場合と同じ方法で、ラムダパラメータに注釈または
final修飾子を追加できます。
 (final String var) -> ... (@NonNull String var) -> ... 
ラムダ式の結果タイプを指定することはありません。 コンテキストから常に明確です。 たとえば、式
 (String firstStr, String secondStr) -> Integer.compare(firstStr.length(), secondStr.length()) 
int型の結果が期待されるコンテキストで使用できます。
ラムダ式は、一部のブランチでは値を返すことができず、他のブランチでは値を返すことができないことに注意してください。 たとえば、 
(int x) -> { if (x <= 1) return -1; } (int x) -> { if (x <= 1) return -1; }は無効です。
機能的インターフェース
説明したように、Javaには
Runnableや
Comparatorなどのコードブロックをカプセル化する既存のインターフェイスが多数あります。 ラムダ式は、これらのインターフェースと下位互換性があります。
1つの抽象メソッドを持つインターフェイスオブジェクトが必要な場合は、ラムダ式を指定できます。 このようなインターフェイスは、機能インターフェイスと呼ばれます。
機能的なインターフェイスに単一の抽象メソッドが必要なのはなぜかと思うかもしれません。 インターフェイスのすべてのメソッドは抽象的ではありませんか? 実際、インターフェースが
toStringや
cloneなどの
Objectクラスのメソッドをオーバーライドすることは常に可能であり、これらの宣言はメソッドを抽象化しません。 (Java APIの一部のインターフェイスは、 
Objectメソッドをオーバーライドしてjavadocコメントを添付します。例については、Comparator APIを参照してください。)さらに重要なことは、Java 8ではインターフェイスが非抽象メソッドを宣言できることです。
機能的なインターフェイスへの変換を示すために、 
Arrays.sortメソッドを検討して
Arrays.sort 。 2番目のパラメーターには、 
Comparatorインスタンス、 
Comparator 1つのメソッドを持つインターフェースが必要です。 ラムダを提供するだけです:
 Arrays.sort(strs, (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length())); 
舞台裏では、 
Arrays.sortメソッドは
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.
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.