0% found this document useful (0 votes)
22 views

Collection

The document discusses collections frameworks and their benefits. It describes common collection interfaces like Collection, Set, List, Queue, Deque, and Map. It also discusses implementations of these interfaces and how collections can be traversed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Collection

The document discusses collections frameworks and their benefits. It describes common collection interfaces like Collection, Set, List, Queue, Deque, and Map. It also discusses implementations of these interfaces and how collections can be traversed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Collection

Introduction
• A container

• An object that groups multiple elements into a


single unit

• A collections framework is a unified


architecture for representing and manipulating
collections
Collections Framework
• Interfaces
– Abstract data types

• Implementations
– Concrete implementations of the collection
interfaces

• Algorithms
– Polymorphic Methods
Benefits

• Reduces programming effort

• Increases program speed and quality


• Allows interoperability among unrelated APIs

• Reduces effort to learn and to use new APIs


• Reduces effort to design new APIs
• Fosters software reuse
Interfaces
Interfaces
• Collection
– The root of the collection hierarchy
– No direct implementations of this interface
• Set
– Cannot contain duplicate elements
• List
– An ordered collection
– Also sometimes called a sequence
– Can contain duplicate elements
Interfaces
• Queue
• Deque
– Can be used both as FIFO (first-in, first-out) and
LIFO (last-in, first-out
• Map
– Maps keys to values
– Cannot contain duplicate keys
– Each key can map to at most one value

• SortedSet & SortedMap are sorted versions of


Set and Map
The Collection Interface
• Used to pass around collections of objects
where maximum generality is desired

• 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

for (Object o : collection)


System.out.println(o);

for (Iterator<?> it = c.iterator(); it.hasNext(); )


if (!cond(it.next()))
it.remove();
Traversing Collections
• Use Iterator instead of the for-each construct
when you need to:
– Remove the current element
– Iterate over multiple collections in parallel

• Collection Interface Array Operation


– Object[] a = c.toArray();
The Set Interface
• Cannot contain duplicate elements

• Contains only methods inherited from


Collection and adds the restriction

• Three general-purpose Set implementations


– HashSet
• Stores its elements in a hash table
• Is the best-performing implementation
• No guarantees concerning the order of iteration
The Set Interface
– TreeSet
• Stores its elements in a red-black tree
• Orders its elements based on their values
• Substantially slower

– 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

• Two general-purpose List implementations


– ArrayList
• The better-performing implementation
– LinkedList
• Better performance under certain circumstances
The List Interface
• List Algorithms
– Sort — Using a merge sort, a stable
– shuffle — randomly permutes the elements
– reverse — reverses the order of the
– rotate — rotates all the elements in a List by a
specified distance
– swap — swaps the elements at specified positions
– replaceAll — replaces all occurrences of one
specified value with another.
– fill — overwrites every element in a List with the
specified value.
– copy — copies the source List into the destination
List
The List Interface
• List Algorithms
– binarySearch — searches for an element in an
ordered List using the binary search algorithm
– indexOfSubList — returns the index of the first
sublist of one List that is equal to another
– lastIndexOfSubList — returns the index of the last
sublist of one List that is equal to another
The Queue Interface
• Each Queue method exists in two forms:
– Throws an exception if the operation fails
– Returns a special value if the operation fails (either
null or false, depending on the operation)
Queue Interface Structure
Type of Operation Throws exception Returns special value
Insert add(e) offer(e)
Remove remove() poll()
Examine element() peek()
The Queue Interface
• There are two general-purpose Queue
implementations
– LinkedList
• Is also a List implementation
– PriorityQueue

• LinkedList provides FIFO semantics


• PriorityQueue orders its elements according
to their values
The Deque Interface
Deque Methods

Type of Operation First Element (Beginning of Last Element (End of


the Deque instance) the Deque instance)

addFirst(e) addLast(e)
Insert
offerFirst(e) offerLast(e)

removeFirst() removeLast()
Remove
pollFirst() pollLast()

getFirst() getLast()
Examine
peekFirst() peekLast()

There are two general-purpose implementations


• LinkedList
• ArrayDeque
The Map Interface
• A Map is an object that maps keys to values
• Cannot contain duplicate keys
• Each key can map to at most one value

• 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 Set implementations


– HashSet
– TreeSet
– LinkedHashSet.

• Special-purpose Set implementations


– EnumSet
– CopyOnWriteArraySet
List Implementations
• Grouped into general-purpose and special-
purpose implementations

• General-purpose List implementations


– ArrayList
– LinkedList

• Special-purpose List implementations


– CopyOnWriteArrayList
Map Implementations
• Grouped into general-purpose and special-
purpose and concurrent implementations

• General-purpose Map implementations


– HashMap, TreeMap, LinkedHashMap
• Special-purpose Map implementations
– EnumMap, WeakHashMap and IdentityHashMap
• Concurrent Map implementations
– java.util.concurrent
– ConcurrentMap interface
– ConcurrentHashMap implementation
Queue Implementations
• Grouped into general-purpose and
concurrent 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

Set HashSet   TreeSet   LinkedHashSet

List   ArrayList   LinkedList  

Queue    PriorityQueue    LinkedList  

Deque   ArrayDeque   LinkedList  

LinkedHashMa
Map HashMap   TreeMap  
p

You might also like