run method of Runnable , like this: class MyRunner implements Runnable { public void run() { for (int i = 0; i < 1000; i++) doWork(); } ... } MyRunner class. You can put an instance in the thread pool, or simply enter and start a new thread: MyRunner r = new MyRunner(); new Thread(r).start(); run method contains the code that needs to be executed in a separate thread.Comparator object to the sort method: class LengthStringComparator implements Comparator<String> { public int compare(String firstStr, String secondStr) { return Integer.compare(firstStr.length(),secondStr.length()); } } Arrays.sort(strings, new LengthStringComparator ()); sort method still calls the compare method, rearranging the elements if they are not in order until the array is sorted. You provide a sort of code to the sort method for comparing elements, and this code is embedded in the rest of the sorting logic, which you probably do not need to override. Note that the call to Integer.compare (, ) returns zero if x and y are equal, a negative number if x <y, and a positive number if x> y. This static method was added to Java 7. You do not need to calculate x - y to compare x and y, because the calculation may cause overflow for large operands of the opposite sign. button.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent event) { System.out.println("The button has been clicked!"); } }); handle method is important here. This code is executed whenever the button is pressed. Integer.compare(firstStr.length(), secondStr.length()) firstStr and secondStr ? They are both lines! Java is a strongly typed language, and we must specify the types: (String firstStr, String secondStr) -> Integer.compare(firstStr.length(),secondStr.length()) return expressions. For example, (String firstStr, String secondStr) -> { if (firstStr.length() < secondStr.length()) return -1; else if (firstStr.length() > secondStr.length()) return 1; else return 0; } () -> { for (int i = 0; i < 1000; i++) doWork(); } Comparator<String> comp = (firstStr, secondStr) // Same as (String firstStr, String secondStr) -> Integer.compare(firstStr.length(),secondStr.length()); firstStr and secondStr must be strings, because the lambda expression is assigned to the string comparator. (We will look at this assignment more closely later.) EventHandler<ActionEvent> listener = event -> System.out.println("The button has been clicked!"); // Instead of (event) -> or (ActionEvent event) -> final modifier to lambda parameters in the same way as for method parameters: (final String var) -> ... (@NonNull String var) -> ... (String firstStr, String secondStr) -> Integer.compare(firstStr.length(), secondStr.length()) int result is expected.(int x) -> { if (x <= 1) return -1; } (int x) -> { if (x <= 1) return -1; } is not valid.Runnable or Comparator . Lambda expressions are backward compatible with these interfaces.Object class, for example, toString or clone , and these declarations do not make the methods abstract. (Some interfaces in the Java API override Object methods to attach javadoc comments. See the Comparator API for an example.) More importantly, as you will see shortly, in Java 8, interfaces can declare non-abstract methods.Arrays.sort method. Its second parameter requires an instance of the Comparator , an interface with a single method. Just provide lambda: Arrays.sort(strs, (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length())); Arrays.sort method gets an object of some class that implements the Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8. Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8. Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8. Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8. Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8. Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8. Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8. Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8.Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8.Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8. Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8. Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8. Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8. Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8. Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8. Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8.Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8.Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8.Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8.Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8. Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8. Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8.Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8.Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8. Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8. Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8. Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8. Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8. Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8. Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8.Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8.Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8. Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8. Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8. Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8. Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8.Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8.Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8.Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8.Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8. Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8. Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8. Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8. Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8. Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8. Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8. Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8. Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8. Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8. Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8. Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8. Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8.Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8.Comparator . compare -. , - , . - , , , .
– , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int , , . Java - Object , Object . Java , .
Java API java.util.function. , BiFunction <T, U, R> , U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort , BiFunction . , . Java . , Comparator , , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface . . , . Javadoc , . . , , . @FunctionalInterface - .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("…"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run , . , . -. , . , call Callable . , Callable ( return null ).
, , . , , event , . ,
button.setOnAction(event -> System.out.println(event));
, println setOnAction . :
button.setOnAction(System.out::println);
System.out::println , - x -> System.out.println(x) .
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println x -> System.out.println(x) . , Math::pow (x, y) -> Math.pow(x, y) . . , String::compareToIgnoreCase - , (x, y) -> x.compareToIgnoreCase(y) .
, . , Math.max , int double . , , Math.max . , -, . .
this . , this::equals – , x -> this.equals(x) . super . super::instanceMethod this . , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable , super::speak , speak . ( , , EnclosingClass.this::method EnclosingClass.super::method .)
, , , new . , Button::new Button . ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream , map collect . , , map Button(String) . Button , , , , .
. , int[]::new : . - x -> new int[x] .
Java. T. new T[n] , new Object[n] . . , , . Stream toArray , Object :
Object[] buttons = stream.toArray();
. , . . Button[]::new toArray :
Button[] buttons = stream.toArray(Button[]::new);
toArray . .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count text -. , -. , repeatText .
, , . - repeatText . text count ?
, , -. - :
; , ,
- , text count . , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++ , , , .
. Java 8 final . -. final ; , , .
, . . matchCount – , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs final . ( final , .) matchedObjs ArrayList . , . add , .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this - this , . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString() toString Application , Runnable . this -. - doWork , this .
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach , .
strList.forEach(System.out::println);
, . Java , . Collection , forEach , , , Collection , , . Java.
Java , ( ). . . Java 8 forEach Iterable , Collection , , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId , , getFirstName . , Person , , , getId , , getFirstName .
, , , Collection/AbstractCollection WindowListener/WindowAdapter . .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName :
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName , Person Naming . , Java . getFirstName Student . , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming getFirstName :
interface Naming { String getFirstName(); }
Student Person ? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person Student :
class Student extends Person implements Naming { ... }
, . Student getFirstName Person , , Naming getFirstName . « » . « » Java 7. , , , . : , Object . , toString equals , , List . , Object.toString Object.equals .
- , - Java, - , . ? Java , . , , , . , , Java 8. 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.Source: https://habr.com/ru/post/224593/
All Articles