90 рдХреЗ рджрд╢рдХ рдореЗрдВ рдЬрд╛рд╡рд╛ рдХреЛ рдПрдХ рд╡рд╕реНрддреБ-рдЙрдиреНрдореБрдЦ рднрд╛рд╖рд╛ рдХреЗ рд░реВрдк рдореЗрдВ рдХрд▓реНрдкрдирд╛ рдХреА рдЧрдИ рдереА, рдЬрдм рдЕрдиреБрдкреНрд░рдпреЛрдЧ-рд╡рд┐рдХрд╛рд╕ рдореЗрдВ рдСрдмреНрдЬреЗрдХреНрдЯ-рдУрд░рд┐рдПрдВрдЯреЗрдб рдкреНрд░реЛрдЧреНрд░рд╛рдорд┐рдВрдЧ рдореБрдЦреНрдп рдкреНрд░рддрд┐рдорд╛рди рдерд╛ред рдЗрд╕рд╕реЗ рдкрд╣рд▓реЗ, рдСрдмреНрдЬреЗрдХреНрдЯ-рдУрд░рд┐рдПрдВрдЯреЗрдб рдкреНрд░реЛрдЧреНрд░рд╛рдорд┐рдВрдЧ рдереА, рд▓рд┐рд╕реНрдк рдФрд░ рд╕реНрдХреАрдо рдЬреИрд╕реА рдХрд╛рд░реНрдпрд╛рддреНрдордХ рдкреНрд░реЛрдЧреНрд░рд╛рдорд┐рдВрдЧ рднрд╛рд╖рд╛рдПрдВ рдереАрдВ, рд▓реЗрдХрд┐рди рдЕрдХрд╛рджрдорд┐рдХ рд╡рд╛рддрд╛рд╡рд░рдг рдХреЗ рдмрд╛рд╣рд░ рдЙрдирдХреЗ рдлрд╛рдпрджреЗ рдХреА рд╕рд░рд╛рд╣рдирд╛ рдирд╣реАрдВ рдХреА рдЧрдИ рдереАред рд╣рд╛рд▓ рд╣реА рдореЗрдВ, рдХрд╛рд░реНрдпрд╛рддреНрдордХ рдкреНрд░реЛрдЧреНрд░рд╛рдорд┐рдВрдЧ рдХрд╛ рдорд╣рддреНрд╡ рдХрд╛рдлреА рдмрдврд╝ рдЧрдпрд╛ рд╣реИ рдХреНрдпреЛрдВрдХрд┐ рдпрд╣ рд╕рдорд╡рд░реНрддреА рдкреНрд░реЛрдЧреНрд░рд╛рдорд┐рдВрдЧ рдФрд░ рдЗрд╡реЗрдВрдЯ-рдЖрдзрд╛рд░рд┐рдд рдкреНрд░реЛрдЧреНрд░рд╛рдорд┐рдВрдЧ ("рдкреНрд░рддрд┐рдХреНрд░рд┐рдпрд╛рд╢реАрд▓") рдХреЗ рд▓рд┐рдП рдЕрдЪреНрдЫреА рддрд░рд╣ рд╕реЗ рдЕрдиреБрдХреВрд▓ рд╣реИред рдЗрд╕рдХрд╛ рдорддрд▓рдм рдпрд╣ рдирд╣реАрдВ рд╣реИ рдХрд┐ рдСрдмреНрдЬреЗрдХреНрдЯ рдУрд░рд┐рдПрдВрдЯреЗрд╢рди рдЦрд░рд╛рдм рд╣реИред рдЗрд╕рдХреЗ рд╡рд┐рдкрд░реАрдд, рдЗрд╕рдХреЗ рдмрдЬрд╛рдп, рдПрдХ рдЬреАрдд рдХреА рд░рдгрдиреАрддрд┐ рдСрдмреНрдЬреЗрдХреНрдЯ-рдУрд░рд┐рдПрдВрдЯреЗрдб рдкреНрд░реЛрдЧреНрд░рд╛рдорд┐рдВрдЧ рдФрд░ рдХрд╛рд░реНрдпрд╛рддреНрдордХ рдХреЛ рдорд┐рд▓рд╛рдирд╛ рд╣реИред рдпрд╣ рд╕рдордЭ рдореЗрдВ рдЖрддрд╛ рд╣реИ рднрд▓реЗ рд╣реА рдЖрдкрдХреЛ рд╕рдорд╛рдирддрд╛ рдХреА рдЖрд╡рд╢реНрдпрдХрддрд╛ рди рд╣реЛред рдЙрджрд╛рд╣рд░рдг рдХреЗ рд▓рд┐рдП, рд╕рдВрдЧреНрд░рд╣ рдкреБрд╕реНрддрдХрд╛рд▓рдпреЛрдВ рдХреЛ рдПрдХ рд╢рдХреНрддрд┐рд╢рд╛рд▓реА рдПрдкреАрдЖрдИ рдорд┐рд▓ рд╕рдХрддрд╛ рд╣реИ рдпрджрд┐ рднрд╛рд╖рд╛ рдореЗрдВ рдХрд╛рд░реНрдпрд╛рддреНрдордХ рдЕрднрд┐рд╡реНрдпрдХреНрддрд┐рдпреЛрдВ рдХреЗ рд▓рд┐рдП рд╕реБрд╡рд┐рдзрд╛рдЬрдирдХ рд╡рд╛рдХреНрдпрд╡рд┐рдиреНрдпрд╛рд╕ рд╣реИред
рдЬрд╛рд╡рд╛ 8 рдореЗрдВ рдПрдХ рдмрдбрд╝рд╛ рд╕реБрдзрд╛рд░ рдЗрд╕рдХреЗ рдЙрджреНрджреЗрд╢реНрдп рдЙрдиреНрдореБрдЦ рдЖрдзрд╛рд░ рдкрд░ рдХрд╛рд░реНрдпрд╛рддреНрдордХ рд╕реЙрдлреНрдЯрд╡реЗрдпрд░ рдирд┐рд░реНрдорд╛рдг рдХреЗ рд▓рд┐рдП рд╕рдорд░реНрдерди рдХрд╛ рдЬреЛрдбрд╝ рд╣реИред рдЗрд╕ рд▓реЗрдЦ рдореЗрдВ, рдореИрдВ рдореВрд▓ рд╕рд┐рдВрдЯреИрдХреНрд╕ рдХрд╛ рдкреНрд░рджрд░реНрд╢рди рдХрд░реВрдВрдЧрд╛ рдФрд░ рдЗрд╕реЗ рдХрдИ рдорд╣рддреНрд╡рдкреВрд░реНрдг рд╕рдВрджрд░реНрднреЛрдВ рдореЗрдВ рдХреИрд╕реЗ рдЙрдкрдпреЛрдЧ рдХрд░реВрдВред рд▓рдВрдмреЛрджрд░ рдХреА рдЕрд╡рдзрд╛рд░рдгрд╛ рдХреЗ рдореБрдЦреНрдп рдмрд┐рдВрджреБ:
- рдПрдХ рд▓реИрдореНрдмреНрдбрд╛ рдЕрднрд┐рд╡реНрдпрдХреНрддрд┐ рдорд╛рдкрджрдВрдбреЛрдВ рдХреЗ рд╕рд╛рде рдХреЛрдб рдХрд╛ рдПрдХ рдмреНрд▓реЙрдХ рд╣реИред
- рдЬрдм рдЖрдк рд╕рдордп рдореЗрдВ рдмрд╛рдж рдореЗрдВ рдмрд┐рдВрджреБ рдкрд░ рдХреЛрдб рдХреЗ рдПрдХ рдмреНрд▓реЙрдХ рдХреЛ рдирд┐рд╖реНрдкрд╛рджрд┐рдд рдХрд░рдирд╛ рдЪрд╛рд╣рддреЗ рд╣реИрдВ, рддреЛ рдПрдХ рд▓реИрдореНрдмреНрдбрд╛ рдЕрднрд┐рд╡реНрдпрдХреНрддрд┐ рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░реЗрдВред
- рд▓реИрдореНрдмреНрдбрд╛ рдЕрднрд┐рд╡реНрдпрдХреНрддрд┐рдпреЛрдВ рдХреЛ рдХрд╛рд░реНрдпрд╛рддреНрдордХ рдЗрдВрдЯрд░рдлреЗрд╕ рдореЗрдВ рдмрджрд▓рд╛ рдЬрд╛ рд╕рдХрддрд╛ рд╣реИред
- рд▓реИрдВрдмрдбрд╛ рдПрдХреНрд╕рдкреНрд░реЗрд╢рдВрд╕ рдХреЗ рджрд╛рдпрд░реЗ рд╕реЗ рдЕрдВрддрд┐рдо рдЪрд░ рддрдХ рдкрд╣реБрдВрдЪ рд╣реИред
- рдореЗрдердб рдФрд░ рдХрдВрд╕реНрдЯреНрд░рдХреНрдЯрд░ рд░реЗрдлрд░реЗрдВрд╕ рдореЗрдердбреНрд╕ рдпрд╛ рдХрдВрд╕реНрдЯреНрд░рдХреНрдЯрд░реНрд╕ рдХреЛ рдмрд┐рдирд╛ рдмреБрд▓рд╛рдП рд░реЗрдлрд░ рдХрд░рддреЗ рд╣реИрдВред
- рдЕрдм рдЖрдк рдбрд┐рдлреЙрд▓реНрдЯ рдореЗрдердбреНрд╕ рдФрд░ рд╕реНрдЯреИрдЯрд┐рдХ рдореЗрдердбреНрд╕ рдХреЛ рд╕реНрдкреЗрд╕рд┐рдлрд┐рдХ рдореЗрдВ рдЬреЛрдбрд╝ рд╕рдХрддреЗ рд╣реИрдВ рдЬреЛ рд╕реНрдкреЗрд╕рд┐рдлрд┐рдХ рдЗрдореНрдкреНрд▓реАрдореЗрдВрдЯреЗрд╢рди рдкреНрд░рджрд╛рди рдХрд░рддреЗ рд╣реИрдВред
- рдЖрдкрдХреЛ рдХрдИ рдЗрдВрдЯрд░рдлрд╝реЗрд╕ рд╕реЗ рдбрд┐рдлрд╝реЙрд▓реНрдЯ рд╡рд┐рдзрд┐рдпреЛрдВ рдХреЗ рдмреАрдЪ рдХрд┐рд╕реА рднреА рдЯрдХрд░рд╛рд╡ рдХреЛ рд╣рд▓ рдХрд░рдирд╛ рд╣реЛрдЧрд╛ред
рд▓рдВрдмреЛрджрд░ рдХреА рдЬрд░реВрд░рдд рдХреНрдпреЛрдВ рд╣реИ?
рдПрдХ рд▓рдВрдмреЛрджрд░ рдЕрднрд┐рд╡реНрдпрдХреНрддрд┐ рдХреЛрдб рдХрд╛ рдПрдХ рдмреНрд▓реЙрдХ рд╣реИ рдЬрд┐рд╕реЗ рдХрд┐рд╕реА рдЕрдиреНрдп рд╕реНрдерд╛рди рдкрд░ рдкрд╛рд░рд┐рдд рдХрд┐рдпрд╛ рдЬрд╛ рд╕рдХрддрд╛ рд╣реИ, рдЗрд╕рд▓рд┐рдП рдЗрд╕реЗ рдмрд╛рдж рдореЗрдВ, рдПрдХ рдпрд╛ рдЕрдзрд┐рдХ рдмрд╛рд░ рдирд┐рд╖реНрдкрд╛рджрд┐рдд рдХрд┐рдпрд╛ рдЬрд╛ рд╕рдХрддрд╛ рд╣реИред рд╕рд┐рдВрдЯреИрдХреНрд╕ (рдФрд░ рдПрдХ рдЬрд┐рдЬреНрдЮрд╛рд╕реБ рдирд╛рдо) рдореЗрдВ рдЬрд╛рдиреЗ рд╕реЗ рдкрд╣рд▓реЗ, рдЖрдЗрдП рдПрдХ рдХрджрдо рдкреАрдЫреЗ рджреЗрдЦреЗрдВ рдФрд░ рджреЗрдЦреЗрдВ рдХрд┐ рдЖрдкрдиреЗ рдЬрд╛рд╡рд╛ рдореЗрдВ рдХреЛрдб рдХреЗ рд╕рдорд╛рди рдмреНрд▓реЙрдХ рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд╣рд╛рдВ рдХрд┐рдпрд╛ рдерд╛ред
рдпрджрд┐ рдЖрдк рдЕрд▓рдЧ рдереНрд░реЗрдб рдореЗрдВ рдХреНрд░рд┐рдпрд╛рдУрдВ рдХреЛ рдирд┐рд╖реНрдкрд╛рджрд┐рдд рдХрд░рдирд╛ рдЪрд╛рд╣рддреЗ рд╣реИрдВ, рддреЛ рдЖрдк рдЙрдиреНрд╣реЗрдВ
Runnable
рдХреА
run
рд╡рд┐рдзрд┐ рдореЗрдВ рдбрд╛рд▓рддреЗ рд╣реИрдВ, рдЬреИрд╕реЗ:
class MyRunner implements Runnable { public void run() { for (int i = 0; i < 1000; i++) doWork(); } ... }
рдлрд┐рд░, рдЬрдм рдЖрдк рдЗрд╕ рдХреЛрдб рдХреЛ рдирд┐рд╖реНрдкрд╛рджрд┐рдд рдХрд░рдирд╛ рдЪрд╛рд╣рддреЗ рд╣реИрдВ, рддреЛ рдЖрдк
MyRunner
рд╡рд░реНрдЧ рдХреЛ
MyRunner
ред рдЖрдк рдереНрд░реЗрдб рдкреВрд▓ рдореЗрдВ рдЙрджрд╛рд╣рд░рдг рдбрд╛рд▓ рд╕рдХрддреЗ рд╣реИрдВ, рдпрд╛ рдЗрд╕реЗ рдЖрд╕рд╛рди рдХрд░ рд╕рдХрддреЗ рд╣реИрдВ рдФрд░ рдПрдХ рдирдпрд╛ рдзрд╛рдЧрд╛ рд╢реБрд░реВ рдХрд░ рд╕рдХрддреЗ рд╣реИрдВ:
MyRunner r = new MyRunner(); new Thread(r).start();
рдореБрдЦреНрдп рдмрд┐рдВрджреБ рдпрд╣ рд╣реИ рдХрд┐
run
рд╡рд┐рдзрд┐ рдореЗрдВ рдХреЛрдб рд╣реЛрддрд╛ рд╣реИ рдЬрд┐рд╕реЗ рдЕрд▓рдЧ рдереНрд░реЗрдб рдореЗрдВ рдирд┐рд╖реНрдкрд╛рджрд┐рдд рдХрд░рдиреЗ рдХреА рдЖрд╡рд╢реНрдпрдХрддрд╛ рд╣реЛрддреА рд╣реИред
рдПрдХ рдХрд╕реНрдЯрдо рддреБрд▓рдирд┐рддреНрд░ рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░рдХреЗ рдЫрдВрдЯрд╛рдИ рдкрд░ рд╡рд┐рдЪрд╛рд░ рдХрд░реЗрдВред рдпрджрд┐ рдЖрдк рд▓рд╛рдЗрдиреЛрдВ рдХреЛ рд▓рдВрдмрд╛рдИ рдХреЗ рдЖрдзрд╛рд░ рдкрд░ рдХреНрд░рдордмрджреНрдз рдХрд░рдирд╛ рдЪрд╛рд╣рддреЗ рд╣реИрдВ, рдФрд░ рдбрд┐рдлрд╝реЙрд▓реНрдЯ рд░реВрдк рд╕реЗ рдирд╣реАрдВ, рддреЛ рдЖрдк
Comparator
рд╡рд┐рдзрд┐ рдХреЛ
sort
рд╡рд┐рдзрд┐ рд╕реЗ рдкрд╛рд╕ рдХрд░ рд╕рдХрддреЗ рд╣реИрдВ:
class LengthStringComparator implements Comparator<String> { public int compare(String firstStr, String secondStr) { return Integer.compare(firstStr.length(),secondStr.length()); } } Arrays.sort(strings, new LengthStringComparator ());
sort
рд╡рд┐рдзрд┐ рдЕрднреА рднреА
compare
рдкрджреНрдзрддрд┐ рдХреЛ рдХреЙрд▓ рдХрд░рддреА рд╣реИ, рддрддреНрд╡реЛрдВ рдХреЛ рдлрд┐рд░ рд╕реЗ рд╡реНрдпрд╡рд╕реНрдерд┐рдд рдХрд░рддреА рд╣реИ рдпрджрд┐ рд╡реЗ рдХреНрд░рдо рдореЗрдВ рдирд╣реАрдВ рд╣реИрдВ, рдЬрдм рддрдХ рдХрд┐ рд╕рд░рдгреА рдХреЛ рд╕реЙрд░реНрдЯ рдирд╣реАрдВ рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред рдЖрдк рддрддреНрд╡реЛрдВ рдХреА рддреБрд▓рдирд╛ рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рдЖрд╡рд╢реНрдпрдХ рдХреЛрдб рд╕реНрдирд┐рдкреЗрдЯ рдХреЗ рд╕рд╛рде
sort
рд╡рд┐рдзрд┐ рдкреНрд░рджрд╛рди рдХрд░рддреЗ рд╣реИрдВ, рдФрд░ рдпрд╣ рдХреЛрдб рдмрд╛рдХреА рдкреНрд░рдХрд╛рд░ рдХреЗ рд▓реЙрдЬрд┐рдХ рдореЗрдВ рдПрдореНрдмреЗрдбреЗрдб рд╣реЛрддрд╛ рд╣реИ, рдЬрд┐рд╕реЗ рд╢рд╛рдпрдж рдЖрдкрдХреЛ рдлрд┐рд░ рд╕реЗ рдкрд░рд┐рднрд╛рд╖рд┐рдд рдХрд░рдиреЗ рдХреА рдЖрд╡рд╢реНрдпрдХрддрд╛ рдирд╣реАрдВ рд╣реИред рдзреНрдпрд╛рди рджреЗрдВ рдХрд┐
Integer.compare (, )
рдХреЙрд▓ рдХрд░рдиреЗ рдкрд░ рд╢реВрдиреНрдп рд╡рд╛рдкрд╕ рдЖ рдЬрд╛рддрд╛ рд╣реИ рдпрджрд┐ x рдФрд░ y рдмрд░рд╛рдмрд░ рд╣реИрдВ, рддреЛ рдЛрдгрд╛рддреНрдордХ рд╕рдВрдЦреНрдпрд╛ рдпрджрд┐ x <y, рдФрд░ рд╕рдХрд╛рд░рд╛рддреНрдордХ рд╕рдВрдЦреНрдпрд╛ рд╣реИ рддреЛ x> yред рдпрд╣ рд╕реНрдерд┐рд░ рд╡рд┐рдзрд┐ рдЬрд╛рд╡рд╛ 7 рдореЗрдВ рдЬреЛрдбрд╝реА рдЧрдИ рдереАред рдЖрдкрдХреЛ x рдФрд░ y рдХреА рддреБрд▓рдирд╛ рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП x - y рдХреА рдЧрдгрдирд╛ рдХрд░рдиреЗ рдХреА рдЖрд╡рд╢реНрдпрдХрддрд╛ рдирд╣реАрдВ рд╣реИ, рдХреНрдпреЛрдВрдХрд┐ рдЧрдгрдирд╛ рд╡рд┐рдкрд░реАрдд рд╕рдВрдХреЗрдд рдХреЗ рдмрдбрд╝реЗ рдСрдкрд░реЗрдВрдб рдХреЗ рд▓рд┐рдП рдЕрддрд┐рдкреНрд░рд╡рд╛рд╣ рдХрд╛ рдХрд╛рд░рдг рдмрди рд╕рдХрддреА рд╣реИред
рдЖрд╕реНрдердЧрд┐рдд рдирд┐рд╖реНрдкрд╛рджрди рдХреЗ рдПрдХ рдЕрдиреНрдп рдЙрджрд╛рд╣рд░рдг рдХреЗ рд░реВрдк рдореЗрдВ, рдПрдХ рдмрдЯрди рдХреЗ рд▓рд┐рдП рдХреЙрд▓рдмреИрдХ рдкрд░ рд╡рд┐рдЪрд╛рд░ рдХрд░реЗрдВред рдЖрдк рдХреЙрд▓рдмреИрдХ рдХрд╛рд░реНрд░рд╡рд╛рдИ рдХреЛ рдЙрд╕ рд╡рд░реНрдЧ рдХреА рд╡рд┐рдзрд┐ рдореЗрдВ рдбрд╛рд▓рддреЗ рд╣реИрдВ рдЬреЛ рд╢реНрд░реЛрддрд╛ рдЗрдВрдЯрд░рдлрд╝реЗрд╕ рдХреЛ рд▓рд╛рдЧреВ рдХрд░рддрд╛ рд╣реИ, рдПрдХ рдЙрджрд╛рд╣рд░рдг рдмрдирд╛рддрд╛ рд╣реИ, рдФрд░ рдЙрджрд╛рд╣рд░рдг рдкрдВрдЬреАрдХреГрдд рдХрд░рддрд╛ рд╣реИред рдпрд╣ рдПрдХ рдРрд╕рд╛ рд╕рд╛рдорд╛рдиреНрдп рдкрд░рд┐рджреГрд╢реНрдп рд╣реИ рдХрд┐ рдХрдИ рдкреНрд░реЛрдЧреНрд░рд╛рдорд░ рд╡рд╛рдХреНрдпрд╡рд┐рдиреНрдпрд╛рд╕ рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░рддреЗ рд╣реИрдВ "рдПрдХ рдЕрдирд╛рдо рд╡рд░реНрдЧ рдХрд╛ рдЕрдЬреНрдЮрд╛рдд рдЙрджрд╛рд╣рд░рдг":
button.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent event) { System.out.println("The button has been clicked!"); } });
handle
рд╡рд┐рдзрд┐ рдХреЗ рдЕрдВрджрд░ рдХреЛрдб рдпрд╣рд╛рдБ рдорд╣рддреНрд╡рдкреВрд░реНрдг рд╣реИред рдЬрдм рднреА рдХреЛрдИ рдмрдЯрди рджрдмрд╛рдпрд╛ рдЬрд╛рддрд╛ рд╣реИ рддреЛ рдЗрд╕ рдХреЛрдб рдХреЛ рдирд┐рд╖реНрдкрд╛рджрд┐рдд рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред
рдЪреВрдВрдХрд┐ Java 8 рдЬрд╛рд╡рд╛рдПрдлрдПрдХреНрд╕ рдХреЛ рд╕реНрд╡рд┐рдВрдЧрд┐рдВрдЧ рдЬреАрдпреВрдЖрдИ рдЯреВрд▓рдХрд┐рдЯ рдХреЗ рдЙрддреНрддрд░рд╛рдзрд┐рдХрд╛рд░реА рдХреЗ рд░реВрдк рдореЗрдВ рд░рдЦрддрд╛ рд╣реИ, рдЗрд╕рд▓рд┐рдП рдореИрдВ рдЗрди рдЙрджрд╛рд╣рд░рдгреЛрдВ рдореЗрдВ рдЬрд╛рд╡рд╛рдПрдлрдПрдХреНрд╕ рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░рддрд╛ рд╣реВрдВред рд╡рд┐рд╡рд░рдг рдорд╛рдпрдиреЗ рдирд╣реАрдВ рд░рдЦрддрд╛ред рдкреНрд░рддреНрдпреЗрдХ рдЙрдкрдпреЛрдЧрдХрд░реНрддрд╛ рдЗрдВрдЯрд░рдлрд╝реЗрд╕ рд▓рд╛рдЗрдмреНрд░реЗрд░реА рдореЗрдВ, рдЪрд╛рд╣реЗ рд╕реНрд╡рд┐рдВрдЧ, рдЬрд╛рд╡рд╛рдПрдлрд╝рдПрдХреНрд╕ рдпрд╛ рдПрдВрдбреНрд░реЙрдЗрдб, рдЖрдк рдмрдЯрди рдХреЛ рдХреБрдЫ рдХреЛрдб рдкрд╛рд╕ рдХрд░рддреЗ рд╣реИрдВ рдЬрд┐рд╕реЗ рдЖрдк рдмрдЯрди рджрдмрд╛рдП рдЬрд╛рдиреЗ рдкрд░ рдЪрд▓рд╛рдирд╛ рдЪрд╛рд╣рддреЗ рд╣реИрдВред
рддреАрдиреЛрдВ рдЙрджрд╛рд╣рд░рдгреЛрдВ рдореЗрдВ рдЖрдкрдиреЗ рдПрдХ рд╣реА рджреГрд╖реНрдЯрд┐рдХреЛрдг рджреЗрдЦрд╛ред рдХреЛрдб рдХрд╛ рдПрдХ рдмреНрд▓реЙрдХ рдХрд┐рд╕реА рдкрд░ рдкрд╛рд░рд┐рдд рдХрд┐рдпрд╛ рдЧрдпрд╛ рдерд╛ - рдПрдХ рдереНрд░реЗрдб рдкреВрд▓, рдЫрдБрдЯрд╛рдИ рд╡рд┐рдзрд┐, рдпрд╛ рдмрдЯрдиред рдЗрд╕ рдХреЛрдб рдХреЛ рдХреБрдЫ рд╕рдордп рдмрд╛рдж рдХрд╣рд╛ рдЧрдпрд╛ рдерд╛ред
рдЕрдм рддрдХ рдЬрд╛рд╡рд╛ рдореЗрдВ рдкрд╛рд╕рд┐рдВрдЧ рдХреЛрдб рдЖрд╕рд╛рди рдирд╣реАрдВ рд░рд╣рд╛ рд╣реИред рдЖрдк рдХрд╣реАрдВ рднреА рдХреЛрдб рдмреНрд▓реЙрдХ рдкрд╛рд╕ рдирд╣реАрдВ рдХрд░ рд╕рдХрддреЗред рдЬрд╛рд╡рд╛ рдПрдХ рдСрдмреНрдЬреЗрдХреНрдЯ-рдУрд░рд┐рдПрдВрдЯреЗрдб рднрд╛рд╖рд╛ рд╣реИ, рдЗрд╕рд▓рд┐рдП рдЖрдкрдХреЛ рдПрдХ рдХреНрд▓рд╛рд╕ рд╕реЗ рд╕рдВрдмрдВрдзрд┐рдд рдСрдмреНрдЬреЗрдХреНрдЯ рдмрдирд╛рдирд╛ рд╣реЛрдЧрд╛ рдЬрд┐рд╕рдореЗрдВ рд╕рд╣реА рдХреЛрдб рдХреЗ рд╕рд╛рде рдПрдХ рд╡рд┐рдзрд┐ рд╣реЛред
рдЕрдиреНрдп рднрд╛рд╖рд╛рдУрдВ рдореЗрдВ, рдЖрдк рд╕реАрдзреЗ рдХреЛрдб рдХреЗ рдмреНрд▓реЙрдХ рдХреЗ рд╕рд╛рде рдХрд╛рдо рдХрд░ рд╕рдХрддреЗ рд╣реИрдВред рдЬрд╛рд╡рд╛ рдбрд┐рдЬрд╛рдЗрдирд░реЛрдВ рдиреЗ рд▓рдВрдмреЗ рд╕рдордп рддрдХ рдЗрд╕ рд╕реБрд╡рд┐рдзрд╛ рдХреЛ рдЬреЛрдбрд╝рдиреЗ рдХрд╛ рд╡рд┐рд░реЛрдз рдХрд┐рдпрд╛ рд╣реИред рдЖрдЦрд┐рд░рдХрд╛рд░, рдЬрд╛рд╡рд╛ рдХреА рдорд╣рд╛рди рддрд╛рдХрдд рдЗрд╕рдХреА рд╕рд╛рджрдЧреА рдФрд░ рдирд┐рд░рдВрддрд░рддрд╛ рдореЗрдВ рдирд┐рд╣рд┐рдд рд╣реИред рдпрджрд┐ рднрд╛рд╖рд╛ рдореЗрдВ рд╡реЗ рд╕рднреА рдХрд╛рд░реНрдп рд╢рд╛рдорд┐рд▓ рд╣реИрдВ рдЬреЛ рдереЛрдбрд╝реЗ рдЕрдзрд┐рдХ рд╕рдВрдХреНрд╖рд┐рдкреНрдд рдХреЛрдб рджреЗрддреЗ рд╣реИрдВ, рддреЛ рднрд╛рд╖рд╛ рдмрд╣реБрдд рдЧрдбрд╝рдмрдбрд╝ рд╣реЛ рд╕рдХрддреА рд╣реИред рд╣рд╛рд▓рд╛рдБрдХрд┐, рдЙрди рдЕрдиреНрдп рднрд╛рд╖рд╛рдУрдВ рдореЗрдВ, рдзрд╛рд░рд╛ рдкреНрд░рд╡рд╛рд╣ рдпрд╛ рдПрдХ рдХреНрд▓рд┐рдХ рдмрдЯрди рд╣реИрдВрдбрд▓рд░ рдХреЛ рдкрдВрдЬреАрдХреГрдд рдХрд░рдирд╛ рдЖрд╕рд╛рди рдирд╣реАрдВ рд╣реИ; рдЙрдирдХреЗ рдХрдИ рдПрдкреАрдЖрдИ рд╕рд░рд▓, рдЕрдзрд┐рдХ рд╕реБрд╕рдВрдЧрдд рдФрд░ рд╢рдХреНрддрд┐рд╢рд╛рд▓реА рд╣реИрдВред рдЬрд╛рд╡рд╛ рдореЗрдВ, рдПрдХ рд╕рдорд╛рди рдЗрдВрдЯрд░рдлреЗрд╕ рд▓рд┐рдЦ рд╕рдХрддрд╛ рд╣реИ рдЬреЛ рдХреНрд▓рд╛рд╕ рдСрдмреНрдЬреЗрдХреНрдЯреНрд╕ рдХреЛ рд╕реНрд╡реАрдХрд╛рд░ рдХрд░рддрд╛ рд╣реИ рдЬреЛ рдПрдХ рд╡рд┐рд╢реЗрд╖ рдлрд╝рдВрдХреНрд╢рди рдХреЛ рд▓рд╛рдЧреВ рдХрд░рддреЗ рд╣реИрдВ, рд▓реЗрдХрд┐рди рдРрд╕реЗ рдПрдкреАрдЖрдИ рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рдЕрд╕реБрд╡рд┐рдзрд╛рдЬрдирдХ рд╣реЛрдЧрд╛ред
рд╣рд╛рд▓ рд╣реА рдореЗрдВ, рд╕рд╡рд╛рд▓ рдпрд╣ рдирд╣реАрдВ рд╣реИ рдХрд┐ рдХрд╛рд░реНрдпрд╛рддреНрдордХ рдкреНрд░реЛрдЧреНрд░рд╛рдорд┐рдВрдЧ рдХреЗ рд▓рд┐рдП рдЬрд╛рд╡рд╛ рдХрд╛ рд╡рд┐рд╕реНрддрд╛рд░ рдХрд░рдирд╛ рд╣реИ рдпрд╛ рдирд╣реАрдВ, рд▓реЗрдХрд┐рди рдпрд╣ рдХреИрд╕реЗ рдХрд░рдирд╛ рд╣реИред рдпрд╣ рд╕реНрдкрд╖реНрдЯ рд╣реЛрдиреЗ рд╕реЗ рдкрд╣рд▓реЗ рдХрд┐ рдЬрд╛рд╡рд╛ рдХреЗ рд▓рд┐рдП рдпрд╣ рдЕрдЪреНрдЫреА рддрд░рд╣ рд╕реЗ рдХрд╛рдо рдХрд░рддрд╛ рд╣реИ, рдХрдИ рд╡рд░реНрд╖реЛрдВ рдХреЗ рдкреНрд░рдпреЛрдЧ рд╣реБрдПред рдЕрдЧрд▓реЗ рднрд╛рдЧ рдореЗрдВ, рдЖрдк рджреЗрдЦреЗрдВрдЧреЗ рдХрд┐ рдЖрдк рдЬрд╛рд╡рд╛ 8 рдореЗрдВ рдХреЛрдб рдХреЗ рдмреНрд▓реЙрдХ рдХреЗ рд╕рд╛рде рдХреИрд╕реЗ рдХрд╛рдо рдХрд░ рд╕рдХрддреЗ рд╣реИрдВред
рд▓реИрдореНрдмреНрдбрд╛ рдПрдХреНрд╕рдкреНрд░реЗрд╢рди рд╕рд┐рдВрдЯреИрдХреНрд╕
рдкрд┐рдЫрд▓реЗ рдЫрдБрдЯрд╛рдИ рдЙрджрд╛рд╣рд░рдг рдкрд░ рдлрд┐рд░ рд╕реЗ рд╡рд┐рдЪрд╛рд░ рдХрд░реЗрдВред рд╣рдо рдПрдХ рдХреЛрдб рдореЗрдВ рдкрд╛рд╕ рд╣реЛрддреЗ рд╣реИрдВ рдЬреЛ рдпрд╣ рдЬрд╛рдВрдЪрддрд╛ рд╣реИ рдХрд┐ рдХреМрди рд╕реА рд░реЗрдЦрд╛ рдЫреЛрдЯреА рд╣реИред рд╣рдо рд╣рд┐рд╕рд╛рдм рд▓рдЧрд╛рддреЗ рд╣реИрдВ
Integer.compare(firstStr.length(), secondStr.length())
firstStr
рдФрд░
secondStr
рдХреНрдпрд╛ рд╣реИрдВ? рд╡реЗ рджреЛрдиреЛрдВ рд▓рд╛рдЗрдиреЗрдВ рд╣реИрдВ! рдЬрд╛рд╡рд╛ рдПрдХ рджреГрдврд╝рддрд╛ рд╕реЗ рдЯрд╛рдЗрдк рдХреА рдЧрдИ рднрд╛рд╖рд╛ рд╣реИ, рдФрд░ рд╣рдореЗрдВ рдкреНрд░рдХрд╛рд░ рдирд┐рд░реНрджрд┐рд╖реНрдЯ рдХрд░рдиреЗ рд╣реЛрдВрдЧреЗ:
(String firstStr, String secondStr) -> Integer.compare(firstStr.length(),secondStr.length())
рдЖрдкрдиреЗ рдЕрднреА-рдЕрднреА рдЕрдкрдирд╛ рдкрд╣рд▓рд╛ рд▓рдВрдмреЛрджрд░ рдПрдХреНрд╕рдкреНрд░реЗрд╢рди рджреЗрдЦрд╛ рд╣реИ! рдЗрд╕ рддрд░рд╣ рдХреА рдЕрднрд┐рд╡реНрдпрдХреНрддрд┐ рдмрд╕ рдХреЛрдб рдХрд╛ рдПрдХ рдмреНрд▓реЙрдХ рд╣реИ рд╕рд╛рде рд╣реА рдХрд┐рд╕реА рднреА рдЪрд░ рдХреЗ рд╡рд┐рдирд┐рд░реНрджреЗрд╢ рдХреЗ рд╕рд╛рде рдЬрд┐рд╕реЗ рдХреЛрдб рдХреЛ рдкрд╛рд░рд┐рдд рдХрд┐рдпрд╛ рдЬрд╛рдирд╛ рдЪрд╛рд╣рд┐рдПред
рдРрд╕рд╛ рдирд╛рдо рдХреНрдпреЛрдВ? рдХрдИ рд╕рд╛рд▓ рдкрд╣рд▓реЗ, рдЬрдм рдЕрднреА рддрдХ рдХреЛрдИ рдХрдВрдкреНрдпреВрдЯрд░ рдирд╣реАрдВ рдереЗ, рддреЛ рддрд░реНрдХрд╢рд╛рд╕реНрддреНрд░реА рдЕрд▓реЛрдВрдЬреЛ рдЪрд░реНрдЪ рдФрдкрдЪрд╛рд░рд┐рдХ рд░реВрдк рд╕реЗ рдЧрдгрдирд╛ рдХреЗ рд▓рд┐рдП рдЧрдгрд┐рддреАрдп рдлрд╝рдВрдХреНрд╢рди рдХреЗ рд▓рд┐рдП рдЗрд╕рдХрд╛ рдХреНрдпрд╛ рдЕрд░реНрде рд╣реИ, рдпрд╣ рдФрдкрдЪрд╛рд░рд┐рдХ рд░реВрдк рджреЗрдирд╛ рдЪрд╛рд╣рддрд╛ рдерд╛ред (рдЙрддреНрд╕реБрдХрддрд╛ рд╕реЗ, рдРрд╕реЗ рдХрд╛рд░реНрдп рд╣реИрдВ рдЬреЛ рдЕрд╕реНрддрд┐рддреНрд╡ рдХреЗ рд▓рд┐рдП рдЬрд╛рдиреЗ рдЬрд╛рддреЗ рд╣реИрдВ, рд▓реЗрдХрд┐рди рдХреЛрдИ рднреА рдЕрдкрдиреЗ рдореВрд▓реНрдпреЛрдВ рдХреА рдЧрдгрдирд╛ рдХрд░рдирд╛ рдирд╣реАрдВ рдЬрд╛рдирддрд╛ рд╣реИред) рдЙрдиреНрд╣реЛрдВрдиреЗ рдорд╛рдкрджрдВрдбреЛрдВ рдХреЛ рдЪрд┐рд╣реНрдирд┐рдд рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рдЧреНрд░реАрдХ рдЕрдХреНрд╖рд░ рд▓реИрдореНрдмреНрдбрд╛ (╬╗) рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд┐рдпрд╛ред рдпрджрд┐ рд╡рд╣ рдЬрд╛рд╡рд╛ рдПрдкреАрдЖрдИ рдХреЗ рдмрд╛рд░реЗ рдореЗрдВ рдЬрд╛рдирддрд╛ рд╣реИ, рддреЛ рд╡рд╣ рдХреБрдЫ рдРрд╕рд╛ рдирд╣реАрдВ рд▓рд┐рдЦреЗрдЧрд╛ рдЬреЛ рдЖрдкрдиреЗ рджреЗрдЦрд╛ рдерд╛, рд╕рдмрд╕реЗ рдЕрдзрд┐рдХ рд╕рдВрднрд╛рд╡рдирд╛ рд╣реИред
рдкрддреНрд░ ╬╗ рдХреНрдпреЛрдВ рд╣реИ? рдХреНрдпрд╛ рдЪрд░реНрдЪ рдиреЗ рд╡рд░реНрдгрдорд╛рд▓рд╛ рдХреЗ рд╕рднреА рдЕрдХреНрд╖рд░реЛрдВ рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд┐рдпрд╛ рдерд╛? рд╡рд╛рд╕реНрддрд╡ рдореЗрдВ, рдкреНрд░рд┐рдВрд╕рд┐рдкрд┐рдпрд╛ рдореИрдереЗрдореЗрдЯрд┐рдХрд╛ рдХреЗ рдЖрджрд░рдгреАрдп рдХрд╛рд░реНрдп рдореЗрдВ рдкреНрд░рддреАрдХ рдХрд╛ рдЙрдкрдпреЛрдЧ ener рдореБрдХреНрдд рдЪрд░ рдХреЛ рджрд░реНрд╢рд╛рдиреЗ рдХреЗ рд▓рд┐рдП рдХрд┐рдпрд╛ рдЧрдпрд╛ рд╣реИ рдЬрд┐рд╕рдиреЗ рдЪрд░реНрдЪ рдХреЛ рдорд╛рдкрджрдВрдбреЛрдВ рдХреЗ рд▓рд┐рдП рд░рд╛рдЬрдзрд╛рдиреА рд▓реИрдВрдмрдбрд╛ (╬Ы) рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рдкреНрд░реЗрд░рд┐рдд рдХрд┐рдпрд╛ред рд▓реЗрдХрд┐рди рдЕрдВрдд рдореЗрдВ, рдЙрдиреНрд╣реЛрдВрдиреЗ рдкрддреНрд░ рдХреЗ рдирд┐рдЪрд▓реЗ рд╕рдВрд╕реНрдХрд░рдг рдореЗрдВ рд╕реНрд╡рд┐рдЪ рдХрд┐рдпрд╛ред рддрдм рд╕реЗ, рдЪрд░ рдорд╛рдкрджрдВрдбреЛрдВ рдХреЗ рд╕рд╛рде рдПрдХ рдЕрднрд┐рд╡реНрдпрдХреНрддрд┐ рдХреЛ "рд▓рдВрдмреЛрджрд░ рдЕрднрд┐рд╡реНрдпрдХреНрддрд┐" рдХрд╣рд╛ рдЬрд╛рддрд╛ рд╣реИред
рдЖрдкрдиреЗ рдЕрднреА рдЬрд╛рд╡рд╛ рдореЗрдВ рд▓реИрдореНрдмрдбрд╛ рдПрдХреНрд╕рдкреНрд░реЗрд╢рди рдХрд╛ рдПрдХ рд░реВрдк рджреЗрдЦрд╛ рд╣реИ: рдкреИрд░рд╛рдореАрдЯрд░, рдПрд░реЛ -> рдФрд░ рдПрдХреНрд╕рдкреНрд░реЗрд╢рдиред рдпрджрд┐ рдХреЛрдб рдПрдХ рдЧрдгрдирд╛ рдХрд░рддрд╛ рд╣реИ рдЬреЛ рдПрдХ рдПрдХрд▓ рдЕрднрд┐рд╡реНрдпрдХреНрддрд┐ рдореЗрдВ рдлрд┐рдЯ рдирд╣реАрдВ рд╣реЛрддрд╛ рд╣реИ, рддреЛ рдЗрд╕реЗ рдЙрд╕реА рддрд░рд╣ рд╕реЗ рд▓рд┐рдЦреЗрдВ рдЬреИрд╕реЗ рдЖрдк рдПрдХ рд╡рд┐рдзрд┐ рд▓рд┐рдЦреЗрдВрдЧреЗ: {} рдореЗрдВ рд╕рдВрд▓рдЧреНрди рдФрд░ рд╕реНрдкрд╖реНрдЯ
return
ред рдЙрджрд╛рд╣рд░рдг рдХреЗ рд▓рд┐рдП
(String firstStr, String secondStr) -> { if (firstStr.length() < secondStr.length()) return -1; else if (firstStr.length() > secondStr.length()) return 1; else return 0; }
рдпрджрд┐ рд▓реИрдореНрдмреНрдбрд╛ рдЕрднрд┐рд╡реНрдпрдХреНрддрд┐ рдореЗрдВ рдХреЛрдИ рдкреИрд░рд╛рдореАрдЯрд░ рдирд╣реАрдВ рд╣реИ, рддреЛ рдЖрдк рдЕрднреА рднреА рдЦрд╛рд▓реА рдмреНрд░реИрдХреЗрдЯ рд▓рдЧрд╛рддреЗ рд╣реИрдВ, рдареАрдХ рдЙрд╕реА рддрд░рд╣ рдЬреИрд╕реЗ рдмрд┐рдирд╛ рдорд╛рдкрджрдВрдбреЛрдВ рдХреЗ рдПрдХ рд╡рд┐рдзрд┐ рдХреЗ рд╕рд╛рде:
() -> { for (int i = 0; i < 1000; i++) doWork(); }
рдпрджрд┐ рд▓рдВрдмреЛрджрд░ рдЕрднрд┐рд╡реНрдпрдХреНрддрд┐ рдХреЗ рдкреИрд░рд╛рдореАрдЯрд░ рдкреНрд░рдХрд╛рд░ рдХрд╛ рдЕрдиреБрдорд╛рди рд▓рдЧрд╛рдпрд╛ рдЬрд╛ рд╕рдХрддрд╛ рд╣реИ, рддреЛ рдЖрдк рдЙрдиреНрд╣реЗрдВ рдЫреЛрдбрд╝ рд╕рдХрддреЗ рд╣реИрдВред рдЙрджрд╛рд╣рд░рдг рдХреЗ рд▓рд┐рдП
Comparator<String> comp = (firstStr, secondStr) // Same as (String firstStr, String secondStr) -> Integer.compare(firstStr.length(),secondStr.length());
рдпрд╣рд╛рдВ, рд╕рдВрдХрд▓рдХ рдирд┐рд╖реНрдХрд░реНрд╖ рдирд┐рдХрд╛рд▓ рд╕рдХрддрд╛ рд╣реИ рдХрд┐
firstStr
рдФрд░
secondStr
рддрд╛рд░ рд╣реЛрдирд╛ рдЪрд╛рд╣рд┐рдП, рдХреНрдпреЛрдВрдХрд┐ рд▓реИрдореНрдмреНрдбрд╛ рдЕрднрд┐рд╡реНрдпрдХреНрддрд┐ рд╕реНрдЯреНрд░рд┐рдВрдЧ рддреБрд▓рдирд┐рддреНрд░ рдХреЛ рд╕реМрдВрдкрд╛ рдЧрдпрд╛ рд╣реИред (рд╣рдо рдЗрд╕ рдЕрд╕рд╛рдЗрдирдореЗрдВрдЯ рдкрд░ рдмрд╛рдж рдореЗрдВ рд╡рд┐рдЪрд╛рд░ рдХрд░реЗрдВрдЧреЗред)
рдпрджрд┐ рд╡рд┐рдзрд┐ рдХреЗ рдкреНрд░рдХрд╛рд░ рдХрд╛ рдПрдХ рдкреИрд░рд╛рдореАрдЯрд░ рдкреНрд░рджрд░реНрд╢рд┐рдд рдХрд┐рдпрд╛ рдЬрд╛ рд░рд╣рд╛ рд╣реИ, рддреЛ рдЖрдк рдХреЛрд╖реНрдардХ рдХреЛ рдЫреЛрдбрд╝ рднреА рд╕рдХрддреЗ рд╣реИрдВ:
EventHandler<ActionEvent> listener = event -> System.out.println("The button has been clicked!"); // Instead of (event) -> or (ActionEvent event) ->
рдЖрдк рд▓реИрдореНрдмрдбрд╛ рдкреИрд░рд╛рдореАрдЯрд░ рдореЗрдВ рдПрдиреЛрдЯреЗрд╢рди рдпрд╛
final
рд╕рдВрд╢реЛрдзрдХ рдХреЛ рдЙрд╕реА рддрд░рд╣ рд╕реЗ рдЬреЛрдбрд╝ рд╕рдХрддреЗ рд╣реИрдВ рдЬреИрд╕реЗ рдХрд┐ рд╡рд┐рдзрд┐ рдкреИрд░рд╛рдореАрдЯрд░ рдХреЗ рд▓рд┐рдП:
(final String var) -> ... (@NonNull String var) -> ...
рдЖрдк рд▓реИрдВрдмрдбрд╛ рдПрдХреНрд╕рдкреНрд░реЗрд╢рди рдХреЗ рдкрд░рд┐рдгрд╛рдо рдкреНрд░рдХрд╛рд░ рдХреЛ рдХрднреА рдирд┐рд░реНрджрд┐рд╖реНрдЯ рдирд╣реАрдВ рдХрд░рддреЗ рд╣реИрдВред рдпрд╣ рд╣рдореЗрд╢рд╛ рд╕рдВрджрд░реНрдн рд╕реЗ рд╕реНрдкрд╖реНрдЯ рд╣реИред рдЙрджрд╛рд╣рд░рдг рдХреЗ рд▓рд┐рдП, рдЕрднрд┐рд╡реНрдпрдХреНрддрд┐
(String firstStr, String secondStr) -> Integer.compare(firstStr.length(), secondStr.length())
рдПрдХ рд╕рдВрджрд░реНрдн рдореЗрдВ рдкреНрд░рдпреЛрдЧ рдХрд┐рдпрд╛ рдЬрд╛ рд╕рдХрддрд╛ рд╣реИ рдЬрд╣рд╛рдВ рдПрдХ
int
рдкреНрд░рдХрд╛рд░ рдХреЗ рдкрд░рд┐рдгрд╛рдо рдХреА рдЙрдореНрдореАрдж рд╣реИред
рдзреНрдпрд╛рди рджреЗрдВ рдХрд┐ рдПрдХ рд▓реИрдореНрдмреНрдбрд╛ рдЕрднрд┐рд╡реНрдпрдХреНрддрд┐ рдХреБрдЫ рд╢рд╛рдЦрд╛рдУрдВ рдореЗрдВ рдПрдХ рдореВрд▓реНрдп рдирд╣реАрдВ рд▓реМрдЯрд╛ рд╕рдХрддрд╛ рд╣реИ, рдФрд░ рджреВрд╕рд░реЛрдВ рдореЗрдВ рд╡рд╛рдкрд╕ рдирд╣реАрдВ рдХрд░ рд╕рдХрддрд╛ рд╣реИред рдЙрджрд╛рд╣рд░рдг рдХреЗ рд▓рд┐рдП,
(int x) -> { if (x <= 1) return -1; }
(int x) -> { if (x <= 1) return -1; }
рдЕрдорд╛рдиреНрдп рд╣реИред
рдХрд╛рд░реНрдпрд╛рддреНрдордХ рдЗрдВрдЯрд░рдлреЗрд╕
рдЬреИрд╕рд╛ рдХрд┐ рд╣рдордиреЗ рдЪрд░реНрдЪрд╛ рдХреА, рдЬрд╛рд╡рд╛ рдореЗрдВ рдХрдИ рдореМрдЬреВрджрд╛ рдЗрдВрдЯрд░рдлреЗрд╕ рд╣реИрдВ рдЬреЛ рдХреЛрдб рдХреЗ рдмреНрд▓реЙрдХ рдХреЛ
Runnable
, рдЬреИрд╕реЗ рдХрд┐
Runnable
рдпрд╛
Comparator
ред рдЗрди рдЗрдВрдЯрд░рдлреЗрд╕ рдХреЗ рд╕рд╛рде рд▓реИрдореНрдмреНрдбрд╛ рдПрдХреНрд╕рдкреНрд░реЗрд╢рди рдкрд┐рдЫрдбрд╝реЗ рд╣реБрдП рд╣реИрдВред
рдЬрдм рднреА рдПрдХ рд╕рд╛рд░ рд╡рд┐рдзрд┐ рдХреЗ рд╕рд╛рде рдПрдХ рдЗрдВрдЯрд░рдлрд╝реЗрд╕ рдСрдмреНрдЬреЗрдХреНрдЯ рдХреА рдЙрдореНрдореАрдж рдХреА рдЬрд╛рддреА рд╣реИ, рддреЛ рдЖрдк рдПрдХ рд▓реИрдореНрдмреНрдбрд╛ рдЕрднрд┐рд╡реНрдпрдХреНрддрд┐ рдХреА рдЖрдкреВрд░реНрддрд┐ рдХрд░ рд╕рдХрддреЗ рд╣реИрдВред рдЗрд╕ рддрд░рд╣ рдХреЗ рдПрдХ рдЗрдВрдЯрд░рдлрд╝реЗрд╕ рдХреЛ рдПрдХ рдХрд╛рд░реНрдпрд╛рддреНрдордХ рдЗрдВрдЯрд░рдлрд╝реЗрд╕ рдХрд╣рд╛ рдЬрд╛рддрд╛ рд╣реИред
рдЖрдк рдЖрд╢реНрдЪрд░реНрдпрдЪрдХрд┐рдд рд╣реЛ рд╕рдХрддреЗ рд╣реИрдВ рдХрд┐ рдПрдХ рдХрд╛рд░реНрдпрд╛рддреНрдордХ рдЗрдВрдЯрд░рдлрд╝реЗрд╕ рдореЗрдВ рдПрдХ рдПрдХрд▓ рд╕рд╛рд░ рдкрджреНрдзрддрд┐ рдХреНрдпреЛрдВ рд╣реЛрдиреА рдЪрд╛рд╣рд┐рдПред рдЗрдВрдЯрд░рдлрд╝реЗрд╕ рд╕рд╛рд░ рдореЗрдВ рд╕рднреА рддрд░реАрдХреЗ рдирд╣реАрдВ рд╣реИрдВ? рд╡рд╛рд╕реНрддрд╡ рдореЗрдВ, рдЗрдВрдЯрд░рдлрд╝реЗрд╕ рдХреЗ рд▓рд┐рдП
Object
рдХреНрд▓рд╛рд╕ рдХреЗ рддрд░реАрдХреЛрдВ рдХреЛ рдУрд╡рд░рд░рд╛рдЗрдб рдХрд░рдирд╛ рд╣рдореЗрд╢рд╛ рд╕рдВрднрд╡ рдерд╛, рдЬреИрд╕реЗ рдХрд┐
toString
рдпрд╛
clone
, рдФрд░ рдпреЗ рдШреЛрд╖рдгрд╛рдПрдВ рддрд░реАрдХреЛрдВ рдХреЛ рд╕рд╛рд░ рдирд╣реАрдВ рдмрдирд╛рддреА рд╣реИрдВред (рдЬрд╛рд╡рд╛ рдПрдкреАрдЖрдИ рдореЗрдВ рдХреБрдЫ рдЗрдВрдЯрд░рдлреЗрд╕ javadoc рдЯрд┐рдкреНрдкрдгрд┐рдпреЛрдВ рдХреЛ рд╕рдВрд▓рдЧреНрди рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП
Object
рд╡рд┐рдзрд┐рдпреЛрдВ рдХреЛ рдУрд╡рд░рд░рд╛рдЗрдб рдХрд░рддреЗ рд╣реИрдВред рдПрдХ рдЙрджрд╛рд╣рд░рдг рдХреЗ рд▓рд┐рдП рддреБрд▓рдирд┐рддреНрд░ рдПрдкреАрдЖрдИ рджреЗрдЦреЗрдВред) рдЕрдзрд┐рдХ рдорд╣рддреНрд╡рдкреВрд░реНрдг рдмрд╛рдд, рдЬреИрд╕рд╛ рдХрд┐ рдЖрдк рдЬрд▓реНрдж рд╣реА рджреЗрдЦреЗрдВрдЧреЗ рдЬрд╛рд╡рд╛ 8 рдореЗрдВ, рдЗрдВрдЯрд░рдлреЗрд╕ рдЧреИрд░-рдЕрдореВрд░реНрдд рд╡рд┐рдзрд┐рдпреЛрдВ рдХреА рдШреЛрд╖рдгрд╛ рдХрд░ рд╕рдХрддреЗ рд╣реИрдВред
рдПрдХ рдХрд╛рд░реНрдпрд╛рддреНрдордХ рдЗрдВрдЯрд░рдлрд╝реЗрд╕ рдореЗрдВ рд░реВрдкрд╛рдВрддрд░рдг рдкреНрд░рджрд░реНрд╢рд┐рдд рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП,
Arrays.sort
рд╡рд┐рдзрд┐ рдкрд░ рд╡рд┐рдЪрд╛рд░ рдХрд░реЗрдВред рдЗрд╕рдХреЗ рджреВрд╕рд░реЗ рдкреИрд░рд╛рдореАрдЯрд░ рдореЗрдВ рдПрдХ рдПрдХрд▓ рд╡рд┐рдзрд┐ рдХреЗ рд╕рд╛рде
Comparator
рдЗрдВрдЯрд░рдлрд╝реЗрд╕ рдХреА рдПрдХ рдЙрджрд╛рд╣рд░рдг рдХреА рдЖрд╡рд╢реНрдпрдХрддрд╛ рд╣реЛрддреА рд╣реИред рдмрд╕ рд▓рдВрдмреЛрджрд░ рдкреНрд░рджрд╛рди рдХрд░реЗрдВ:
Arrays.sort(strs, (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length()));
рдкрд░реНрджреЗ рдХреЗ рдкреАрдЫреЗ,
Arrays.sort
рдкрджреНрдзрддрд┐ рдХреЛ рдХреБрдЫ рд╡рд░реНрдЧ рдХреА рдПрдХ рд╡рд╕реНрддреБ рдорд┐рд▓рддреА рд╣реИ рдЬреЛ рдХрд┐
Comparator . compare -. , - , . - , , , .
тАУ , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int
, , . Java - Object
, Object
. Java , .
Java API java.util.function. , BiFunction <T, U, R>
, U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort
, BiFunction
. , . Java . , Comparator
, , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface
. . , . Javadoc , . . , , . @FunctionalInterface
- .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("тАж"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run
, . , . -. , . , call
Callable
. , Callable ( return null
).
, , . , , event
, . ,
button.setOnAction(event -> System.out.println(event));
, println
setOnAction
. :
button.setOnAction(System.out::println);
System.out::println
, - x -> System.out.println(x)
.
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println
x -> System.out.println(x)
. , Math::pow
(x, y) -> Math.pow(x, y)
. . , String::compareToIgnoreCase
- , (x, y) -> x.compareToIgnoreCase(y)
.
, . , Math.max
, int
double
. , , Math.max
. , -, . .
this
. , this::equals
тАУ , x -> this.equals(x)
. super
. super::instanceMethod
this
. , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable
, super::speak
, speak
. ( , , EnclosingClass.this::method
EnclosingClass.super::method
.)
, , , new
. , Button::new
Button
. ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream
, map
collect
. , , map
Button(String)
. Button
, , , , .
. , int[]::new
: . - x -> new int[x]
.
Java. T. new T[n]
, new Object[n]
. . , , . Stream
toArray
, Object
:
Object[] buttons = stream.toArray();
. , . . Button[]::new
toArray
:
Button[] buttons = stream.toArray(Button[]::new);
toArray
. .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count
text
-. , -. , repeatText
.
, , . - repeatText
. text
count
?
, , -. - :
; , ,
- , text
count
. , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++
, , , .
. Java 8 final
. -. final
; , , .
, . . matchCount
тАУ , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs
final
. ( final
, .) matchedObjs
ArrayList
. , . add
, .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this
- this
, . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString()
toString
Application
, Runnable
. this
-. - doWork
, this
.
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach
, .
strList.forEach(System.out::println);
, . Java , . Collection
, forEach
, , , Collection
, , . Java.
Java , ( ). . . Java 8 forEach
Iterable
, Collection
, , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId
, , getFirstName
. , Person
, , , getId
, , getFirstName
.
, , , Collection/AbstractCollection
WindowListener/WindowAdapter
. .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName
:
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName
, Person
Naming
. , Java . getFirstName
Student
. , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming
getFirstName
:
interface Naming { String getFirstName(); }
Student
Person
? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person
Student
:
class Student extends Person implements Naming { ... }
, . Student
getFirstName
Person
, , Naming
getFirstName
. ┬л ┬╗ . ┬л ┬╗ Java 7. , , , . : , Object
. , toString
equals
, , List
. , Object.toString
Object.equals
.
- , - Java, - , . ? Java , . , , , . , , Java 8.
рд▓рд╛рдЧреВ рдХрд░рддреА рд╣реИ
Comparator . compare -. , - , . - , , , .
тАУ , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int
, , . Java - Object
, Object
. Java , .
Java API java.util.function. , BiFunction <T, U, R>
, U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort
, BiFunction
. , . Java . , Comparator
, , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface
. . , . Javadoc , . . , , . @FunctionalInterface
- .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("тАж"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run
, . , . -. , . , call
Callable
. , Callable ( return null
).
, , . , , event
, . ,
button.setOnAction(event -> System.out.println(event));
, println
setOnAction
. :
button.setOnAction(System.out::println);
System.out::println
, - x -> System.out.println(x)
.
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println
x -> System.out.println(x)
. , Math::pow
(x, y) -> Math.pow(x, y)
. . , String::compareToIgnoreCase
- , (x, y) -> x.compareToIgnoreCase(y)
.
, . , Math.max
, int
double
. , , Math.max
. , -, . .
this
. , this::equals
тАУ , x -> this.equals(x)
. super
. super::instanceMethod
this
. , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable
, super::speak
, speak
. ( , , EnclosingClass.this::method
EnclosingClass.super::method
.)
, , , new
. , Button::new
Button
. ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream
, map
collect
. , , map
Button(String)
. Button
, , , , .
. , int[]::new
: . - x -> new int[x]
.
Java. T. new T[n]
, new Object[n]
. . , , . Stream
toArray
, Object
:
Object[] buttons = stream.toArray();
. , . . Button[]::new
toArray
:
Button[] buttons = stream.toArray(Button[]::new);
toArray
. .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count
text
-. , -. , repeatText
.
, , . - repeatText
. text
count
?
, , -. - :
; , ,
- , text
count
. , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++
, , , .
. Java 8 final
. -. final
; , , .
, . . matchCount
тАУ , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs
final
. ( final
, .) matchedObjs
ArrayList
. , . add
, .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this
- this
, . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString()
toString
Application
, Runnable
. this
-. - doWork
, this
.
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach
, .
strList.forEach(System.out::println);
, . Java , . Collection
, forEach
, , , Collection
, , . Java.
Java , ( ). . . Java 8 forEach
Iterable
, Collection
, , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId
, , getFirstName
. , Person
, , , getId
, , getFirstName
.
, , , Collection/AbstractCollection
WindowListener/WindowAdapter
. .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName
:
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName
, Person
Naming
. , Java . getFirstName
Student
. , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming
getFirstName
:
interface Naming { String getFirstName(); }
Student
Person
? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person
Student
:
class Student extends Person implements Naming { ... }
, . Student
getFirstName
Person
, , Naming
getFirstName
. ┬л ┬╗ . ┬л ┬╗ Java 7. , , , . : , Object
. , toString
equals
, , List
. , Object.toString
Object.equals
.
- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare
-. , - , . - , , , .
тАУ , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int
, , . Java - Object
, Object
. Java , .
Java API java.util.function. , BiFunction <T, U, R>
, U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort
, BiFunction
. , . Java . , Comparator
, , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface
. . , . Javadoc , . . , , . @FunctionalInterface
- .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("тАж"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run
, . , . -. , . , call
Callable
. , Callable ( return null
).
, , . , , event
, . ,
button.setOnAction(event -> System.out.println(event));
, println
setOnAction
. :
button.setOnAction(System.out::println);
System.out::println
, - x -> System.out.println(x)
.
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println
x -> System.out.println(x)
. , Math::pow
(x, y) -> Math.pow(x, y)
. . , String::compareToIgnoreCase
- , (x, y) -> x.compareToIgnoreCase(y)
.
, . , Math.max
, int
double
. , , Math.max
. , -, . .
this
. , this::equals
тАУ , x -> this.equals(x)
. super
. super::instanceMethod
this
. , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable
, super::speak
, speak
. ( , , EnclosingClass.this::method
EnclosingClass.super::method
.)
, , , new
. , Button::new
Button
. ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream
, map
collect
. , , map
Button(String)
. Button
, , , , .
. , int[]::new
: . - x -> new int[x]
.
Java. T. new T[n]
, new Object[n]
. . , , . Stream
toArray
, Object
:
Object[] buttons = stream.toArray();
. , . . Button[]::new
toArray
:
Button[] buttons = stream.toArray(Button[]::new);
toArray
. .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count
text
-. , -. , repeatText
.
, , . - repeatText
. text
count
?
, , -. - :
; , ,
- , text
count
. , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++
, , , .
. Java 8 final
. -. final
; , , .
, . . matchCount
тАУ , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs
final
. ( final
, .) matchedObjs
ArrayList
. , . add
, .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this
- this
, . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString()
toString
Application
, Runnable
. this
-. - doWork
, this
.
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach
, .
strList.forEach(System.out::println);
, . Java , . Collection
, forEach
, , , Collection
, , . Java.
Java , ( ). . . Java 8 forEach
Iterable
, Collection
, , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId
, , getFirstName
. , Person
, , , getId
, , getFirstName
.
, , , Collection/AbstractCollection
WindowListener/WindowAdapter
. .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName
:
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName
, Person
Naming
. , Java . getFirstName
Student
. , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming
getFirstName
:
interface Naming { String getFirstName(); }
Student
Person
? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person
Student
:
class Student extends Person implements Naming { ... }
, . Student
getFirstName
Person
, , Naming
getFirstName
. ┬л ┬╗ . ┬л ┬╗ Java 7. , , , . : , Object
. , toString
equals
, , List
. , Object.toString
Object.equals
.
- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare
-. , - , . - , , , .
тАУ , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int
, , . Java - Object
, Object
. Java , .
Java API java.util.function. , BiFunction <T, U, R>
, U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort
, BiFunction
. , . Java . , Comparator
, , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface
. . , . Javadoc , . . , , . @FunctionalInterface
- .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("тАж"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run
, . , . -. , . , call
Callable
. , Callable ( return null
).
, , . , , event
, . ,
button.setOnAction(event -> System.out.println(event));
, println
setOnAction
. :
button.setOnAction(System.out::println);
System.out::println
, - x -> System.out.println(x)
.
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println
x -> System.out.println(x)
. , Math::pow
(x, y) -> Math.pow(x, y)
. . , String::compareToIgnoreCase
- , (x, y) -> x.compareToIgnoreCase(y)
.
, . , Math.max
, int
double
. , , Math.max
. , -, . .
this
. , this::equals
тАУ , x -> this.equals(x)
. super
. super::instanceMethod
this
. , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable
, super::speak
, speak
. ( , , EnclosingClass.this::method
EnclosingClass.super::method
.)
, , , new
. , Button::new
Button
. ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream
, map
collect
. , , map
Button(String)
. Button
, , , , .
. , int[]::new
: . - x -> new int[x]
.
Java. T. new T[n]
, new Object[n]
. . , , . Stream
toArray
, Object
:
Object[] buttons = stream.toArray();
. , . . Button[]::new
toArray
:
Button[] buttons = stream.toArray(Button[]::new);
toArray
. .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count
text
-. , -. , repeatText
.
, , . - repeatText
. text
count
?
, , -. - :
; , ,
- , text
count
. , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++
, , , .
. Java 8 final
. -. final
; , , .
, . . matchCount
тАУ , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs
final
. ( final
, .) matchedObjs
ArrayList
. , . add
, .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this
- this
, . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString()
toString
Application
, Runnable
. this
-. - doWork
, this
.
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach
, .
strList.forEach(System.out::println);
, . Java , . Collection
, forEach
, , , Collection
, , . Java.
Java , ( ). . . Java 8 forEach
Iterable
, Collection
, , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId
, , getFirstName
. , Person
, , , getId
, , getFirstName
.
, , , Collection/AbstractCollection
WindowListener/WindowAdapter
. .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName
:
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName
, Person
Naming
. , Java . getFirstName
Student
. , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming
getFirstName
:
interface Naming { String getFirstName(); }
Student
Person
? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person
Student
:
class Student extends Person implements Naming { ... }
, . Student
getFirstName
Person
, , Naming
getFirstName
. ┬л ┬╗ . ┬л ┬╗ Java 7. , , , . : , Object
. , toString
equals
, , List
. , Object.toString
Object.equals
.
- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare
-. , - , . - , , , .
тАУ , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int
, , . Java - Object
, Object
. Java , .
Java API java.util.function. , BiFunction <T, U, R>
, U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort
, BiFunction
. , . Java . , Comparator
, , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface
. . , . Javadoc , . . , , . @FunctionalInterface
- .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("тАж"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run
, . , . -. , . , call
Callable
. , Callable ( return null
).
, , . , , event
, . ,
button.setOnAction(event -> System.out.println(event));
, println
setOnAction
. :
button.setOnAction(System.out::println);
System.out::println
, - x -> System.out.println(x)
.
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println
x -> System.out.println(x)
. , Math::pow
(x, y) -> Math.pow(x, y)
. . , String::compareToIgnoreCase
- , (x, y) -> x.compareToIgnoreCase(y)
.
, . , Math.max
, int
double
. , , Math.max
. , -, . .
this
. , this::equals
тАУ , x -> this.equals(x)
. super
. super::instanceMethod
this
. , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable
, super::speak
, speak
. ( , , EnclosingClass.this::method
EnclosingClass.super::method
.)
, , , new
. , Button::new
Button
. ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream
, map
collect
. , , map
Button(String)
. Button
, , , , .
. , int[]::new
: . - x -> new int[x]
.
Java. T. new T[n]
, new Object[n]
. . , , . Stream
toArray
, Object
:
Object[] buttons = stream.toArray();
. , . . Button[]::new
toArray
:
Button[] buttons = stream.toArray(Button[]::new);
toArray
. .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count
text
-. , -. , repeatText
.
, , . - repeatText
. text
count
?
, , -. - :
; , ,
- , text
count
. , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++
, , , .
. Java 8 final
. -. final
; , , .
, . . matchCount
тАУ , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs
final
. ( final
, .) matchedObjs
ArrayList
. , . add
, .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this
- this
, . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString()
toString
Application
, Runnable
. this
-. - doWork
, this
.
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach
, .
strList.forEach(System.out::println);
, . Java , . Collection
, forEach
, , , Collection
, , . Java.
Java , ( ). . . Java 8 forEach
Iterable
, Collection
, , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId
, , getFirstName
. , Person
, , , getId
, , getFirstName
.
, , , Collection/AbstractCollection
WindowListener/WindowAdapter
. .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName
:
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName
, Person
Naming
. , Java . getFirstName
Student
. , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming
getFirstName
:
interface Naming { String getFirstName(); }
Student
Person
? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person
Student
:
class Student extends Person implements Naming { ... }
, . Student
getFirstName
Person
, , Naming
getFirstName
. ┬л ┬╗ . ┬л ┬╗ Java 7. , , , . : , Object
. , toString
equals
, , List
. , Object.toString
Object.equals
.
- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare
-. , - , . - , , , .
тАУ , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int
, , . Java - Object
, Object
. Java , .
Java API java.util.function. , BiFunction <T, U, R>
, U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort
, BiFunction
. , . Java . , Comparator
, , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface
. . , . Javadoc , . . , , . @FunctionalInterface
- .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("тАж"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run
, . , . -. , . , call
Callable
. , Callable ( return null
).
, , . , , event
, . ,
button.setOnAction(event -> System.out.println(event));
, println
setOnAction
. :
button.setOnAction(System.out::println);
System.out::println
, - x -> System.out.println(x)
.
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println
x -> System.out.println(x)
. , Math::pow
(x, y) -> Math.pow(x, y)
. . , String::compareToIgnoreCase
- , (x, y) -> x.compareToIgnoreCase(y)
.
, . , Math.max
, int
double
. , , Math.max
. , -, . .
this
. , this::equals
тАУ , x -> this.equals(x)
. super
. super::instanceMethod
this
. , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable
, super::speak
, speak
. ( , , EnclosingClass.this::method
EnclosingClass.super::method
.)
, , , new
. , Button::new
Button
. ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream
, map
collect
. , , map
Button(String)
. Button
, , , , .
. , int[]::new
: . - x -> new int[x]
.
Java. T. new T[n]
, new Object[n]
. . , , . Stream
toArray
, Object
:
Object[] buttons = stream.toArray();
. , . . Button[]::new
toArray
:
Button[] buttons = stream.toArray(Button[]::new);
toArray
. .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count
text
-. , -. , repeatText
.
, , . - repeatText
. text
count
?
, , -. - :
; , ,
- , text
count
. , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++
, , , .
. Java 8 final
. -. final
; , , .
, . . matchCount
тАУ , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs
final
. ( final
, .) matchedObjs
ArrayList
. , . add
, .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this
- this
, . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString()
toString
Application
, Runnable
. this
-. - doWork
, this
.
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach
, .
strList.forEach(System.out::println);
, . Java , . Collection
, forEach
, , , Collection
, , . Java.
Java , ( ). . . Java 8 forEach
Iterable
, Collection
, , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId
, , getFirstName
. , Person
, , , getId
, , getFirstName
.
, , , Collection/AbstractCollection
WindowListener/WindowAdapter
. .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName
:
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName
, Person
Naming
. , Java . getFirstName
Student
. , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming
getFirstName
:
interface Naming { String getFirstName(); }
Student
Person
? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person
Student
:
class Student extends Person implements Naming { ... }
, . Student
getFirstName
Person
, , Naming
getFirstName
. ┬л ┬╗ . ┬л ┬╗ Java 7. , , , . : , Object
. , toString
equals
, , List
. , Object.toString
Object.equals
.
- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare
-. , - , . - , , , .
тАУ , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int
, , . Java - Object
, Object
. Java , .
Java API java.util.function. , BiFunction <T, U, R>
, U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort
, BiFunction
. , . Java . , Comparator
, , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface
. . , . Javadoc , . . , , . @FunctionalInterface
- .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("тАж"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run
, . , . -. , . , call
Callable
. , Callable ( return null
).
, , . , , event
, . ,
button.setOnAction(event -> System.out.println(event));
, println
setOnAction
. :
button.setOnAction(System.out::println);
System.out::println
, - x -> System.out.println(x)
.
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println
x -> System.out.println(x)
. , Math::pow
(x, y) -> Math.pow(x, y)
. . , String::compareToIgnoreCase
- , (x, y) -> x.compareToIgnoreCase(y)
.
, . , Math.max
, int
double
. , , Math.max
. , -, . .
this
. , this::equals
тАУ , x -> this.equals(x)
. super
. super::instanceMethod
this
. , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable
, super::speak
, speak
. ( , , EnclosingClass.this::method
EnclosingClass.super::method
.)
, , , new
. , Button::new
Button
. ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream
, map
collect
. , , map
Button(String)
. Button
, , , , .
. , int[]::new
: . - x -> new int[x]
.
Java. T. new T[n]
, new Object[n]
. . , , . Stream
toArray
, Object
:
Object[] buttons = stream.toArray();
. , . . Button[]::new
toArray
:
Button[] buttons = stream.toArray(Button[]::new);
toArray
. .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count
text
-. , -. , repeatText
.
, , . - repeatText
. text
count
?
, , -. - :
; , ,
- , text
count
. , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++
, , , .
. Java 8 final
. -. final
; , , .
, . . matchCount
тАУ , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs
final
. ( final
, .) matchedObjs
ArrayList
. , . add
, .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this
- this
, . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString()
toString
Application
, Runnable
. this
-. - doWork
, this
.
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach
, .
strList.forEach(System.out::println);
, . Java , . Collection
, forEach
, , , Collection
, , . Java.
Java , ( ). . . Java 8 forEach
Iterable
, Collection
, , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId
, , getFirstName
. , Person
, , , getId
, , getFirstName
.
, , , Collection/AbstractCollection
WindowListener/WindowAdapter
. .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName
:
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName
, Person
Naming
. , Java . getFirstName
Student
. , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming
getFirstName
:
interface Naming { String getFirstName(); }
Student
Person
? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person
Student
:
class Student extends Person implements Naming { ... }
, . Student
getFirstName
Person
, , Naming
getFirstName
. ┬л ┬╗ . ┬л ┬╗ Java 7. , , , . : , Object
. , toString
equals
, , List
. , Object.toString
Object.equals
.
- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare
-. , - , . - , , , .
тАУ , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int
, , . Java - Object
, Object
. Java , .
Java API java.util.function. , BiFunction <T, U, R>
, U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort
, BiFunction
. , . Java . , Comparator
, , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface
. . , . Javadoc , . . , , . @FunctionalInterface
- .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("тАж"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run
, . , . -. , . , call
Callable
. , Callable ( return null
).
, , . , , event
, . ,
button.setOnAction(event -> System.out.println(event));
, println
setOnAction
. :
button.setOnAction(System.out::println);
System.out::println
, - x -> System.out.println(x)
.
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println
x -> System.out.println(x)
. , Math::pow
(x, y) -> Math.pow(x, y)
. . , String::compareToIgnoreCase
- , (x, y) -> x.compareToIgnoreCase(y)
.
, . , Math.max
, int
double
. , , Math.max
. , -, . .
this
. , this::equals
тАУ , x -> this.equals(x)
. super
. super::instanceMethod
this
. , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable
, super::speak
, speak
. ( , , EnclosingClass.this::method
EnclosingClass.super::method
.)
, , , new
. , Button::new
Button
. ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream
, map
collect
. , , map
Button(String)
. Button
, , , , .
. , int[]::new
: . - x -> new int[x]
.
Java. T. new T[n]
, new Object[n]
. . , , . Stream
toArray
, Object
:
Object[] buttons = stream.toArray();
. , . . Button[]::new
toArray
:
Button[] buttons = stream.toArray(Button[]::new);
toArray
. .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count
text
-. , -. , repeatText
.
, , . - repeatText
. text
count
?
, , -. - :
; , ,
- , text
count
. , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++
, , , .
. Java 8 final
. -. final
; , , .
, . . matchCount
тАУ , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs
final
. ( final
, .) matchedObjs
ArrayList
. , . add
, .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this
- this
, . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString()
toString
Application
, Runnable
. this
-. - doWork
, this
.
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach
, .
strList.forEach(System.out::println);
, . Java , . Collection
, forEach
, , , Collection
, , . Java.
Java , ( ). . . Java 8 forEach
Iterable
, Collection
, , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId
, , getFirstName
. , Person
, , , getId
, , getFirstName
.
, , , Collection/AbstractCollection
WindowListener/WindowAdapter
. .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName
:
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName
, Person
Naming
. , Java . getFirstName
Student
. , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming
getFirstName
:
interface Naming { String getFirstName(); }
Student
Person
? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person
Student
:
class Student extends Person implements Naming { ... }
, . Student
getFirstName
Person
, , Naming
getFirstName
. ┬л ┬╗ . ┬л ┬╗ Java 7. , , , . : , Object
. , toString
equals
, , List
. , Object.toString
Object.equals
.
- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare
-. , - , . - , , , .
тАУ , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int
, , . Java - Object
, Object
. Java , .
Java API java.util.function. , BiFunction <T, U, R>
, U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort
, BiFunction
. , . Java . , Comparator
, , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface
. . , . Javadoc , . . , , . @FunctionalInterface
- .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("тАж"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run
, . , . -. , . , call
Callable
. , Callable ( return null
).
, , . , , event
, . ,
button.setOnAction(event -> System.out.println(event));
, println
setOnAction
. :
button.setOnAction(System.out::println);
System.out::println
, - x -> System.out.println(x)
.
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println
x -> System.out.println(x)
. , Math::pow
(x, y) -> Math.pow(x, y)
. . , String::compareToIgnoreCase
- , (x, y) -> x.compareToIgnoreCase(y)
.
, . , Math.max
, int
double
. , , Math.max
. , -, . .
this
. , this::equals
тАУ , x -> this.equals(x)
. super
. super::instanceMethod
this
. , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable
, super::speak
, speak
. ( , , EnclosingClass.this::method
EnclosingClass.super::method
.)
, , , new
. , Button::new
Button
. ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream
, map
collect
. , , map
Button(String)
. Button
, , , , .
. , int[]::new
: . - x -> new int[x]
.
Java. T. new T[n]
, new Object[n]
. . , , . Stream
toArray
, Object
:
Object[] buttons = stream.toArray();
. , . . Button[]::new
toArray
:
Button[] buttons = stream.toArray(Button[]::new);
toArray
. .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count
text
-. , -. , repeatText
.
, , . - repeatText
. text
count
?
, , -. - :
; , ,
- , text
count
. , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++
, , , .
. Java 8 final
. -. final
; , , .
, . . matchCount
тАУ , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs
final
. ( final
, .) matchedObjs
ArrayList
. , . add
, .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this
- this
, . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString()
toString
Application
, Runnable
. this
-. - doWork
, this
.
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach
, .
strList.forEach(System.out::println);
, . Java , . Collection
, forEach
, , , Collection
, , . Java.
Java , ( ). . . Java 8 forEach
Iterable
, Collection
, , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId
, , getFirstName
. , Person
, , , getId
, , getFirstName
.
, , , Collection/AbstractCollection
WindowListener/WindowAdapter
. .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName
:
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName
, Person
Naming
. , Java . getFirstName
Student
. , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming
getFirstName
:
interface Naming { String getFirstName(); }
Student
Person
? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person
Student
:
class Student extends Person implements Naming { ... }
, . Student
getFirstName
Person
, , Naming
getFirstName
. ┬л ┬╗ . ┬л ┬╗ Java 7. , , , . : , Object
. , toString
equals
, , List
. , Object.toString
Object.equals
.
- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare
-. , - , . - , , , .
тАУ , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int
, , . Java - Object
, Object
. Java , .
Java API java.util.function. , BiFunction <T, U, R>
, U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort
, BiFunction
. , . Java . , Comparator
, , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface
. . , . Javadoc , . . , , . @FunctionalInterface
- .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("тАж"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run
, . , . -. , . , call
Callable
. , Callable ( return null
).
, , . , , event
, . ,
button.setOnAction(event -> System.out.println(event));
, println
setOnAction
. :
button.setOnAction(System.out::println);
System.out::println
, - x -> System.out.println(x)
.
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println
x -> System.out.println(x)
. , Math::pow
(x, y) -> Math.pow(x, y)
. . , String::compareToIgnoreCase
- , (x, y) -> x.compareToIgnoreCase(y)
.
, . , Math.max
, int
double
. , , Math.max
. , -, . .
this
. , this::equals
тАУ , x -> this.equals(x)
. super
. super::instanceMethod
this
. , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable
, super::speak
, speak
. ( , , EnclosingClass.this::method
EnclosingClass.super::method
.)
, , , new
. , Button::new
Button
. ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream
, map
collect
. , , map
Button(String)
. Button
, , , , .
. , int[]::new
: . - x -> new int[x]
.
Java. T. new T[n]
, new Object[n]
. . , , . Stream
toArray
, Object
:
Object[] buttons = stream.toArray();
. , . . Button[]::new
toArray
:
Button[] buttons = stream.toArray(Button[]::new);
toArray
. .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count
text
-. , -. , repeatText
.
, , . - repeatText
. text
count
?
, , -. - :
; , ,
- , text
count
. , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++
, , , .
. Java 8 final
. -. final
; , , .
, . . matchCount
тАУ , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs
final
. ( final
, .) matchedObjs
ArrayList
. , . add
, .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this
- this
, . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString()
toString
Application
, Runnable
. this
-. - doWork
, this
.
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach
, .
strList.forEach(System.out::println);
, . Java , . Collection
, forEach
, , , Collection
, , . Java.
Java , ( ). . . Java 8 forEach
Iterable
, Collection
, , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId
, , getFirstName
. , Person
, , , getId
, , getFirstName
.
, , , Collection/AbstractCollection
WindowListener/WindowAdapter
. .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName
:
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName
, Person
Naming
. , Java . getFirstName
Student
. , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming
getFirstName
:
interface Naming { String getFirstName(); }
Student
Person
? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person
Student
:
class Student extends Person implements Naming { ... }
, . Student
getFirstName
Person
, , Naming
getFirstName
. ┬л ┬╗ . ┬л ┬╗ Java 7. , , , . : , Object
. , toString
equals
, , List
. , Object.toString
Object.equals
.
- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare
-. , - , . - , , , .
тАУ , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int
, , . Java - Object
, Object
. Java , .
Java API java.util.function. , BiFunction <T, U, R>
, U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort
, BiFunction
. , . Java . , Comparator
, , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface
. . , . Javadoc , . . , , . @FunctionalInterface
- .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("тАж"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run
, . , . -. , . , call
Callable
. , Callable ( return null
).
, , . , , event
, . ,
button.setOnAction(event -> System.out.println(event));
, println
setOnAction
. :
button.setOnAction(System.out::println);
System.out::println
, - x -> System.out.println(x)
.
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println
x -> System.out.println(x)
. , Math::pow
(x, y) -> Math.pow(x, y)
. . , String::compareToIgnoreCase
- , (x, y) -> x.compareToIgnoreCase(y)
.
, . , Math.max
, int
double
. , , Math.max
. , -, . .
this
. , this::equals
тАУ , x -> this.equals(x)
. super
. super::instanceMethod
this
. , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable
, super::speak
, speak
. ( , , EnclosingClass.this::method
EnclosingClass.super::method
.)
, , , new
. , Button::new
Button
. ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream
, map
collect
. , , map
Button(String)
. Button
, , , , .
. , int[]::new
: . - x -> new int[x]
.
Java. T. new T[n]
, new Object[n]
. . , , . Stream
toArray
, Object
:
Object[] buttons = stream.toArray();
. , . . Button[]::new
toArray
:
Button[] buttons = stream.toArray(Button[]::new);
toArray
. .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count
text
-. , -. , repeatText
.
, , . - repeatText
. text
count
?
, , -. - :
; , ,
- , text
count
. , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++
, , , .
. Java 8 final
. -. final
; , , .
, . . matchCount
тАУ , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs
final
. ( final
, .) matchedObjs
ArrayList
. , . add
, .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this
- this
, . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString()
toString
Application
, Runnable
. this
-. - doWork
, this
.
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach
, .
strList.forEach(System.out::println);
, . Java , . Collection
, forEach
, , , Collection
, , . Java.
Java , ( ). . . Java 8 forEach
Iterable
, Collection
, , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId
, , getFirstName
. , Person
, , , getId
, , getFirstName
.
, , , Collection/AbstractCollection
WindowListener/WindowAdapter
. .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName
:
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName
, Person
Naming
. , Java . getFirstName
Student
. , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming
getFirstName
:
interface Naming { String getFirstName(); }
Student
Person
? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person
Student
:
class Student extends Person implements Naming { ... }
, . Student
getFirstName
Person
, , Naming
getFirstName
. ┬л ┬╗ . ┬л ┬╗ Java 7. , , , . : , Object
. , toString
equals
, , List
. , Object.toString
Object.equals
.
- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare
-. , - , . - , , , .
тАУ , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int
, , . Java - Object
, Object
. Java , .
Java API java.util.function. , BiFunction <T, U, R>
, U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort
, BiFunction
. , . Java . , Comparator
, , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface
. . , . Javadoc , . . , , . @FunctionalInterface
- .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("тАж"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run
, . , . -. , . , call
Callable
. , Callable ( return null
).
, , . , , event
, . ,
button.setOnAction(event -> System.out.println(event));
, println
setOnAction
. :
button.setOnAction(System.out::println);
System.out::println
, - x -> System.out.println(x)
.
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println
x -> System.out.println(x)
. , Math::pow
(x, y) -> Math.pow(x, y)
. . , String::compareToIgnoreCase
- , (x, y) -> x.compareToIgnoreCase(y)
.
, . , Math.max
, int
double
. , , Math.max
. , -, . .
this
. , this::equals
тАУ , x -> this.equals(x)
. super
. super::instanceMethod
this
. , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable
, super::speak
, speak
. ( , , EnclosingClass.this::method
EnclosingClass.super::method
.)
, , , new
. , Button::new
Button
. ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream
, map
collect
. , , map
Button(String)
. Button
, , , , .
. , int[]::new
: . - x -> new int[x]
.
Java. T. new T[n]
, new Object[n]
. . , , . Stream
toArray
, Object
:
Object[] buttons = stream.toArray();
. , . . Button[]::new
toArray
:
Button[] buttons = stream.toArray(Button[]::new);
toArray
. .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count
text
-. , -. , repeatText
.
, , . - repeatText
. text
count
?
, , -. - :
; , ,
- , text
count
. , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++
, , , .
. Java 8 final
. -. final
; , , .
, . . matchCount
тАУ , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs
final
. ( final
, .) matchedObjs
ArrayList
. , . add
, .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this
- this
, . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString()
toString
Application
, Runnable
. this
-. - doWork
, this
.
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach
, .
strList.forEach(System.out::println);
, . Java , . Collection
, forEach
, , , Collection
, , . Java.
Java , ( ). . . Java 8 forEach
Iterable
, Collection
, , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId
, , getFirstName
. , Person
, , , getId
, , getFirstName
.
, , , Collection/AbstractCollection
WindowListener/WindowAdapter
. .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName
:
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName
, Person
Naming
. , Java . getFirstName
Student
. , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming
getFirstName
:
interface Naming { String getFirstName(); }
Student
Person
? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person
Student
:
class Student extends Person implements Naming { ... }
, . Student
getFirstName
Person
, , Naming
getFirstName
. ┬л ┬╗ . ┬л ┬╗ Java 7. , , , . : , Object
. , toString
equals
, , List
. , Object.toString
Object.equals
.
- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare
-. , - , . - , , , .
тАУ , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int
, , . Java - Object
, Object
. Java , .
Java API java.util.function. , BiFunction <T, U, R>
, U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort
, BiFunction
. , . Java . , Comparator
, , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface
. . , . Javadoc , . . , , . @FunctionalInterface
- .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("тАж"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run
, . , . -. , . , call
Callable
. , Callable ( return null
).
, , . , , event
, . ,
button.setOnAction(event -> System.out.println(event));
, println
setOnAction
. :
button.setOnAction(System.out::println);
System.out::println
, - x -> System.out.println(x)
.
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println
x -> System.out.println(x)
. , Math::pow
(x, y) -> Math.pow(x, y)
. . , String::compareToIgnoreCase
- , (x, y) -> x.compareToIgnoreCase(y)
.
, . , Math.max
, int
double
. , , Math.max
. , -, . .
this
. , this::equals
тАУ , x -> this.equals(x)
. super
. super::instanceMethod
this
. , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable
, super::speak
, speak
. ( , , EnclosingClass.this::method
EnclosingClass.super::method
.)
, , , new
. , Button::new
Button
. ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream
, map
collect
. , , map
Button(String)
. Button
, , , , .
. , int[]::new
: . - x -> new int[x]
.
Java. T. new T[n]
, new Object[n]
. . , , . Stream
toArray
, Object
:
Object[] buttons = stream.toArray();
. , . . Button[]::new
toArray
:
Button[] buttons = stream.toArray(Button[]::new);
toArray
. .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count
text
-. , -. , repeatText
.
, , . - repeatText
. text
count
?
, , -. - :
; , ,
- , text
count
. , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++
, , , .
. Java 8 final
. -. final
; , , .
, . . matchCount
тАУ , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs
final
. ( final
, .) matchedObjs
ArrayList
. , . add
, .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this
- this
, . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString()
toString
Application
, Runnable
. this
-. - doWork
, this
.
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach
, .
strList.forEach(System.out::println);
, . Java , . Collection
, forEach
, , , Collection
, , . Java.
Java , ( ). . . Java 8 forEach
Iterable
, Collection
, , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId
, , getFirstName
. , Person
, , , getId
, , getFirstName
.
, , , Collection/AbstractCollection
WindowListener/WindowAdapter
. .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName
:
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName
, Person
Naming
. , Java . getFirstName
Student
. , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming
getFirstName
:
interface Naming { String getFirstName(); }
Student
Person
? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person
Student
:
class Student extends Person implements Naming { ... }
, . Student
getFirstName
Person
, , Naming
getFirstName
. ┬л ┬╗ . ┬л ┬╗ Java 7. , , , . : , Object
. , toString
equals
, , List
. , Object.toString
Object.equals
.
- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare
-. , - , . - , , , .
тАУ , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int
, , . Java - Object
, Object
. Java , .
Java API java.util.function. , BiFunction <T, U, R>
, U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort
, BiFunction
. , . Java . , Comparator
, , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface
. . , . Javadoc , . . , , . @FunctionalInterface
- .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("тАж"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run
, . , . -. , . , call
Callable
. , Callable ( return null
).
, , . , , event
, . ,
button.setOnAction(event -> System.out.println(event));
, println
setOnAction
. :
button.setOnAction(System.out::println);
System.out::println
, - x -> System.out.println(x)
.
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println
x -> System.out.println(x)
. , Math::pow
(x, y) -> Math.pow(x, y)
. . , String::compareToIgnoreCase
- , (x, y) -> x.compareToIgnoreCase(y)
.
, . , Math.max
, int
double
. , , Math.max
. , -, . .
this
. , this::equals
тАУ , x -> this.equals(x)
. super
. super::instanceMethod
this
. , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable
, super::speak
, speak
. ( , , EnclosingClass.this::method
EnclosingClass.super::method
.)
, , , new
. , Button::new
Button
. ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream
, map
collect
. , , map
Button(String)
. Button
, , , , .
. , int[]::new
: . - x -> new int[x]
.
Java. T. new T[n]
, new Object[n]
. . , , . Stream
toArray
, Object
:
Object[] buttons = stream.toArray();
. , . . Button[]::new
toArray
:
Button[] buttons = stream.toArray(Button[]::new);
toArray
. .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count
text
-. , -. , repeatText
.
, , . - repeatText
. text
count
?
, , -. - :
; , ,
- , text
count
. , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++
, , , .
. Java 8 final
. -. final
; , , .
, . . matchCount
тАУ , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs
final
. ( final
, .) matchedObjs
ArrayList
. , . add
, .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this
- this
, . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString()
toString
Application
, Runnable
. this
-. - doWork
, this
.
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach
, .
strList.forEach(System.out::println);
, . Java , . Collection
, forEach
, , , Collection
, , . Java.
Java , ( ). . . Java 8 forEach
Iterable
, Collection
, , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId
, , getFirstName
. , Person
, , , getId
, , getFirstName
.
, , , Collection/AbstractCollection
WindowListener/WindowAdapter
. .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName
:
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName
, Person
Naming
. , Java . getFirstName
Student
. , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming
getFirstName
:
interface Naming { String getFirstName(); }
Student
Person
? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person
Student
:
class Student extends Person implements Naming { ... }
, . Student
getFirstName
Person
, , Naming
getFirstName
. ┬л ┬╗ . ┬л ┬╗ Java 7. , , , . : , Object
. , toString
equals
, , List
. , Object.toString
Object.equals
.
- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare
-. , - , . - , , , .
тАУ , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int
, , . Java - Object
, Object
. Java , .
Java API java.util.function. , BiFunction <T, U, R>
, U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort
, BiFunction
. , . Java . , Comparator
, , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface
. . , . Javadoc , . . , , . @FunctionalInterface
- .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("тАж"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run
, . , . -. , . , call
Callable
. , Callable ( return null
).
, , . , , event
, . ,
button.setOnAction(event -> System.out.println(event));
, println
setOnAction
. :
button.setOnAction(System.out::println);
System.out::println
, - x -> System.out.println(x)
.
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println
x -> System.out.println(x)
. , Math::pow
(x, y) -> Math.pow(x, y)
. . , String::compareToIgnoreCase
- , (x, y) -> x.compareToIgnoreCase(y)
.
, . , Math.max
, int
double
. , , Math.max
. , -, . .
this
. , this::equals
тАУ , x -> this.equals(x)
. super
. super::instanceMethod
this
. , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable
, super::speak
, speak
. ( , , EnclosingClass.this::method
EnclosingClass.super::method
.)
, , , new
. , Button::new
Button
. ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream
, map
collect
. , , map
Button(String)
. Button
, , , , .
. , int[]::new
: . - x -> new int[x]
.
Java. T. new T[n]
, new Object[n]
. . , , . Stream
toArray
, Object
:
Object[] buttons = stream.toArray();
. , . . Button[]::new
toArray
:
Button[] buttons = stream.toArray(Button[]::new);
toArray
. .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count
text
-. , -. , repeatText
.
, , . - repeatText
. text
count
?
, , -. - :
; , ,
- , text
count
. , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++
, , , .
. Java 8 final
. -. final
; , , .
, . . matchCount
тАУ , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs
final
. ( final
, .) matchedObjs
ArrayList
. , . add
, .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this
- this
, . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString()
toString
Application
, Runnable
. this
-. - doWork
, this
.
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach
, .
strList.forEach(System.out::println);
, . Java , . Collection
, forEach
, , , Collection
, , . Java.
Java , ( ). . . Java 8 forEach
Iterable
, Collection
, , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId
, , getFirstName
. , Person
, , , getId
, , getFirstName
.
, , , Collection/AbstractCollection
WindowListener/WindowAdapter
. .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName
:
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName
, Person
Naming
. , Java . getFirstName
Student
. , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming
getFirstName
:
interface Naming { String getFirstName(); }
Student
Person
? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person
Student
:
class Student extends Person implements Naming { ... }
, . Student
getFirstName
Person
, , Naming
getFirstName
. ┬л ┬╗ . ┬л ┬╗ Java 7. , , , . : , Object
. , toString
equals
, , List
. , Object.toString
Object.equals
.
- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare
-. , - , . - , , , .
тАУ , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int
, , . Java - Object
, Object
. Java , .
Java API java.util.function. , BiFunction <T, U, R>
, U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort
, BiFunction
. , . Java . , Comparator
, , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface
. . , . Javadoc , . . , , . @FunctionalInterface
- .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("тАж"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run
, . , . -. , . , call
Callable
. , Callable ( return null
).
, , . , , event
, . ,
button.setOnAction(event -> System.out.println(event));
, println
setOnAction
. :
button.setOnAction(System.out::println);
System.out::println
, - x -> System.out.println(x)
.
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println
x -> System.out.println(x)
. , Math::pow
(x, y) -> Math.pow(x, y)
. . , String::compareToIgnoreCase
- , (x, y) -> x.compareToIgnoreCase(y)
.
, . , Math.max
, int
double
. , , Math.max
. , -, . .
this
. , this::equals
тАУ , x -> this.equals(x)
. super
. super::instanceMethod
this
. , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable
, super::speak
, speak
. ( , , EnclosingClass.this::method
EnclosingClass.super::method
.)
, , , new
. , Button::new
Button
. ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream
, map
collect
. , , map
Button(String)
. Button
, , , , .
. , int[]::new
: . - x -> new int[x]
.
Java. T. new T[n]
, new Object[n]
. . , , . Stream
toArray
, Object
:
Object[] buttons = stream.toArray();
. , . . Button[]::new
toArray
:
Button[] buttons = stream.toArray(Button[]::new);
toArray
. .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count
text
-. , -. , repeatText
.
, , . - repeatText
. text
count
?
, , -. - :
; , ,
- , text
count
. , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++
, , , .
. Java 8 final
. -. final
; , , .
, . . matchCount
тАУ , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs
final
. ( final
, .) matchedObjs
ArrayList
. , . add
, .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this
- this
, . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString()
toString
Application
, Runnable
. this
-. - doWork
, this
.
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach
, .
strList.forEach(System.out::println);
, . Java , . Collection
, forEach
, , , Collection
, , . Java.
Java , ( ). . . Java 8 forEach
Iterable
, Collection
, , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId
, , getFirstName
. , Person
, , , getId
, , getFirstName
.
, , , Collection/AbstractCollection
WindowListener/WindowAdapter
. .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName
:
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName
, Person
Naming
. , Java . getFirstName
Student
. , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming
getFirstName
:
interface Naming { String getFirstName(); }
Student
Person
? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person
Student
:
class Student extends Person implements Naming { ... }
, . Student
getFirstName
Person
, , Naming
getFirstName
. ┬л ┬╗ . ┬л ┬╗ Java 7. , , , . : , Object
. , toString
equals
, , List
. , Object.toString
Object.equals
.
- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare
-. , - , . - , , , .
тАУ , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int
, , . Java - Object
, Object
. Java , .
Java API java.util.function. , BiFunction <T, U, R>
, U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort
, BiFunction
. , . Java . , Comparator
, , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface
. . , . Javadoc , . . , , . @FunctionalInterface
- .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("тАж"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run
, . , . -. , . , call
Callable
. , Callable ( return null
).
, , . , , event
, . ,
button.setOnAction(event -> System.out.println(event));
, println
setOnAction
. :
button.setOnAction(System.out::println);
System.out::println
, - x -> System.out.println(x)
.
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println
x -> System.out.println(x)
. , Math::pow
(x, y) -> Math.pow(x, y)
. . , String::compareToIgnoreCase
- , (x, y) -> x.compareToIgnoreCase(y)
.
, . , Math.max
, int
double
. , , Math.max
. , -, . .
this
. , this::equals
тАУ , x -> this.equals(x)
. super
. super::instanceMethod
this
. , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable
, super::speak
, speak
. ( , , EnclosingClass.this::method
EnclosingClass.super::method
.)
, , , new
. , Button::new
Button
. ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream
, map
collect
. , , map
Button(String)
. Button
, , , , .
. , int[]::new
: . - x -> new int[x]
.
Java. T. new T[n]
, new Object[n]
. . , , . Stream
toArray
, Object
:
Object[] buttons = stream.toArray();
. , . . Button[]::new
toArray
:
Button[] buttons = stream.toArray(Button[]::new);
toArray
. .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count
text
-. , -. , repeatText
.
, , . - repeatText
. text
count
?
, , -. - :
; , ,
- , text
count
. , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++
, , , .
. Java 8 final
. -. final
; , , .
, . . matchCount
тАУ , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs
final
. ( final
, .) matchedObjs
ArrayList
. , . add
, .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this
- this
, . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString()
toString
Application
, Runnable
. this
-. - doWork
, this
.
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach
, .
strList.forEach(System.out::println);
, . Java , . Collection
, forEach
, , , Collection
, , . Java.
Java , ( ). . . Java 8 forEach
Iterable
, Collection
, , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId
, , getFirstName
. , Person
, , , getId
, , getFirstName
.
, , , Collection/AbstractCollection
WindowListener/WindowAdapter
. .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName
:
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName
, Person
Naming
. , Java . getFirstName
Student
. , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming
getFirstName
:
interface Naming { String getFirstName(); }
Student
Person
? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person
Student
:
class Student extends Person implements Naming { ... }
, . Student
getFirstName
Person
, , Naming
getFirstName
. ┬л ┬╗ . ┬л ┬╗ Java 7. , , , . : , Object
. , toString
equals
, , List
. , Object.toString
Object.equals
.
- , - Java, - , . ? Java , . , , , . , , Java 8.
Comparator . compare
-. , - , . - , , , .
тАУ , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , , (String, String) -> int
, , . Java - Object
, Object
. Java , .
Java API java.util.function. , BiFunction <T, U, R>
, U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, . Arrays.sort
, BiFunction
. , . Java . , Comparator
, , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . , @FunctionalInterface
. . , . Javadoc , . . , , . @FunctionalInterface
- .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("тАж"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run
, . , . -. , . , call
Callable
. , Callable ( return null
).
, , . , , event
, . ,
button.setOnAction(event -> System.out.println(event));
, println
setOnAction
. :
button.setOnAction(System.out::println);
System.out::println
, - x -> System.out.println(x)
.
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . , System.out::println
x -> System.out.println(x)
. , Math::pow
(x, y) -> Math.pow(x, y)
. . , String::compareToIgnoreCase
- , (x, y) -> x.compareToIgnoreCase(y)
.
, . , Math.max
, int
double
. , , Math.max
. , -, . .
this
. , this::equals
тАУ , x -> this.equals(x)
. super
. super::instanceMethod
this
. , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable
, super::speak
, speak
. ( , , EnclosingClass.this::method
EnclosingClass.super::method
.)
, , , new
. , Button::new
Button
. ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream
, map
collect
. , , map
Button(String)
. Button
, , , , .
. , int[]::new
: . - x -> new int[x]
.
Java. T. new T[n]
, new Object[n]
. . , , . Stream
toArray
, Object
:
Object[] buttons = stream.toArray();
. , . . Button[]::new
toArray
:
Button[] buttons = stream.toArray(Button[]::new);
toArray
. .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count
text
-. , -. , repeatText
.
, , . - repeatText
. text
count
?
, , -. - :
; , ,
- , text
count
. , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, . matchCount++
, , , .
. Java 8 final
. -. final
; , , .
, . . matchCount
тАУ , , .
, , . ,
List<Path> matchedObjs = new ArrayList<>(); for (Path p : files) new Thread(() -> { if (p has some property) matchedObjs.add(p); }).start(); // Legal to mutate matchedObjs, but unsafe
, matchedObjs
final
. ( final
, .) matchedObjs
ArrayList
. , . add
, .
. . .
, , - . 1, :
int[] counts = new int[1]; button.setOnAction(event -> counts[0]++);
, . , , , .
- , . . , , .
Path first = Paths.get("/usr/local"); Comparator<String> comp = (first, second) -> Integer.compare(first.length(), second.length()); // Error: Variable first already defined
. , -. this
- this
, . , ,
public class Application() { public void doWork() { Runnable r = () -> { ...; System.out.println(this.toString()); ... }; ... } }
this.toString()
toString
Application
, Runnable
. this
-. - doWork
, this
.
. , , , . , :
for (int i = 0; i < strList.size(); i++) System.out.println(strList.get(i));
. forEach
, .
strList.forEach(System.out::println);
, . Java , . Collection
, forEach
, , , Collection
, , . Java.
Java , ( ). . . Java 8 forEach
Iterable
, Collection
, , .
:
interface Person { long getId(); default String getFirstName() { return "Jack"; } }
: getId
, , getFirstName
. , Person
, , , getId
, , getFirstName
.
, , , Collection/AbstractCollection
WindowListener/WindowAdapter
. .
, , ? C++ . , Java . :
. , . . , ( ), .
. getFirstName
:
interface Naming { default String getFirstName() { return getClass().getName() + "_" + hashCode(); } }
, , ?
class Student implements Person, Naming { ... }
getFirstName
, Person
Naming
. , Java . getFirstName
Student
. , :
class Student implements Person, Naming { public String getFirstName() { returnPerson.super.getFirstName(); } ... }
, Naming
getFirstName
:
interface Naming { String getFirstName(); }
Student
Person
? , Java . , . , , .
, -Java 8 . : . .
. , , . , , Person
Student
:
class Student extends Person implements Naming { ... }
, . Student
getFirstName
Person
, , Naming
getFirstName
. ┬л ┬╗ . ┬л ┬╗ Java 7. , , , . : , Object
. , toString
equals
, , List
. , Object.toString
Object.equals
.
- , - Java, - , . ? Java , . , , , . , , Java 8.
-
Comparator . compare
-. , - , . - , , , .
тАУ , - . . :
button.setOnAction(event -> System.out.println("The button has been clicked!"));
.
, - , - Java. , , ,(String, String) -> int
, , . Java -Object
,Object
. Java , .
Java API java.util.function. ,BiFunction <T, U, R>
, U R. :
BiFunction<String, String, Integer> compareFunc = (firstStr, secondStr) -> Integer.compare(firstStr.length(), secondStr.length());
, .Arrays.sort
,BiFunction
. , . Java . ,Comparator
, , . Java 8 . - - , , .
java.util.function Java 8 API , , , . , - , API, . ,@FunctionalInterface
. . , . Javadoc , . . , , .@FunctionalInterface
- .
, , checked . - checked , . , :
Runnable sleepingRunner = () -> { System.out.println("тАж"); Thread.sleep(1000); }; // Error: Thread.sleep can throw a checkedInterruptedException
Runnable.run
, . , . -. , . ,call
Callable
. ,Callable ( return null
).
, , . , ,event
, . ,
button.setOnAction(event -> System.out.println(event));
,println
setOnAction
. :
button.setOnAction(System.out::println);
System.out::println
, -x -> System.out.println(x)
.
, , . :
Arrays.sort(strs, String::compareToIgnoreCase)
:: . :
object::instanceMethod Class::staticMethod Class::instanceMethod
-, . ,System.out::println
x -> System.out.println(x)
. ,Math::pow
(x, y) -> Math.pow(x, y)
. . ,String::compareToIgnoreCase
- ,(x, y) -> x.compareToIgnoreCase(y)
.
, . ,Math.max
,int
double
. , ,Math.max
. , -, . .
this
. ,this::equals
тАУ ,x -> this.equals(x)
.super
.super::instanceMethod
this
. , :
class Speaker { public void speak() { System.out.println("Hello, world!"); } } class ConcurrentSpeaker extends Speaker { public void speak() { Thread t = new Thread(super::speak); t.start(); } }
Runnable
,super::speak
,speak
. ( , ,EnclosingClass.this::method
EnclosingClass.super::method
.)
, , ,new
. ,Button::new
Button
. ? . , . , , , :
List<String> strs = ...; Stream<Button> stream = strs.stream().map(Button::new); List<Button> buttons = stream.collect(Collectors.toList());
stream
,map
collect
. , ,map
Button(String)
.Button
, , , , .
. ,int[]::new
: . -x -> new int[x]
.
Java. T.new T[n]
,new Object[n]
. . , , .Stream
toArray
,Object
:
Object[] buttons = stream.toArray();
. , . .Button[]::new
toArray
:
Button[] buttons = stream.toArray(Button[]::new);
toArray
. .
-. :
public static void repeatText(String text, int count) { Runnable r = () -> { for (int i = 0; i < count; i++) { System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
:
repeatText("Hi!", 2000); // Prints Hi 2000 times in a separate thread
count
text
-. , -. ,repeatText
.
, , . -repeatText
.text
count
?
, , -. - :
; , ,
- ,text
count
. , -, , , "Hi!" 2000. , -. ( , . , - , .)
. - , , , Java . Java - . , . Java 8 .
, - . Java, , , . - , . , :
public static void repeatText(String text, int count) { Runnable r = () -> { while (count > 0) { count--; // Error: Can't mutate captured variable System.out.println(text); Thread.yield(); } }; new Thread(r).start(); }
. - . , .
int matchCount = 0; for (Path p : files) new Thread(() -> { if (p has some property) matchCount++; }).start(); // Illegal to mutate matchCount
, .matchCount++
, , , .
. Java 8final
. -.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 8forEach
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 8final
. -.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 8forEach
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 8final
. -.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 8forEach
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 8final
. -.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 8forEach
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 8final
. -.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 8forEach
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 8final
. -.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 8forEach
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 8final
. -.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 8forEach
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 8final
. -.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 8forEach
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.