נבנה על ידיBuilt by Ariel Yohimovich
JavaJava
Lambdas ו-functional interfacesLambdas & functional interfaces
מפגש 26: ביטויי למבדה, Function, Predicate, Consumer ו-Supplier, ו-method references.Session 26: Lambda expressions, Function, Predicate, Consumer and Supplier, and method references.
ביטויי למבדה, Function, Predicate, Consumer ו-Supplier, ו-method references. בסוף המפגש מוסיפים צעד לפרויקט המלווה.Lambda expressions, Function, Predicate, Consumer and Supplier, and method references. At the end we add a step to the companion project.מה נעשה היוםToday's plan
- מונחיםTerms
- מחלקה קטנה → שורה אחתA small class → one line
- ארבעת ה-interfaces השימושייםThe four handy interfaces
- למבדה לא משנה משתנה מבחוץA lambda doesn't change an outside variable
- טעויות נפוצותCommon mistakes
- פרויקט מדףShelf project
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.המונחים של המפגשThis session's terms
- למבדה: פונקציה קצרה בלי שם:lambda: A short nameless function:
- functional interface: interface עם מתודה מופשטת אחת בדיוק (כמו
Comparator, Runnable, Predicate).functional interface: An interface with exactly one abstract method (like Comparator, Runnable, Predicate). - Predicate, Function, Consumer, Supplier:
Predicate<T>:Predicate, Function, Consumer, Supplier: Predicate<T>: - method reference: קיצור ללמבדה שרק קוראת למתודה:method reference: A shorthand for a lambda that only calls a method:
- effectively final: למבדה יכולה להשתמש במשתנה מקומי מבחוץ רק אם הוא לא משתנה אחרי שנקבע.effectively final: A lambda can use an outside local variable only if it never changes after it's set.
- Comparator.comparing: בונה Comparator ממפתח:Comparator.comparing: Builds a Comparator from a key:
לפני הקוד, המילים. כל מונח כאן מקושר גם במילון של הקורס.Before the code, the words. Every term here is also linked in the course glossary.נושאTopic 2
מחלקה קטנה → שורה אחתA small class → one line
מחלקה קטנה → שורה אחתA small class → one line
- במפגש 24 כתבנו מחלקה שלמה בשביל Comparator.In session 24 we wrote a whole class for a Comparator.
- למבדה אומרת אותו דבר בשורה:
(a, b) -> a.length() - b.length().A lambda says the same in one line: (a, b) -> a.length() - b.length(). Comparator.comparing(String::length) עוד קצר יותר, ו-forEach מקבל למבדה שרצה על כל איבר.Comparator.comparing(String::length) is shorter still, and forEach takes a lambda that runs on each item.
במפגש 24 כתבנו מחלקה שלמה בשביל Comparator. למבדה אומרת אותו דבר בשורה: (a, b) -> a.length() - b.length(). Comparator.comparing(String::length) עוד קצר יותר, ו-forEach מקבל למבדה שרצה על כל איבר.In session 24 we wrote a whole class for a Comparator. A lambda says the same in one line: (a, b) -> a.length() - b.length(). Comparator.comparing(String::length) is shorter still, and forEach takes a lambda that runs on each item.נושאTopic 3
ארבעת ה-interfaces השימושייםThe four handy interfaces
ארבעת ה-interfaces השימושייםThe four handy interfaces
- Predicate בודק, Function ממיר, Consumer עושה משהו, ו-Supplier מייצר.A Predicate tests, a Function converts, a Consumer does something, and a Supplier produces.
- אפשר להרכיב:
negate(), andThen(...).They compose: negate(), andThen(...).
Predicate בודק, Function ממיר, Consumer עושה משהו, ו-Supplier מייצר. אפשר להרכיב: negate(), andThen(...).A Predicate tests, a Function converts, a Consumer does something, and a Supplier produces. They compose: negate(), andThen(...).נושאTopic 4
למבדה לא משנה משתנה מבחוץA lambda doesn't change an outside variable
למבדה לא משנה משתנה מבחוץA lambda doesn't change an outside variable
total משתנה בתוך הלמבדה, ולכן הוא לא effectively final.total changes inside the lambda, so it isn't effectively final.- javac עוצר.javac stops.
- בתרגיל 4 תתקנו עם לולאה רגילה (ובמפגש הבא, עם Stream).In exercise 4 you'll fix it with a plain loop (and next session, with a Stream).
total משתנה בתוך הלמבדה, ולכן הוא לא effectively final. javac עוצר. בתרגיל 4 תתקנו עם לולאה רגילה (ובמפגש הבא, עם Stream).total changes inside the lambda, so it isn't effectively final. javac stops. In exercise 4 you'll fix it with a plain loop (and next session, with a Stream).נושאTopic 5
טעויות נפוצותCommon mistakes
טעויות נפוצות, ומה Java עונהCommon mistakes, and what Java says
local variables referenced from a lambda expression must be final or effectively final:local variables referenced from a lambda expression must be final or effectively final:
אלה הטעויות שהתרגילים מהסוג "צייד באגים" בנויים סביבן. כל אחת רצה באמת, והפלט הוא מה ש-Java עונה.These are the mistakes the "bug hunter" exercises are built around. Each one really runs, and the output is what Java says.נושאTopic 6
פרויקט מדףShelf project
הצעד של היום בפרויקט מדףToday's step in the Shelf project
- מדף, צעד 26: סינון ספרים עם PredicateShelf, step 26: filtering books with a Predicate
ל-Shelf הוסיפו find(Predicate<Book>), count(Predicate<Book>) ו-static Predicate<Book> byAuthor(String). הדפיסו [Emma, Persuasion], [Persuasion], [Emma, Ulysses], 2.Add find(Predicate<Book>), count(Predicate<Book>) and static Predicate<Book> byAuthor(String) to Shelf. Print [Emma, Persuasion], [Persuasion], [Emma, Ulysses], 2.למבדה: פונקציה קצרה בלי שם:lambda: A short nameless function:
המונחים של המפגשThis session's termsfunctional interface: interface עם מתודה מופשטת אחת בדיוק (כמו Comparator, Runnable, Predicate).functional interface: An interface with exactly one abstract method (like Comparator, Runnable, Predicate).
המונחים של המפגשThis session's termsPredicate, Function, Consumer, Supplier: Predicate<T>:Predicate, Function, Consumer, Supplier: Predicate<T>:
המונחים של המפגשThis session's termsmethod reference: קיצור ללמבדה שרק קוראת למתודה:method reference: A shorthand for a lambda that only calls a method:
המונחים של המפגשThis session's termseffectively final: למבדה יכולה להשתמש במשתנה מקומי מבחוץ רק אם הוא לא משתנה אחרי שנקבע.effectively final: A lambda can use an outside local variable only if it never changes after it's set.
המונחים של המפגשThis session's termsComparator.comparing: בונה Comparator ממפתח:Comparator.comparing: Builds a Comparator from a key:
המונחים של המפגשThis session's termsבמפגש 24 כתבנו מחלקה שלמה בשביל Comparator.In session 24 we wrote a whole class for a Comparator.
מחלקה קטנה → שורה אחתA small class → one lineלמבדה אומרת אותו דבר בשורה: (a, b) -> a.length() - b.length().A lambda says the same in one line: (a, b) -> a.length() - b.length().
מחלקה קטנה → שורה אחתA small class → one lineComparator.comparing(String::length) עוד קצר יותר, ו-forEach מקבל למבדה שרצה על כל איבר.Comparator.comparing(String::length) is shorter still, and forEach takes a lambda that runs on each item.
מחלקה קטנה → שורה אחתA small class → one linePredicate בודק, Function ממיר, Consumer עושה משהו, ו-Supplier מייצר.A Predicate tests, a Function converts, a Consumer does something, and a Supplier produces.
ארבעת ה-interfaces השימושייםThe four handy interfacesאפשר להרכיב: negate(), andThen(...).They compose: negate(), andThen(...).
ארבעת ה-interfaces השימושייםThe four handy interfacestotal משתנה בתוך הלמבדה, ולכן הוא לא effectively final.total changes inside the lambda, so it isn't effectively final.
למבדה לא משנה משתנה מבחוץA lambda doesn't change an outside variablejavac עוצר.javac stops.
למבדה לא משנה משתנה מבחוץA lambda doesn't change an outside variableבתרגיל 4 תתקנו עם לולאה רגילה (ובמפגש הבא, עם Stream).In exercise 4 you'll fix it with a plain loop (and next session, with a Stream).
למבדה לא משנה משתנה מבחוץA lambda doesn't change an outside variablelocal variables referenced from a lambda expression must be final or effectively final:local variables referenced from a lambda expression must be final or effectively final:
טעויות נפוצות, ומה Java עונהCommon mistakes, and what Java saysמדף, צעד 26: סינון ספרים עם PredicateShelf, step 26: filtering books with a Predicate
הצעד של היום בפרויקט מדףToday's step in the Shelf projectאיזה interface מתאים ל-s -> s.isEmpty()?Which interface fits s -> s.isEmpty()?
Predicate<String>Predicate<String>Supplier<String>Supplier<String>Consumer<String>Consumer<String>RunnableRunnable
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.Predicate<String>Predicate<String>
נכון: מקבל String ומחזיר boolean.Right: takes a String, returns a boolean.מה שווה ל-String::toUpperCase?What's equivalent to String::toUpperCase?
s -> s.toUpperCase()s -> s.toUpperCase()() -> toUpperCase()() -> toUpperCase()String.toUpperCase(s)String.toUpperCase(s)s -> String.toUpperCases -> String.toUpperCase
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.s -> s.toUpperCase()s -> s.toUpperCase()
נכון.Right.כמה מתודות מופשטות יש ב-functional interface?How many abstract methods does a functional interface have?
- בדיוק אחתExactly one
- לפחות שתייםAt least two
- אפסZero
- כמה שרוציםAny number
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.בדיוק אחתExactly one
נכון (מתודות default לא נספרות).Right (default methods don't count).מיון עם למבדהSorting with a lambda
- מיינו
Noa, Avraham, Dan, Michal לפי אורך עם למבדה ([Noa, Dan, Michal, Avraham]), ואז לפי אורך יורד, ובשוויון לפי א-ב, עם Comparator.comparing:Sort Noa, Avraham, Dan, Michal by length with a lambda ([Noa, Dan, Michal, Avraham]), then by length descending, ties alphabetical, with Comparator.comparing:
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מסננים עם PredicateFilters with a Predicate
static List<Book> filter(List<Book> books, Predicate<Book> rule).static List<Book> filter(List<Book> books, Predicate<Book> rule).
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.שרשרת פונקציותA chain of functions
trim.andThen(upper) מנקה מחרוזת.trim.andThen(upper) cleans a string.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.צייד באגים: משתנה בתוך למבדהBug hunter: a variable inside a lambda
- התוכנית מהתזכורת לא מתקמפלת.The program from the refresher doesn't compile.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מחשבון של למבדותA calculator of lambdas
@FunctionalInterface interface Op { int apply(int a, int b); }.@FunctionalInterface interface Op { int apply(int a, int b); }.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מדף, צעד 26: סינון ספרים עם PredicateShelf, step 26: filtering books with a Predicate
- ל-
Shelf הוסיפו find(Predicate<Book>), count(Predicate<Book>) ו-static Predicate<Book> byAuthor(String).Add find(Predicate<Book>), count(Predicate<Book>) and static Predicate<Book> byAuthor(String) to Shelf.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.פונקציה שמחזירה פונקציהA function returning a function
Function<Integer, Function<Integer, Integer>> add = a -> b -> a + b;.Function<Integer, Function<Integer, Integer>> add = a -> b -> a + b;.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.ארבעה סוגי method referenceFour kinds of method reference
- כתבו method reference מכל סוג:Write a method reference of each kind:
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מה למדנוWhat we learned
- למבדה: פונקציה קצרה בלי שם:lambda: A short nameless function:
- במפגש 24 כתבנו מחלקה שלמה בשביל Comparator.In session 24 we wrote a whole class for a Comparator.
- Predicate בודק, Function ממיר, Consumer עושה משהו, ו-Supplier מייצר.A Predicate tests, a Function converts, a Consumer does something, and a Supplier produces.
total משתנה בתוך הלמבדה, ולכן הוא לא effectively final.total changes inside the lambda, so it isn't effectively final.local variables referenced from a lambda expression must be final or effectively final:local variables referenced from a lambda expression must be final or effectively final:- מדף, צעד 26: סינון ספרים עם PredicateShelf, step 26: filtering books with a Predicate
כל אחד אומר דבר אחד שלמד היום.Everyone says one thing they learned today.עכשיו לתרגולNow, practice
6 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית6 exercises and 2 challenges, auto-checked
מפנים לעמוד התרגול.Point to the practice page.