Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Collections Framework

Similar presentations


Presentation on theme: "Java Collections Framework"— Presentation transcript:

1 Java Collections Framework
The client view

2 The Big Picture

3 Abstract Data Type Operations supported Defined as interfaces, such as
CRUD Membership management Misc. Defined as interfaces, such as Collection List Queue Set Iterable ADT data CRUD

4 List: An ADT We Already Know
Real world example Ways to manipulate Shopping list 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<T> specs in Java API

5 List: The Abstract View
A Sequence of Elements Two popular ways With no rules how they can be accessed To implement a list Using an array: for fast access Using a “link” structure: for easy insertion and deletion (in the middle or anywhere else) 1 2 3 4 5

6 Queue: Another Linear Structure
A Real World Example Ways To Manipulate A service queue A line of customers to be serviced General rule: The head and tail are known First come first serviced or FIFO 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 access Java API is allowed Operations as specified in java.util.Queue<T> interface dequeue head Throws exception Returns special value Insert add(e) offer(e) Remove remove() poll() Examine element() peek() false null NoSuchElementException if queue is empty, or IllegalStateException if size limit reached tail enqueue

8 Stack A real world example Ways 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 push pop top Provided as a class since Java 1.0 Return Type Method boolean empty() E peek() pop() push(E item) int search(Object o) 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: TreeSet<E>, in which elements are ordered using their natural ordering HashSet<E>, for which no iteration ordering guaranteed

10 Map: Collection of K-V Pairs
In the Java API, Map is specified as an interface: An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value. Map<K,V> 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) Key set Value collection

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. 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: This interface requires only one method: iterator(). Implementing this interface allows an object to be the target of the "for-each" statement. Iterable<T> c = …; for (T t : c) System.out.println(t); Iterable<T> c = …; Iterator<T> itr = c.iterator(); while(itr.hasNext()) System.out.println(itr.next()); Iterator used implicitly.

12 <<interface>>
Iterating thru a Map Map is not a subtype of Iterable. Its contents need to be iterated based on Its key set, or Its value collection <<interface>> Iterable values() returns all values in a Collection keySet() returns all keys in a Set

13 Map as a Frequency Table
Map<Character,Integer> 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<Character> 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 Map<K,V> qByCats = new Map<K,V>(); Set<K> cats = qByCats.keySet(); for (K key : cats) System.out.println(key + " --> " qByCats.get(key)); Value 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 A Map Of Lists Display the map contents in the desired way Key Value
Map<K,V> qByCats = new Map<K,V>(); Set<K> 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++) { System.out.print(m.get(k).get(i) '\t'); Key Value A B C D E F A0 B0 C0 D0 E0 F0 A1 B1 C1 D1 E1 F1 A2 B2 C2 D2 E2 F2 A3 B3 C3 D3 E3 F3 A4 B4 C4 D4 E4 F4

16 Sorting w/ a New Order import java.util.Comparator;
for (String k : keys) { System.out.print(k + ‘\t'); Collections.sort(m.get(k), c); } Comparator<String> c = new Comparator<String>() { @Override public int compare(String priceS1, String priceS2) { return Integer.parseInt(priceS1.substring(1)) - Integer.parseInt(priceS2.substring(1)); } }; Sorting w/ a Comparator Format to a fixed length System.out.print( formatToLength(colWidth, k) + ' ');


Download ppt "Java Collections Framework"

Similar presentations


Ads by Google