נבנה על ידיBuilt by Ariel Yohimovich

חזרה למפגשBack
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.

מה נעשה היום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
נושאTopic 1

מונחיםTerms

המונחים של המפגש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:
נושא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.
נושא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(...).
נושא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).
נושא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:
נושאTopic 6

פרויקט מדףShelf project

הצעד של היום בפרויקט מדףToday's step in the Shelf project

  • מדף, צעד 26: סינון ספרים עם PredicateShelf, step 26: filtering books with a Predicate

למבדה: פונקציה קצרה בלי שם: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:

במפגש 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.

Predicate בודק, Function ממיר, Consumer עושה משהו, ו-Supplier מייצר.A Predicate tests, a Function converts, a Consumer does something, and a Supplier produces.

אפשר להרכיב: negate(), andThen(...).They compose: negate(), andThen(...).

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).

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

איזה interface מתאים ל-s -> s.isEmpty()?Which interface fits s -> s.isEmpty()?

  • Predicate<String>Predicate<String>
  • Supplier<String>Supplier<String>
  • Consumer<String>Consumer<String>
  • RunnableRunnable

Predicate<String>Predicate<String>

מה שווה ל-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

s -> s.toUpperCase()s -> s.toUpperCase()

כמה מתודות מופשטות יש ב-functional interface?How many abstract methods does a functional interface have?

  • בדיוק אחתExactly one
  • לפחות שתייםAt least two
  • אפסZero
  • כמה שרוציםAny number

בדיוק אחתExactly one

מיון עם למבדה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:

מסננים עם PredicateFilters with a Predicate

  • static List<Book> filter(List<Book> books, Predicate<Book> rule).static List<Book> filter(List<Book> books, Predicate<Book> rule).

שרשרת פונקציותA chain of functions

  • trim.andThen(upper) מנקה מחרוזת.trim.andThen(upper) cleans a string.

צייד באגים: משתנה בתוך למבדהBug hunter: a variable inside a lambda

  • התוכנית מהתזכורת לא מתקמפלת.The program from the refresher doesn't compile.

מחשבון של למבדותA calculator of lambdas

  • @FunctionalInterface interface Op { int apply(int a, int b); }.@FunctionalInterface interface Op { int apply(int a, int b); }.

מדף, צעד 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.

פונקציה שמחזירה פונקציה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;.

ארבעה סוגי method referenceFour kinds of method reference

  • כתבו method reference מכל סוג:Write a method reference of each kind:

מה למדנו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

עכשיו לתרגולNow, practice

6 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית6 exercises and 2 challenges, auto-checked
הערות מרצהSpeaker notes

נבנה על ידיBuilt by Ariel Yohimovich