נבנה על ידיBuilt by Ariel Yohimovich
JavaJava
Collections לעומקCollections in depth
מפגש 25: List, Set, Map ו-Queue, מימושים שונים, סיבוכיות זמן, ומתי לבחור מה.Session 25: List, Set, Map and Queue, different implementations, time complexity, and when to choose which.
List, Set, Map ו-Queue, מימושים שונים, סיבוכיות זמן, ומתי לבחור מה. בסוף המפגש מוסיפים צעד לפרויקט המלווה.List, Set, Map and Queue, different implementations, time complexity, and when to choose which. At the end we add a step to the companion project.מה נעשה היוםToday's plan
- מונחיםTerms
- שלושה Sets, שלושה סדריםThree Sets, three orders
- TreeMap: מפתחות ממוינים וניווטTreeMap: sorted keys and navigation
- תור, מחסנית ותור עדיפויותQueue, stack and priority queue
- טעויות נפוצותCommon mistakes
- פרויקט מדףShelf project
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.המונחים של המפגשThis session's terms
- סיבוכיות (O גדול): כמה העבודה גדלה כשהאוסף גדל.complexity (big O): How the work grows as the collection grows.
- LinkedHashSet: Set ששומר את סדר ההכנסה.LinkedHashSet: A Set that keeps insertion order.
- TreeSet: Set ממוין תמיד (לפי
Comparable או Comparator), עם ניווט:TreeSet: An always-sorted Set (by Comparable or a Comparator), with navigation: - TreeMap: Map עם מפתחות ממוינים:TreeMap: A Map with sorted keys:
- ArrayDeque: תור דו-כיווני מהיר.ArrayDeque: A fast double-ended queue.
- PriorityQueue: תור ש-
poll שלו מחזיר תמיד את האיבר הקטן ביותר (לפי הסדר הטבעי או Comparator), לא את הראשון שנכנס.PriorityQueue: A queue whose poll always returns the smallest item (by natural order or a Comparator), not the first one in.
לפני הקוד, המילים. כל מונח כאן מקושר גם במילון של הקורס.Before the code, the words. Every term here is also linked in the course glossary.נושאTopic 2
שלושה Sets, שלושה סדריםThree Sets, three orders
שלושה Sets, שלושה סדריםThree Sets, three orders
- שני ה-Sets מוחקים כפולים (
fig מופיע פעם אחת).Both Sets drop duplicates (fig appears once). - LinkedHashSet שומר את סדר ההכנסה, ו-TreeSet ממיין.LinkedHashSet keeps insertion order, and TreeSet sorts.
- (
HashSet רגיל לא מבטיח שום סדר.)(A plain HashSet promises no order at all.)
שני ה-Sets מוחקים כפולים (fig מופיע פעם אחת). LinkedHashSet שומר את סדר ההכנסה, ו-TreeSet ממיין. (HashSet רגיל לא מבטיח שום סדר.)Both Sets drop duplicates (fig appears once). LinkedHashSet keeps insertion order, and TreeSet sorts. (A plain HashSet promises no order at all.)נושאTopic 3
TreeMap: מפתחות ממוינים וניווטTreeMap: sorted keys and navigation
TreeMap: מפתחות ממוינים וניווטTreeMap: sorted keys and navigation
- TreeMap מחזיק את השנים ממוינות.A TreeMap keeps the years sorted.
headMap(1950) נותן את כל מה שלפני 1950, ו-ceilingKey(1900) את המפתח הראשון שגדול או שווה ל-1900.headMap(1950) gives everything before 1950, and ceilingKey(1900) the first key at or above 1900.
TreeMap מחזיק את השנים ממוינות. headMap(1950) נותן את כל מה שלפני 1950, ו-ceilingKey(1900) את המפתח הראשון שגדול או שווה ל-1900.A TreeMap keeps the years sorted. headMap(1950) gives everything before 1950, and ceilingKey(1900) the first key at or above 1900.נושאTopic 4
תור, מחסנית ותור עדיפויותQueue, stack and priority queue
תור, מחסנית ותור עדיפויותQueue, stack and priority queue
אותו ArrayDeque משמש כתור (offer/poll) וכמחסנית (push/pop). PriorityQueue מוציא תמיד את הקטן ביותר.The same ArrayDeque serves as a queue (offer/poll) and a stack (push/pop). A PriorityQueue always takes out the smallest.נושאTopic 5
טעויות נפוצותCommon mistakes
טעויות נפוצות, ומה Java עונהCommon mistakes, and what Java says
java.util.ConcurrentModificationException:java.util.ConcurrentModificationException:
אלה הטעויות שהתרגילים מהסוג "צייד באגים" בנויים סביבן. כל אחת רצה באמת, והפלט הוא מה ש-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
- מדף, צעד 25: אינדקס מהירShelf, step 25: a fast index
Index מחזיק שתי מפות: סופר → רשימת כותרים (TreeMap<String, List<String>>), וז'אנר → כותרים ממוינים (TreeMap<String, Set<String>>). add, byAuthor ו-byGenre (רשימה ריקה כשאין). הדפיסו [Emma, Persuasion], [Children of Dune, Dune], [].Index holds two maps: author → list of titles (TreeMap<String, List<String>>), and genre → sorted titles (TreeMap<String, Set<String>>). add, byAuthor and byGenre (empty when missing). Print [Emma, Persuasion], [Children of Dune, Dune], [].סיבוכיות (O גדול): כמה העבודה גדלה כשהאוסף גדל.complexity (big O): How the work grows as the collection grows.
המונחים של המפגשThis session's termsLinkedHashSet: Set ששומר את סדר ההכנסה.LinkedHashSet: A Set that keeps insertion order.
המונחים של המפגשThis session's termsTreeSet: Set ממוין תמיד (לפי Comparable או Comparator), עם ניווט:TreeSet: An always-sorted Set (by Comparable or a Comparator), with navigation:
המונחים של המפגשThis session's termsTreeMap: Map עם מפתחות ממוינים:TreeMap: A Map with sorted keys:
המונחים של המפגשThis session's termsArrayDeque: תור דו-כיווני מהיר.ArrayDeque: A fast double-ended queue.
המונחים של המפגשThis session's termsPriorityQueue: תור ש-poll שלו מחזיר תמיד את האיבר הקטן ביותר (לפי הסדר הטבעי או Comparator), לא את הראשון שנכנס.PriorityQueue: A queue whose poll always returns the smallest item (by natural order or a Comparator), not the first one in.
המונחים של המפגשThis session's termsשני ה-Sets מוחקים כפולים (fig מופיע פעם אחת).Both Sets drop duplicates (fig appears once).
שלושה Sets, שלושה סדריםThree Sets, three ordersשלושה Sets, שלושה סדריםThree Sets, three orders(HashSet רגיל לא מבטיח שום סדר.)(A plain HashSet promises no order at all.)
שלושה Sets, שלושה סדריםThree Sets, three ordersTreeMap מחזיק את השנים ממוינות.A TreeMap keeps the years sorted.
TreeMap: מפתחות ממוינים וניווטTreeMap: sorted keys and navigationheadMap(1950) נותן את כל מה שלפני 1950, ו-ceilingKey(1900) את המפתח הראשון שגדול או שווה ל-1900.headMap(1950) gives everything before 1950, and ceilingKey(1900) the first key at or above 1900.
TreeMap: מפתחות ממוינים וניווטTreeMap: sorted keys and navigationאותו ArrayDeque משמש כתור (offer/poll) וכמחסנית (push/pop).The same ArrayDeque serves as a queue (offer/poll) and a stack (push/pop).
תור, מחסנית ותור עדיפויותQueue, stack and priority queuePriorityQueue מוציא תמיד את הקטן ביותר.A PriorityQueue always takes out the smallest.
תור, מחסנית ותור עדיפויותQueue, stack and priority queuejava.util.ConcurrentModificationException:java.util.ConcurrentModificationException:
טעויות נפוצות, ומה Java עונהCommon mistakes, and what Java saysמדף, צעד 25: אינדקס מהירShelf, step 25: a fast index
הצעד של היום בפרויקט מדףToday's step in the Shelf projectצריך לבדוק הרבה פעמים "האם המילה כבר נראתה?". מה הכי מהיר?You need to check "was this word seen already?" many times. What's fastest?
ArrayList.containsArrayList.containsHashSet.containsHashSet.containsLinkedList.containsLinkedList.contains- אין הבדלNo difference
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.HashSet.containsHashSet.contains
נכון: O(1) בממוצע.Right: O(1) on average.stack.push(1); stack.push(2); stack.pop() מחזיר?stack.push(1); stack.push(2); stack.pop() returns?
- 11
- 22
- שגיאהAn error
[1, 2][1, 2]
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.22
נכון: אחרון נכנס, ראשון יוצא.Right: last in, first out.איזה Set ידפיס את האיברים ממוינים?Which Set prints its items sorted?
HashSetHashSetLinkedHashSetLinkedHashSetTreeSetTreeSet- כולםAll of them
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.TreeSetTreeSet
נכון.Right.דפים ייחודייםUnique pages
- ברשימת ביקורים
home, about, home, shop, about, cart.A list of visits home, about, home, shop, about, cart.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.שכיחות ממוינתSorted frequencies
- ספרו את המילים של
the cat and the hat and the bat ב-TreeMap (עם merge), הדפיסו את המפה, ואז את המפתח הראשון והאחרון:Count the words of the cat and the hat and the bat in a TreeMap (with merge), print the map, then the first and last keys:
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.סוגריים מאוזניםBalanced brackets
static boolean balanced(String s) בודקת שכל סוגר נסגר בסוג הנכון ובסדר הנכון, בעזרת מחסנית (ArrayDeque).static boolean balanced(String s) checks that every bracket closes with the right kind in the right order, using a stack (ArrayDeque).
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.משימות לפי עדיפותTasks by priority
record Task(String name, int priority) implements Comparable<Task> (1 הכי דחוף).record Task(String name, int priority) implements Comparable<Task> (1 is most urgent).
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.צייד באגים: מחיקה תוך כדי מעברBug hunter: removing while looping
- התוכנית מוחקת מילים קצרות (פחות מ-3 אותיות) וקורסת.The program removes short words (under 3 letters) and crashes.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מדף, צעד 25: אינדקס מהירShelf, step 25: a fast index
Index מחזיק שתי מפות:Index holds two maps:
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מטמון LRUAn LRU cache
class Lru<K, V> extends LinkedHashMap<K, V> עם קיבולת.class Lru<K, V> extends LinkedHashMap<K, V> with a capacity.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.השנה הקרובהThe nearest year
TreeSet של שנים 1815, 1922, 1949, 1965, 2004.A TreeSet of the years 1815, 1922, 1949, 1965, 2004.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מה למדנוWhat we learned
- סיבוכיות (O גדול): כמה העבודה גדלה כשהאוסף גדל.complexity (big O): How the work grows as the collection grows.
- שני ה-Sets מוחקים כפולים (
fig מופיע פעם אחת).Both Sets drop duplicates (fig appears once). - TreeMap מחזיק את השנים ממוינות.A TreeMap keeps the years sorted.
- אותו ArrayDeque משמש כתור (
offer/poll) וכמחסנית (push/pop).The same ArrayDeque serves as a queue (offer/poll) and a stack (push/pop). java.util.ConcurrentModificationException:java.util.ConcurrentModificationException:- מדף, צעד 25: אינדקס מהירShelf, step 25: a fast index
כל אחד אומר דבר אחד שלמד היום.Everyone says one thing they learned today.עכשיו לתרגולNow, practice
6 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית6 exercises and 2 challenges, auto-checked
מפנים לעמוד התרגול.Point to the practice page.