Java JavaCollectionsFramework BW
Java JavaCollectionsFramework BW
1
Programming
Java Collection Framework
13 March 2019
2
Java Collection Framework
Collection AbstractCollection
Vector Stack
List AbstractList
ArrayList
AbstractSequentialList LinkedList
Deque
SortedMap TreeMap
«interface»
java.util.Iterator<E>
+hasNext(): boolean Returns true if this iterator has more elements to traverse.
+next(): E Returns the next element from this iterator.
+remove(): void Removes the last element obtained using the next method.
6
The Set Interface
«interface»
java.util.Set<E>
What are 3
concrete
class of the
java.util.AbstractSet<E> «interface»
java.util.SortedSet<E>
java.util.HashSet<E> +first(): E
+HashSet()
+HashSet(c: Collection<? extends E>)
+last(): E
+headSet(toElement: E): SortedSet<E>
+tailSet(fromElement: E): SortedSet<E>
Set
interface?
+HashSet(initialCapacity: int)
+HashSet(initialCapacity: int, loadFactor: float)
«interface»
java.util.NavigableSet<E>
java.util.LinkedHashSet<E>
+LinkedHashSet()
+LinkedHashSet(c: Collection<? extends E>)
+pollFirst(): E
+pollLast(): E HashSet
LinkedHash
+lower(e: E): E
+LinkedHashSet(initialCapacity: int)
+higher(e: E):E
+LinkedHashSet(initialCapacity: int, loadFactor: float)
+floor(e: E): E
Set
+ceiling(e: E): E
java.util.TreeSet<E>
+TreeSet()
+TreeSet(c: Collection<? extends E>) TreeSet
+TreeSet(comparator: Comparator<? super E>)
+TreeSet(s: SortedSet<E>)
8
The AbstractSet Class
.add() , addAll()
.remove() , removeAll()
.retainAll()
.contains() – returns boolean
13
LinkedHashSet
«interface»
java.util.Collection<E>
«interface»
java.util.List<E>
+add(index: int, element:E): boolean Adds a new element at the specified index.
+addAll(index: int, c: Collection<? extends E>) Adds all the elements in c to this list at the specified
: boolean index.
+get(index: int): E Returns the element in this list at the specified index.
+indexOf(element: Object): int Returns the index of the first matching element.
+lastIndexOf(element: Object): int Returns the index of the last matching element.
+listIterator(): ListIterator<E> Returns the list iterator for the elements in this list.
+listIterator(startIndex: int): ListIterator<E> Returns the iterator for the elements from startIndex.
+remove(index: int): E Removes the element at the specified index.
+set(index: int, element: E): E Sets the element at the specified index.
+subList(fromIndex: int, toIndex: int): List<E> Returns a sublist from fromIndex to toIndex.
The List Iterator 19
«interface»
java.util.Iterator<E>
«interface»
java.util.ListIterator<E>
«interface»
java.util.Collection<E>
«interface»
java.util.List<E>
java.util.ArrayList<E>
«interface»
java.util.Collection<E>
«interface»
java.util.List<E>
java.util.LinkedList<E>
«interface»
java.util.List<E>
java.util.Vector<E>
+Vector() Creates a default empty vector with initial capacity 10.
+Vector(c: Collection<? extends E>) Creates a vector from an existing collection.
+Vector(initialCapacity: int) Creates a vector with the specified initial capacity.
+Vector(initCapacity:int, capacityIncr: int) Creates a vector with the specified initial capacity and increment.
+addElement(o: E): void Appends the element to the end of this vector.
+capacity(): int Returns the current capacity of this vector.
+copyInto(anArray: Object[]): void Copies the elements in this vector to the array.
+elementAt(index: int): E Returns the object at the specified index.
+elements(): Enumeration<E> Returns an enumeration of this vector.
+ensureCapacity(): void Increases the capacity of this vector.
+firstElement(): E Returns the first element in this vector.
+insertElementAt(o: E, index: int): void Inserts o to this vector at the specified index.
+lastElement(): E Returns the last element in this vector.
+removeAllElements(): void Removes all the elements in this vector.
+removeElement(o: Object): boolean Removes the first matching element in this vector.
+removeElementAt(index: int): void Removes the element at the specified index.
+setElementAt(o: E, index: int): void Sets a new element at the specified index.
+setSize(newSize: int): void Sets a new size in this vector.
+trimToSize(): void Trims the capacity of this vector to its size.
33
The Stack Class
The Stack class represents a last-in-
first-out stack of objects. The
elements are accessed only from the
java.util.Vector<E> top of the stack. You can retrieve,
insert, or remove an element from
the top of the stack.
java.util.Stack<E>
«interface»
java.util.Collection<E>
«interface»
java.util.Queue<E>
«interface»
java.util.Queue<E>
java.util.PriorityQueue<E>
+PriorityQueue() Creates a default priority queue with initial capacity 11.
+PriorityQueue(initialCapacity: int) Creates a default priority queue with the specified initial
capacity.
+PriorityQueue(c: Collection<? extends Creates a priority queue with the specified collection.
E>)
+PriorityQueue(initialCapacity: int, Creates a priority queue with the specified initial
comparator: Comparator<? super E>) capacity and the comparator.
39
PriorityQueue
• Prioritization issues:
• Print jobs – faculty or staff first?
• Emergency room scheduling – fire
(burnt) or broken finger patients first?
• Which product can be discontinued
due to poor sales output?
40
PriorityQueue: methods
Method/ Description
Constructor
PriorityQueue<E>() constructs new empty queue
add(E value) adds value in sorted order
clear() removes all elements
iterator() returns iterator over elements
offer() adds value
peek() returns minimum element
poll() returns/removes minimum element
remove() removes/returns min element
size() number of elements in queue
41
PriorityQueue: Example
The Map Interface 42
Entry
…
.
.
The Map Interface UML Diagram 43
java.util.Map<K, V>
+clear(): void Removes all mappings from this map.
+containsKey(key: Object): boolean Returns true if this map contains a mapping for the
specified key.
+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 Returns the value for the specified key in this map.
+isEmpty(): boolean Returns true if this map contains no mappings.
+keySet(): Set<K> Returns a set consisting of the keys in this map.
+put(key: K, value: V): V Puts a mapping in this map.
+putAll(m: Map): void Adds all the mappings from m to this map.
+remove(key: Object): V Removes the mapping for the specified key.
+size(): int Returns the number of mappings in this map.
+values(): Collection<V> Returns a collection consisting of the values in this map.
Concrete Map Classes 44
«interface»
java.util.Map<K, V>
java.util.LinkedHashMap<K, V>
+LinkedHashMap() java.util.TreeMap<K, V>
+LinkedHashMap(m: Map) +TreeMap()
+LinkedHashMap(initialCapacity: int, +TreeMap(m: Map)
loadFactor: float, accessOrder: boolean) +TreeMap(c: Comparator<? super K>)
HashMap and TreeMap 45