22 Slide
22 Slide
22 Slide
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Objectives
To describe the Java Collections Framework hierarchy (22.1-22.2). To use the common methods defined in the Collection interface for operating sets and lists (22.3). To use the Iterator interface to traverse a collection (22.4). To use the for-each loop to simplify traversing a collection (22.4). To explore how and when to use HashSet (22.4.1), LinkedHashSet (22.4.2), or TreeSet (22.4.3) to store elements. To compare elements using the Comparable interface and the Comparator interface (22.5). To explore how and when to use ArrayList or LinkedList to store elements (22.6). To use the static utility methods in the Collections class for sorting, searching, shuffling lists, and finding the largest and smallest element in collections (22.7). To compare performance of sets and lists (22.8). To distinguish between Vector and ArrayList, and to use the Stack class for creating stacks (22.9). To explore the relationships among Collection, Queue, LinkedList, and PriorityQueue and to create priority queues using the PriorityQueue class (22.10). To tell the differences between Collection and Map, and describe when and how to use HashMap, LinkedHashMap, and TreeMap to store values associated with keys (22.11). To obtain singleton sets, lists, and maps, and unmodifiable sets, lists, and maps, using the static methods in the Collections class (22.12).
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Collection
LinkedList
PriorityQueue
Interfaces
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. Concrete All Classes Abstract Classes rights reserved. 0132130807
Map
AbstractMap
HashMap
LinkedHashMap
Interfaces
Abstract Classes
Concrete Classes
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
The Collection interface is the root interface for manipulating a collection of objects.
Adds a new element o to this collection.
+addAll(c: Collection<? extends E>): boolean Adds all the elements in the collection c to this collection. +clear(): void Removes all the elements from this collection. +contains(o: Object): boolean +containsAll(c: Collection<?>):boolean +equals(o: Object): boolean +hashCode(): int +isEmpty(): boolean +iterator(): Iterator +remove(o: Object): boolean +removeAll(c: Collection<?>): boolean +retainAll(c: Collection<?>): boolean +size(): int +toArray(): Object[] Returns true if this collection contains the element o. Returns true if this collection contains all the elements in c. Returns true if this collection is equal to another collection o. Returns the hash code for this collection. Returns true if this collection contains no elements. Returns an iterator for the elements in this collection. Removes the element o from this collection. Removes all the elements in c from this collection. Retains the elements that are both in c and in this collection. Returns the number of elements in this collection. Returns an array of Object for the elements in this collection.
interface java.util.Iterator<E>
+hasNext(): boolean +next(): E +remove(): void Returns true if this iterator has more elements to traverse. Returns the next element from this iterator. Removes the last element obtained using the next method.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
interface
java.util.Set<E>
java.util.AbstractSet<E> java.util.HashSet<E>
+HashSet() +HashSet(c: Collection<? extends E>) +HashSet(initialCapacity: int) +HashSet(initialCapacity: int, loadFactor: float) +first(): E +last(): E
interface
java.util.SortedSet<E>
interface
java.util.LinkedHashSet<E>
+LinkedHashSet() +LinkedHashSet(c: Collection<? extends E>) +LinkedHashSet(initialCapacity: int) +LinkedHashSet(initialCapacity: int, loadFactor: float)
java.util.NavigableSet<E>
+pollFirst(): E +pollLast(): E +lower(e: E): E +higher(e: E):E +floor(e: E): E +ceiling(e: E): E
java.util.TreeSet<E>
+TreeSet() +TreeSet(c: Collection<? extends E>) +TreeSet(comparator: Comparator<? super E>) +TreeSet(s: SortedSet<E>)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
10
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
11
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
12
TestLinkedHashSet
Run
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
13
14
15
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
16
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
17
18
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
20
interface
java.util.List<E>
+add(index: int, element:E): boolean +addAll(index: int, c: Collection<? extends E>) : boolean +get(index: int): E +indexOf(element: Object): int +lastIndexOf(element: Object): int +listIterator(): ListIterator<E> +listIterator(startIndex: int): ListIterator<E> +remove(index: int): E +set(index: int, element: E): E +subList(fromIndex: int, toIndex: int): List<E> Adds a new element at the specified index. Adds all the elements in c to this list at the specified index. Returns the element in this list at the specified index. Returns the index of the first matching element. Returns the index of the last matching element. Returns the list iterator for the elements in this list. Returns the iterator for the elements from startIndex. Removes the element at the specified index. Sets the element at the specified index. Returns a sublist from fromIndex to toIndex.
21
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
interface
java.util.ListIterator<E>
+add(o: E): void +hasPrevious(): boolean +nextIndex(): int +previous(): E +previousIndex(): int +set(o: E): void Adds the specified object to the list. Returns true if this list iterator has more elements when traversing backward. Returns the index of the next element. Returns the previous element in this list iterator. Returns the index of the previous element. Replaces the last element returned by the previous or next method with the specified element.
22
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
23
java.util.ArrayList
interface
java.util.Collection<E>
interface
java.util.List<E>
java.util.ArrayList<E> +ArrayList() +ArrayList(c: Collection<? extends E>) +ArrayList(initialCapacity: int) +trimToSize(): void Creates an empty list with the default initial capacity. Creates an array list from an existing collection. Creates an empty list with the specified initial capacity. Trims the capacity of this ArrayList instance to be the list's current size.
24
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
java.util.LinkedList
interface
java.util.Collection<E>
interface
java.util.List<E>
java.util.LinkedList<E> +LinkedList() Creates a default empty linked list.
+LinkedList(c: Collection<? extends E>) Creates a linked list from an existing collection. +addFirst(o: E): void Adds the object to the head of this list. +addLast(o: E): void +getFirst(): E +getLast(): E +removeFirst(): E +removeLast(): E Adds the object to the tail of this list. Returns the first element from this list. Returns the last element from this list. Returns and removes the first element from this list. Returns and removes the last element from this list.
25
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
27
SetListPerformanceTest
Run
28
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
+max(c: Collection, c: Comparator): Object Returns the max object using the comparator. +min(c: Collection, c: Comparator): Object Returns the min object using the comparator.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
29
TestCollections
Run
30
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
31
32
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
33
java.util.Vector<E> java.util.Stack<E>
+Stack() +empty(): boolean +peek(): E +pop(): E +push(o: E) : E +search(o: Object) : int
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
34
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
35
interface
java.util.Queue<E>
+offer(element: E): boolean +poll(): E +remove(): E +peek(): E +element(): E Inserts an element to the queue. Retrieves and removes the head of this queue, or null if this queue is empty. Retrieves and removes the head of this queue and throws an exception if this queue is empty. Retrieves, but does not remove, the head of this queue, returning null if this queue is empty. Retrieves, but does not remove, the head of this queue, throwing an exception if this queue is empty.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
36
java.util.PriorityQueue<E>
+PriorityQueue() +PriorityQueue(initialCapacity: int) +PriorityQueue(c: Collection<? extends E>) +PriorityQueue(initialCapacity: int, comparator: Comparator<? super E>) Creates a default priority queue with initial capacity 11. Creates a default priority queue with the specified initial capacity. Creates a priority queue with the specified collection. Creates a priority queue with the specified initial capacity and the comparator.
PriorityQueueDemo
Run
37
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
. .
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
38
+containsValue(value: Object): boolean Returns true if this map maps one or more keys to the specified value. +entrySet(): Set Returns a set consisting of the entries in this map. +get(key: Object): V +isEmpty(): boolean +keySet(): Set<K> +put(key: K, value: V): V +putAll(m: Map): void +remove(key: Object): V +size(): int +values(): Collection<V> Returns the value for the specified key in this map. Returns true if this map contains no mappings. Returns a set consisting of the keys in this map. Puts a mapping in this map. Adds all the mappings from m to this map. Removes the mapping for the specified key. Returns the number of mappings in this map. Returns a collection consisting of the values in this map.
39
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
java.util.AbstractMap<K, V>
interface
java.util.SortedMap<K, V>
+firstKey(): K +lastKey(): K +comparator(): Comparator<? super K>) +headMap(toKey: K): SortedMap +tailMap(fromKey: K): SortedMap
java.util.HashMap<K, V>
+HashMap() +HashMap(m: Map)
java.util.LinkedHashMap<K, V>
+LinkedHashMap() +LinkedHashMap(m: Map) +LinkedHashMap(initialCapacity: int, loadFactor: float, accessOrder: boolean)
java.util.TreeMap<K, V>
+TreeMap() +TreeMap(m: Map) +TreeMap(c: Comparator<? super K>)
40
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
41
LinkedHashMap
LinkedHashMap was introduced in JDK 1.4. It extends HashMap with a linked list implementation that supports an ordering of the entries in the map. The entries in a HashMap are not ordered, but the entries in a LinkedHashMap can be retrieved in the order in which they were inserted into the map (known as the insertion order), or the order in which they were last accessed, from least recently accessed to most recently (access order). The noarg constructor constructs a LinkedHashMap with the insertion order. To construct a LinkedHashMap with the access order, use the LinkedHashMap(initialCapacity, loadFactor, true).
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
42
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Run
44
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
45
Returns a list from an array of objects Overloaded binary search method to search a key in the array of byte, char, short, int, long, float, double, and Object
Overloaded equals method that returns true if a is equal to a2 for a and a2 of the boolean, byte, char, short, int, long, float, and Object type
Overloaded fill method to fill in the specified value into the array of the boolean, byte, char, short, int, long, float, and Object type
Overloaded sort method to sort the specified array of the char, byte, short, int, long, float, double, and Object type
46
TestArrays
Run
47
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807