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

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

מה נעשה היוםToday's plan

  • מונחיםTerms
  • שלושה Sets, שלושה סדריםThree Sets, three orders
  • TreeMap: מפתחות ממוינים וניווטTreeMap: sorted keys and navigation
  • תור, מחסנית ותור עדיפויותQueue, stack and priority queue
  • טעויות נפוצותCommon mistakes
  • פרויקט מדףShelf project
נושאTopic 1

מונחיםTerms

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

תור, מחסנית ותור עדיפויותQueue, stack and priority queue

תור, מחסנית ותור עדיפויותQueue, stack and priority queue

  • אותו ArrayDeque משמש כתור (offer/poll) וכמחסנית (push/pop).The same ArrayDeque serves as a queue (offer/poll) and a stack (push/pop).
  • PriorityQueue מוציא תמיד את הקטן ביותר.A PriorityQueue always takes out the smallest.
נושאTopic 5

טעויות נפוצותCommon mistakes

טעויות נפוצות, ומה Java עונהCommon mistakes, and what Java says

  • java.util.ConcurrentModificationException:java.util.ConcurrentModificationException:
נושאTopic 6

פרויקט מדףShelf project

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

  • מדף, צעד 25: אינדקס מהירShelf, step 25: a fast index

סיבוכיות (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.

שני ה-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.)

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.

אותו ArrayDeque משמש כתור (offer/poll) וכמחסנית (push/pop).The same ArrayDeque serves as a queue (offer/poll) and a stack (push/pop).

PriorityQueue מוציא תמיד את הקטן ביותר.A PriorityQueue always takes out the smallest.

java.util.ConcurrentModificationException:java.util.ConcurrentModificationException:

מדף, צעד 25: אינדקס מהירShelf, step 25: a fast index

צריך לבדוק הרבה פעמים "האם המילה כבר נראתה?". מה הכי מהיר?You need to check "was this word seen already?" many times. What's fastest?

  • ArrayList.containsArrayList.contains
  • HashSet.containsHashSet.contains
  • LinkedList.containsLinkedList.contains
  • אין הבדלNo difference

HashSet.containsHashSet.contains

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]

22

איזה Set ידפיס את האיברים ממוינים?Which Set prints its items sorted?

  • HashSetHashSet
  • LinkedHashSetLinkedHashSet
  • TreeSetTreeSet
  • כולםAll of them

TreeSetTreeSet

דפים ייחודייםUnique pages

  • ברשימת ביקורים home, about, home, shop, about, cart.A list of visits home, about, home, shop, about, cart.

שכיחות ממוינת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:

סוגריים מאוזנים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).

משימות לפי עדיפות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).

צייד באגים: מחיקה תוך כדי מעברBug hunter: removing while looping

  • התוכנית מוחקת מילים קצרות (פחות מ-3 אותיות) וקורסת.The program removes short words (under 3 letters) and crashes.

מדף, צעד 25: אינדקס מהירShelf, step 25: a fast index

  • Index מחזיק שתי מפות:Index holds two maps:

מטמון LRUAn LRU cache

  • class Lru<K, V> extends LinkedHashMap<K, V> עם קיבולת.class Lru<K, V> extends LinkedHashMap<K, V> with a capacity.

השנה הקרובהThe nearest year

  • TreeSet של שנים 1815, 1922, 1949, 1965, 2004.A TreeSet of the years 1815, 1922, 1949, 1965, 2004.

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

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

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

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