חלק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.
הנקודות החשובות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 (figappears 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, andceilingKey(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) (עובר על הכול), ב-HashSetO(1) בממוצע, וב-TreeSetO(log n).How the work grows as the collection grows.containson anArrayListis O(n) (it scans everything), on aHashSetO(1) on average, and on aTreeSetO(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 (byComparableor aComparator), 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:offerandpoll(first in, first out). As a stack:pushandpop(last in, first out). - PriorityQueuePriorityQueue: תור ש-
pollשלו מחזיר תמיד את האיבר הקטן ביותר (לפי הסדר הטבעי או Comparator), לא את הראשון שנכנס.A queue whosepollalways 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(), andremove()which safely deletes the last item returned. - סיבוכיות (O גדול)complexity (big O): כמה העבודה גדלה כשהאוסף גדל.
containsב-ArrayListהוא O(n) (עובר על הכול), ב-HashSetO(1) בממוצע, וב-TreeSetO(log n).How the work grows as the collection grows.containson anArrayListis O(n) (it scans everything), on aHashSetO(1) on average, and on aTreeSetO(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 (byComparableor aComparator), 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:offerandpoll(first in, first out). As a stack:pushandpop(last in, first out). - PriorityQueuePriorityQueue: תור ש-
pollשלו מחזיר תמיד את האיבר הקטן ביותר (לפי הסדר הטבעי או Comparator), לא את הראשון שנכנס.A queue whosepollalways 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(), andremove()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 looping | java.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. |