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

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

Collections לעומקCollections in depth

List, Set, Map ו-Queue, מימושים שונים, סיבוכיות זמן, ומתי לבחור מה.

List, Set, Map and Queue, different implementations, time complexity, and when to choose which.

או Ctrl+Por Ctrl+P

הנקודות החשובותKey points

  • סיבוכיות (O גדול): כמה העבודה גדלה כשהאוסף גדל.complexity (big O): How the work grows as the collection grows.
  • LinkedHashSet: Set ששומר את סדר ההכנסה.LinkedHashSet: A Set that keeps insertion order.
  • שני ה-Sets מוחקים כפולים (fig מופיע פעם אחת).Both Sets drop duplicates (fig appears once).
  • LinkedHashSet שומר את סדר ההכנסה, ו-TreeSet ממיין.LinkedHashSet keeps insertion order, and TreeSet sorts.
  • 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:

מונחיםTerms

  • סיבוכיות (O גדול)complexity (big O): כמה העבודה גדלה כשהאוסף גדל. contains ב-ArrayList הוא O(n) (עובר על הכול), ב-HashSet O(1) בממוצע, וב-TreeSet O(log n).How the work grows as the collection grows. contains on an ArrayList is O(n) (it scans everything), on a HashSet O(1) on average, and on a TreeSet O(log n).
  • LinkedHashSetLinkedHashSet: Set ששומר את סדר ההכנסה. מהיר כמו HashSet.A Set that keeps insertion order. As fast as a HashSet.
  • TreeSetTreeSet: Set ממוין תמיד (לפי Comparable או Comparator), עם ניווט: floor, ceiling, subSet.An always-sorted Set (by Comparable or a Comparator), with navigation: floor, ceiling, subSet.
  • TreeMapTreeMap: Map עם מפתחות ממוינים: firstKey, lastEntry, headMap(k) (כל מה שקטן מ-k).A Map with sorted keys: firstKey, lastEntry, headMap(k) (everything smaller than k).
  • ArrayDequeArrayDeque: תור דו-כיווני מהיר. כתור: offer ו-poll (ראשון נכנס, ראשון יוצא). כמחסנית: push ו-pop (אחרון נכנס, ראשון יוצא).A fast double-ended queue. As a queue: offer and poll (first in, first out). As a stack: push and pop (last in, first out).
  • PriorityQueuePriorityQueue: תור ש-poll שלו מחזיר תמיד את האיבר הקטן ביותר (לפי הסדר הטבעי או Comparator), לא את הראשון שנכנס.A queue whose poll always returns the smallest item (by natural order or a Comparator), not the first one in.
  • IteratorIterator: עצם שעובר על אוסף: hasNext(), next(), ו-remove() שמוחק בבטחה את האיבר האחרון שהוחזר.An object that walks a collection: hasNext(), next(), and remove() which safely deletes the last item returned.
  • סיבוכיות (O גדול)complexity (big O): כמה העבודה גדלה כשהאוסף גדל. contains ב-ArrayList הוא O(n) (עובר על הכול), ב-HashSet O(1) בממוצע, וב-TreeSet O(log n).How the work grows as the collection grows. contains on an ArrayList is O(n) (it scans everything), on a HashSet O(1) on average, and on a TreeSet O(log n).
  • LinkedHashSetLinkedHashSet: Set ששומר את סדר ההכנסה. מהיר כמו HashSet.A Set that keeps insertion order. As fast as a HashSet.
  • TreeSetTreeSet: Set ממוין תמיד (לפי Comparable או Comparator), עם ניווט: floor, ceiling, subSet.An always-sorted Set (by Comparable or a Comparator), with navigation: floor, ceiling, subSet.
  • TreeMapTreeMap: Map עם מפתחות ממוינים: firstKey, lastEntry, headMap(k) (כל מה שקטן מ-k).A Map with sorted keys: firstKey, lastEntry, headMap(k) (everything smaller than k).
  • ArrayDequeArrayDeque: תור דו-כיווני מהיר. כתור: offer ו-poll (ראשון נכנס, ראשון יוצא). כמחסנית: push ו-pop (אחרון נכנס, ראשון יוצא).A fast double-ended queue. As a queue: offer and poll (first in, first out). As a stack: push and pop (last in, first out).
  • PriorityQueuePriorityQueue: תור ש-poll שלו מחזיר תמיד את האיבר הקטן ביותר (לפי הסדר הטבעי או Comparator), לא את הראשון שנכנס.A queue whose poll always returns the smallest item (by natural order or a Comparator), not the first one in.
  • IteratorIterator: עצם שעובר על אוסף: hasNext(), next(), ו-remove() שמוחק בבטחה את האיבר האחרון שהוחזר.An object that walks a collection: hasNext(), next(), and remove() which safely deletes the last item returned.

Cheatsheet

שלושה Sets, שלושה סדריםThree Sets, three orders
List<String> words = List.of("pear", "fig", "apple", "fig", "kiwi");
Set<String> linked = new LinkedHashSet<>(words);
Set<String> tree = new TreeSet<>(words);
System.out.println(words.size() + " " + linked.size());
System.out.println(linked);
System.out.println(tree);
System.out.println(tree.contains("kiwi"));
TreeMap: מפתחות ממוינים וניווטTreeMap: sorted keys and navigation
TreeMap<Integer, String> byYear = new TreeMap<>();
byYear.put(1965, "Dune");
byYear.put(1815, "Emma");
byYear.put(1922, "Ulysses");
System.out.println(byYear);
System.out.println(byYear.firstKey() + " " + byYear.lastEntry().getValue());
System.out.println(byYear.headMap(1950));
System.out.println(byYear.ceilingKey(1900));
תור, מחסנית ותור עדיפויותQueue, stack and priority queue
Deque<String> queue = new ArrayDeque<>();
queue.offer("Noa");
queue.offer("Avi");
queue.offer("Dana");
System.out.println(queue.poll() + " is served, next " + queue.peek());
Deque<Integer> stack = new ArrayDeque<>();
stack.push(1);
stack.push(2);
stack.push(3);
System.out.println(stack.pop() + " " + stack.pop());

שגיאות שפגשנוErrors we met

שגיאהErrorמה זה אומרWhat it meansמה עושיםWhat to do
ConcurrentModificationExceptionצייד באגים: מחיקה תוך כדי מעברBug hunter: removing while loopingjava.util.ConcurrentModificationException: for-each משתמש ב-Iterator בסתר, ו-words.remove משנה את הרשימה מאחורי הגב שלו.java.util.ConcurrentModificationException: a for-each uses an Iterator behind the scenes, and words.remove changes the list behind its back.

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