Lambda expressions, Function, Predicate, Consumer and Supplier, and method references.
כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-IntelliJ ולסמן פתרתי. ההתקדמות נשמרת.Write in each exercise's editor, press Run to see output and Check for the automatic checker. You can also work in IntelliJ and mark I solved it. Progress is saved.
חימום: 3 שאלותWarm-up: 3 questions
תרגיליםExercises
תרגילExercise 1
מיון עם למבדהSorting with a lambda
מיינו Noa, Avraham, Dan, Michal לפי אורך עם למבדה ([Noa, Dan, Michal, Avraham]), ואז לפי אורך יורד, ובשוויון לפי א-ב, עם Comparator.comparing: [Avraham, Michal, Dan, Noa].
Sort Noa, Avraham, Dan, Michal by length with a lambda ([Noa, Dan, Michal, Avraham]), then by length descending, ties alphabetical, with Comparator.comparing: [Avraham, Michal, Dan, Noa].
איך הפלט אמור להיראות?What should the output look like?
[Noa, Dan, Michal, Avraham]
[Avraham, Michal, Dan, Noa]
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
static List<Book> filter(List<Book> books, Predicate<Book> rule). עם old (לפני 1950) ו-great (דירוג 4 ומעלה): הדפיסו [Emma, Ulysses], [Emma] (and), ו-[Dune, Ulysses] (חדש או מתחיל ב-U).
static List<Book> filter(List<Book> books, Predicate<Book> rule). With old (before 1950) and great (rating 4 or more): print [Emma, Ulysses], [Emma] (and), and [Dune, Ulysses] (new or starting with U).
איך הפלט אמור להיראות?What should the output look like?
[Emma, Ulysses]
[Emma]
[Dune, Ulysses]
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
trim.andThen(upper) מנקה מחרוזת. הדפיסו [DUNE] עבור " dune ", את האורך אחרי הניקוי (4), ו-12 (twice פעמיים על 3).
trim.andThen(upper) cleans a string. Print [DUNE] for " dune ", the length after cleaning (4), and 12 (twice twice on 3).
איך הפלט אמור להיראות?What should the output look like?
[DUNE]
4
12
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:UnaryOperator<Integer> הוא Function שמחזיר את אותו טיפוס.UnaryOperator<Integer> is a Function returning the same type.
צייד באגים: משתנה בתוך למבדהBug hunter: a variable inside a lambda
התוכנית מהתזכורת לא מתקמפלת. כתבו את הספירה בלולאה רגילה כך שיודפס 6.
The program from the refresher doesn't compile. Write the count as a plain loop so it prints 6.
איך הפלט אמור להיראות?What should the output look like?
6
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:for (String w : words) { total += w.length(); }for (String w : words) { total += w.length(); }
@FunctionalInterface interface Op { int apply(int a, int b); }. מפה ממוינת מסימן לפעולה: +, -, max (Math::max) ו-pow (למבדה עם בלוק). הדפיסו לכל אחת על 2 ו-5: + -> 7, - -> -3, max -> 5, pow -> 32.
@FunctionalInterface interface Op { int apply(int a, int b); }. A sorted map from a symbol to an operation: +, -, max (Math::max) and pow (a lambda with a block). Print each on 2 and 5: + -> 7, - -> -3, max -> 5, pow -> 32.
איך הפלט אמור להיראות?What should the output look like?
+ -> 7
- -> -3
max -> 5
pow -> 32
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:למבדה עם כמה שורות: (a, b) -> { ...; return r; }.A multi-line lambda: (a, b) -> { ...; return r; }.
Add find(Predicate<Book>), count(Predicate<Book>) and static Predicate<Book> byAuthor(String) to Shelf. Print [Emma, Persuasion], [Persuasion], [Emma, Ulysses], 2.
איך הפלט אמור להיראות?What should the output look like?
[Emma, Persuasion]
[Persuasion]
[Emma, Ulysses]
2
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:byAuthor מחזירה למבדה שזוכרת את author.byAuthor returns a lambda that remembers author.
פונקציה שמחזירה פונקציהA function returning a function
Function<Integer, Function<Integer, Integer>> add = a -> b -> a + b;. add.apply(10) היא "הוסף 10". הדפיסו 15 3, ואז 22 (times.andThen(plus10) על 3 ו-4).
Function<Integer, Function<Integer, Integer>> add = a -> b -> a + b;. add.apply(10) is "add 10". Print 15 3, then 22 (times.andThen(plus10) on 3 and 4).
איך הפלט אמור להיראות?What should the output look like?
15 3
22
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:BiFunction מקבל שני ארגומנטים.A BiFunction takes two arguments.
ארבעה סוגי method referenceFour kinds of method reference
כתבו method reference מכל סוג: static (Integer::parseInt), של עצם מסוים (prefix::concat), של כל עצם מהטיפוס (String::length), ובנאי (Book::new). הדפיסו 43, Book: Dune 4, Book[title=Emma, year=1815] [].
Write a method reference of each kind: static (Integer::parseInt), of a particular object (prefix::concat), of any object of the type (String::length), and a constructor (Book::new). Print 43, Book: Dune 4, Book[title=Emma, year=1815] [].
איך הפלט אמור להיראות?What should the output look like?
43
Book: Dune 4
Book[title=Emma, year=1815] []
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:BiFunction<String, Integer, Book> make = Book::new;BiFunction<String, Integer, Book> make = Book::new;