Collection
Collection
Introduction
• A container
• Implementations
– Concrete implementations of the collection
interfaces
• Algorithms
– Polymorphic Methods
Benefits
• Conversion constructor
– Collection<String> c;
– List<String> list = new ArrayList<>(c);
The Collection Interface
• Basic operations
– int size(), boolean isEmpty(), boolean contains(Object
element),boolean add(E element), boolean
remove(Object element), and Iterator<E> iterator()
• Bulk operations
– boolean containsAll(Collection<?> c), boolean
addAll(Collection<? extends E> c),boolean
removeAll(Collection<?> c), boolean
retainAll(Collection<?> c), and void clear()
Traversing Collections
• There are three ways to traverse collections
– Using aggregate operations
– With the for-each construct
– By using Iterators
– LinkedHashSet
• A hash table with a linked list running through it
• Orders its elements based on the order in which they
were inserted into the set (insertion-order)
• Class FindDups
• Class FindDups2
– i came i saw i left
The List Interface
• An ordered Collection (also called
sequence)
• May contain duplicate elements
• Additional operations
– Positional access
• Manipulates elements based on their numerical
position in the list
• Such as get, set, add, addAll, and remove
• Search
– Searches for a specified object in the list and
returns its numerical position
– Search methods include indexOf and lastIndexOf
The List Interface
• Iteration
– The listIterator methods
• Range-view
– The sublist method performs arbitrary range
operations on the list
addFirst(e) addLast(e)
Insert
offerFirst(e) offerLast(e)
removeFirst() removeLast()
Remove
pollFirst() pollLast()
getFirst() getLast()
Examine
peekFirst() peekLast()
• Basic operations
– put, get, remove, containsKey, containsValue, size,
and empty
• Bulk operations
– putAll and clear
The Map Interface
• Collection views
– keySet, entrySet, and values
• keySet — the Set of keys contained in the Map.
• values — The Collection of values contained in the Map.
• entrySet — the Set of key-value pairs contained in the
Map.
•
• Three general-purpose Map implementations
– HashMap
– TreeMap
– LinkedHashMap
• Freq
Implementations
• General-purpose
• Special-purpose
• Concurrent implementations
– Part of the java.util.concurrent package
• Wrapper implementations
• Convenience implementations
– Are mini-implementations (for example, singleton
sets)
• Abstract implementations
– Skeletal implementations that facilitate the
construction of custom implementation
Set Implementations
• Grouped into general-purpose and special-
purpose implementations
• General-purpose implementations
– LinkedList
– PriorityQueue
• Concurrent implementations
– BlockingQueue interface
– LinkedBlockingQueue, ArrayBlockingQueue,
– PriorityBlockingQueue, DelayQueue,
– SynchronousQueue
Deque Implementations
• Grouped into general-purpose and
concurrent implementations
• General-purpose implementations
– LinkedList
– ArrayDeque
• Concurrent implementations
– LinkedBlockingDeque
Wrapper Implementations
• Synchronization Wrappers
– public static <T> Collection<T>
synchronizedCollection(Collection<T> c);
– public static <T> Set<T> synchronizedSet(Set<T> s);
– public static <T> List<T> synchronizedList(List<T>
list);
– public static <K,V> Map<K,V>
synchronizedMap(Map<K,V> m);
– public static <T> SortedSet<T>
synchronizedSortedSet(SortedSet<T> s);
– public static <K,V> SortedMap<K,V>
synchronizedSortedMap(SortedMap<K,V> m);
Wrapper Implementations
• Unmodifiable Wrappers
– public static <T> Collection<T>
unmodifiableCollection(Collection<? extends T> c);
– public static <T> Set<T> unmodifiableSet(Set<?
extends T> s);
– public static <T> List<T> unmodifiableList(List<?
extends T> list);
– public static <K,V> Map<K, V>
unmodifiableMap(Map<? extends K, ? extends V> m);
– public static <T> SortedSet<T>
unmodifiableSortedSet(SortedSet<? extends T> s);
– public static <K,V> SortedMap<K, V>
unmodifiableSortedMap(SortedMap<K, ? extends V>
m);
Convenience Implementations
• List<String> list = Arrays.asList(new
String[size]);
• List<Type> list = new
ArrayList<Type>(Collections.nCopies(1000,
(Type)null);
• c.removeAll(Collections.singleton(e));
• emptySet, emptyList, and emptyMap
Custom Collection Implementations
• AbstractCollection
• AbstractSet
• AbstractList
• AbstractSequentialList
• AbstractQueue
• AbstractMap
Algorithms
• Sorting
• Shuffling
• Routine Data Manipulation
• Searching
• Composition
• Finding Extreme Values
Summary
General-purpose Implementations
Hash table +
Hash table Tree Linked list
Resizable array Linked list
Interfaces Implementat Implementatio Implementatio
Implementations Implementatio
ions ns ns
ns
LinkedHashMa
Map HashMap TreeMap
p