Download presentation
Presentation is loading. Please wait.
Published byMary Payne Modified over 8 years ago
1
Java Collections Framework The client view
2
The Big Picture
3
Abstract Data Type Operations supported o CRUD o Membership management o Misc. Defined as interfaces, such as o Collection o List o Queue o Set o Iterable ADT data CRUD
4
List: An ADT We Already Know Real world exampleWays to manipulate Shopping list o Elements listed in a certain order (position can be labelled with an index) Add, remove, or modify anywhere in the list Order in the list matters Know what’s on the list or not Know count of entries on the list List specs in Java API
5
List: The Abstract View A Sequence of ElementsTwo popular ways With no rules how they can be accessed To implement a list o Using an array: for fast access o Using a “link” structure: for easy insertion and deletion (in the middle or anywhere else) 0 1 2 3 4 5
6
Queue: Another Linear Structure A Real World ExampleWays To Manipulate A service queue o A line of customers to be serviced General rule: o The head and tail are known o First come first serviced or FIFO o Size of the queue may be limited Add: only to the back (or tail) enqueue Remove: only from the front (or head) dequeue
7
Queue: The Abstract View No arbitrary accessJava API is allowed Operations as specified in java.util.Queue interface head tail dequeue enqueue Throws exception Returns special value Insertadd(e)offer(e) Removeremove()poll() Examineelement()peek() NoSuchElementException if queue is empty, or IllegalStateException if size limit reached false null
8
Stack A real world exampleWays To Manipulate A pile of rings that can only be moved (on or off) from the top LIFO: It’s opposite to the order enforced by queue top pop push Return TypeMethod booleanemptyempty() Epeekpeek() Epoppop() Epushpush(E item)E intsearchsearch(Object o)Object Provided as a class since Java 1.0a class since Java 1.0 Index is 1 -based
9
Set: Unordered But No-duplicates Mathematically, a set is defined as “A collection that contains no duplicate elements”, and ordering is not of concern. Two implementing classes often used in Java API:often used in Java API -TreeSet, in which elements are ordered using their natural orderingnatural ordering -HashSet, for which no iteration ordering guaranteed
10
Map: Collection of K-V Pairs In the Java API, Map is specified as an interface: o An object that maps keys to values. o A map cannot contain duplicate keys; o each key can map to at most one value. Map Key set Value collection Map.Entry The CRUD Operations C put(K key, V value) : insert a new entry when the key is not in the Map yet R get(Object key) U put(K key, V value) : replace the value associate with an existing key to a new value D remove(Object key)
11
Iterable: Super-Interface of All All Collection objects need to provide a way to guide its client to iterate (or visit each element just once) through its collection. o Made available by implementing the iterator() method as specified in the Collection interface since Java 1.2. Iterable, a super interface to Collection is added in Java 1.5: o This interface requires only one method: iterator(). o Implementing this interface allows an object to be the target of the "for- each" statement. Iterable c = …; for (T t : c) System.out.println(t); Iterable c = …; Iterator itr = c.iterator(); while(itr.hasNext()) System.out.println(itr.next()); Iterator used implicitly.
12
Iterating thru a Map Map is not a subtype of Iterable. Its contents need to be iterated based on o Its key set, or o Its value collection > Iterable keySet() returns all keys in a Set values() returns all values in a Collection
13
Map as a Frequency Table Map freqTbl = TreeMap(); String text = …; Character ch; for (int i=0; i<text.length(); i++) { ch = text.charAt(i); if (freqTbl.get(ch) == null) freqTbl.put(ch, 1); else freqTbl.put(ch, freqTbl.get(ch) + 1); } Set chs = freqTbl.keySet(); for (Character c : chs) System.out.println(c + " --> " + freqTbl.get(c)); }
14
A Map Of Lists In the Jeopardy! game, a map can be used to relate category names to the lists of questions Key Value Map qByCats = new Map (); Set cats = qByCats.keySet(); for (K key : cats) System.out.println(key + " --> " + qByCats.get(key)); A --> [A0, A1, A2, A3, A4] B --> [B0, B1, B2, B3, B4] C --> [C0, C1, C2, C3, C4] D --> [D0, D1, D2, D3, D4] E --> [E0, E1, E2, E3, E4] F --> [F0, F1, F2, F3, F4]
15
Key Value A Map Of Lists Display the map contents in the desired way ABCDEF A0B0C0D0E0F0 A1B1C1D1E1F1 A2B2C2D2E2F2 A3B3C3D3E3F3 A4B4C4D4E4F4 Map qByCats = new Map (); Set cats = qByCats.keySet(); //display category names in a row for (String k : keys) { System.out.print(k + '\t'); } System.out.println(); //list as a column beneath category for (int i=0; i<5; i++) { for (String k : keys) { System.out.print(m.get(k).get(i) + '\t'); } System.out.println(); }
16
Sorting w/ a New Order Comparator c = new Comparator () { @Override public int compare(String priceS1, String priceS2) { return Integer.parseInt(priceS1.substring(1)) - Integer.parseInt(priceS2.substring(1)); } }; for (String k : keys) { System.out.print(k + ‘\t'); Collections.sort(m.get(k), c); } System.out.print( formatToLength(colWidth, k) + ' '); Sorting w/ a Comparator Format to a fixed length import java.util.Comparator;
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.