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

JavaJava
27
חלקPart 5: Java מודרנית וכליםModern Java & tools כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

Streams ו-OptionalStreams & Optional

filter, map, reduce ו-collect, groupingBy, ו-Optional במקום null.

filter, map, reduce and collect, groupingBy, and Optional instead of null.

כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-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

1. מה מחזיר books.stream().filter(b -> b.year() > 1900) לבד?What does books.stream().filter(b -> b.year() > 1900) return on its own?

2. list.stream().map(String::length).toList() משנה את list?Does list.stream().map(String::length).toList() change list?

3. findFirst() על Stream ריק מחזיר?findFirst() on an empty Stream returns?

תרגיליםExercises

תרגילExercise 1

כותרים חדשים באותיות גדולותNew titles in capitals

מהמדף (Emma 1815, Dune 1965, Persuasion 1817, Ulysses 1922) קחו את הספרים אחרי 1816, הפכו את הכותר לאותיות גדולות, מיינו ואספו: [DUNE, PERSUASION, ULYSSES].

From the shelf (Emma 1815, Dune 1965, Persuasion 1817, Ulysses 1922) take the books after 1816, upper-case the title, sort and collect: [DUNE, PERSUASION, ULYSSES].

איך הפלט אמור להיראות?What should the output look like?
[DUNE, PERSUASION, 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.
רמזHint 1: filter, map, sorted, toListfilter, map, sorted, toList
לפתרוןSolution
תרגילExercise 2

סכום וממוצעSum and average

הדפיסו את סך העמודים, את ממוצע העמודים של Austen, ואת הממוצע של סופר שאין לו ספרים (0.0): 1865 361.5 0.0.

Print the total pages, Austen's average pages, and the average of an author with no books (0.0): 1865 361.5 0.0.

איך הפלט אמור להיראות?What should the output look like?
1865 361.5 0.0
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: average() מחזיר OptionalDouble: .orElse(0).average() returns an OptionalDouble: .orElse(0).
לפתרוןSolution
תרגילExercise 3

קיבוץGrouping

בנו TreeMap מסופר לכמות ספרים ({Austen=2, Herbert=1, Joyce=1}), ו-TreeMap מדירוג לרשימת כותרים ({3=[Ulysses], 4=[Emma], 5=[Dune, Persuasion]}).

Build a TreeMap from author to book count ({Austen=2, Herbert=1, Joyce=1}), and a TreeMap from rating to a list of titles ({3=[Ulysses], 4=[Emma], 5=[Dune, Persuasion]}).

איך הפלט אמור להיראות?What should the output look like?
{Austen=2, Herbert=1, Joyce=1}
{3=[Ulysses], 4=[Emma], 5=[Dune, Persuasion]}
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: Collectors.mapping(Book::title, Collectors.toList()) כ-collector פנימי.Collectors.mapping(Book::title, Collectors.toList()) as the inner collector.
לפתרוןSolution
תרגילExercise 4

מי כתב את זה?Who wrote it?

static String authorOf(List<Book> books, String title) עם findFirst, map ו-orElse("unknown"), בלי null ובלי if. הדפיסו Herbert ואז unknown.

static String authorOf(List<Book> books, String title) with findFirst, map and orElse("unknown"), no null and no if. Print Herbert then unknown.

איך הפלט אמור להיראות?What should the output look like?
Herbert
unknown
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: Optional<Book> b = ...findFirst(); return b.map(Book::author).orElse("unknown");Optional<Book> b = ...findFirst(); return b.map(Book::author).orElse("unknown");
לפתרוןSolution
תרגילExercise 5

צייד באגים: Stream פעמייםBug hunter: a Stream twice

התוכנית מהתזכורת קורסת. תקנו כך שכל שאלה תקבל Stream משלה, ויודפס 3 ואז [Emma, Dune, Persuasion].

The program from the refresher crashes. Fix it so each question gets its own Stream, printing 3 then [Emma, Dune, Persuasion].

איך הפלט אמור להיראות?What should the output look like?
3
[Emma, Dune, Persuasion]
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: books.stream()... פעמיים.books.stream()... twice.
לפתרוןSolution
תרגילExercise 6

מדף, צעד 27: סטטיסטיקות בשורהShelf, step 27: statistics in a line

פרויקט מלווהCompanion project

עם summaryStatistics() הדפיסו 4 books, rating 3-5, avg 4.25. מצאו את הטוב ביותר (דירוג, ובשוויון השנה המאוחרת): Best: Dune. ואז את הסופרים בלי כפולים, ממוינים: Austen | Herbert | Joyce.

With summaryStatistics() print 4 books, rating 3-5, avg 4.25. Find the best (rating, ties by the later year): Best: Dune. Then the authors without duplicates, sorted: Austen | Herbert | Joyce.

איך הפלט אמור להיראות?What should the output look like?
4 books, rating 3-5, avg 4.25
Best: Dune
Austen | Herbert | Joyce
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: max(Comparator.comparingInt(Book::rating).thenComparing(Book::year))max(Comparator.comparingInt(Book::rating).thenComparing(Book::year))
רמזHint 2: distinct(), sorted(), joining(" | ")distinct(), sorted(), joining(" | ")
לפתרוןSolution

אתגרים לחזקיםChallenges for the brave

אתגרChallenge 1

flatMap לתגיותflatMap for tags

לכל פוסט יש רשימת תגיות. עם flatMap הפכו את כל התגיות לזרם אחד, בלי כפולים וממוין: [git, java, oop, streams]. אחר כך את מספר התגיות הכולל (5).

Each post has a list of tags. With flatMap turn all tags into one stream, without duplicates and sorted: [git, java, oop, streams]. Then the total number of tags (5).

איך הפלט אמור להיראות?What should the output look like?
[git, java, oop, streams]
5
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: flatMap(p -> p.tags().stream())flatMap(p -> p.tags().stream())
לפתרוןSolution
אתגרChallenge 2

partitioningBy ו-toMappartitioningBy and toMap

חלקו לספרים ארוכים (מעל 450 עמודים) וקצרים: long: [Emma, Ulysses] short: [Dune, Persuasion]. אחר כך מפה ממוינת מסופר לסך העמודים (toMap עם Integer::sum): {Austen=723, Herbert=412, Joyce=730}.

Split into long books (over 450 pages) and short: long: [Emma, Ulysses] short: [Dune, Persuasion]. Then a sorted map from author to total pages (toMap with Integer::sum): {Austen=723, Herbert=412, Joyce=730}.

איך הפלט אמור להיראות?What should the output look like?
long: [Emma, Ulysses] short: [Dune, Persuasion]
{Austen=723, Herbert=412, Joyce=730}
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: toMap(key, value, merge, TreeMap::new): בלי merge, מפתח כפול זורק חריגה.toMap(key, value, merge, TreeMap::new): without merge, a duplicate key throws.
לפתרוןSolution

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