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

Interface Description Concrete Classes: Summary of Collection and Map Implementations

The summary describes key interfaces and classes for collections and maps in Java, including their purpose, allowed elements, ordering, and underlying data structures. It covers the Collection interface and its extensions Set, SortedSet, List, and Map/SortedMap. Concrete subclasses include HashSet, TreeSet, ArrayList, LinkedList, HashMap, TreeMap, which are based on hash tables, trees, arrays and linked lists. Basic operations like size(), contains(), and iterators are also summarized.

Uploaded by

api-3831588
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views

Interface Description Concrete Classes: Summary of Collection and Map Implementations

The summary describes key interfaces and classes for collections and maps in Java, including their purpose, allowed elements, ordering, and underlying data structures. It covers the Collection interface and its extensions Set, SortedSet, List, and Map/SortedMap. Concrete subclasses include HashSet, TreeSet, ArrayList, LinkedList, HashMap, TreeMap, which are based on hash tables, trees, arrays and linked lists. Basic operations like size(), contains(), and iterators are also summarized.

Uploaded by

api-3831588
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Interface Description Concrete

Classes
Collection A basic interface that defines the normal operations that
allow a collection of objects to be maintained or handled
as a single unit.
Set The Set interface extends the Collection interface to HashSet
LinkedHashSet
represent its mathematical namesake: a set of unique
elements.
SortedSet The SortedSet interface extends the Set interface to TreeSet
provide the required functionality for maintaining a set in
which the elements are stored in some sorted order.
List The List interface extends the Collection interface to ArrayList
maintain a sequence of elements that need not be unique. Vector
LinkedList
Map A basic interface that defines operations for maintaining HashMap
Hashtable
mappings of keys to values. LinkedHashMap

SortedMap Extends the Map interface for maps that maintain their TreeMap
mappings sorted in key order.

By convention, each of the collection implementation classes provides a constructor for


creating a collection based on the elements of another Collection object passed as
argument. This allows the implementation of a collection to be changed by merely
passing the collection to the constructor of the desired implementation. This
interchangeability is also true between Map implementations. But collections and maps
are not interchangeable. Note that a collection (or a map) only stores references to
objects, and not the actual objects.

Summary of Collection and Map Implementations

Concrete Interface Duplicates Ordered/Sorted Methods Data Structures


Collection/Map Called on on Which
Elements Implementation
Is Based
HashSet Set Unique No order equals() Hash table
hashCode()
elements
LinkedHashSet Set Unique Insertion order equals() Hash table and
hashCode()
elements doubly-linked list
TreeSet SortedSet Unique Sorted equals() Balanced tree
Summary of Collection and Map Implementations

Concrete Interface Duplicates Ordered/Sorted Methods Data Structures


Collection/Map Called on on Which
Elements Implementation
Is Based
elements compareTo()

ArrayList List Allowed Insertion order equals() Resizable array


LinkedList List Allowed Insertion order equals() Linked list
Vector List Allowed Insertion order equals() Resizable array
HashMap Map Unique No order equals() Hash table
hashCode()
keys
LinkedHashMap Map Unique Key insertion equals() Hash table and
hashCode()
keys order/Access doubly-linked list
order of entries
Hashtable Map Unique No order equals() Hash table
hashCode()
keys
TreeMap SortedMap Unique Sorted in key equals() Balanced tree
compareTo()
keys order

Basic Operations

The basic operations are used to query a collection about its contents and allow elements
to be added and removed from a collection.

int size()
boolean isEmpty()
boolean contains(Object element)
boolean add(Object element) // Optional
boolean remove(Object element) // Optional

The add() and remove() methods return true if the collection was modified as a result
of the operation.

By returning the value false, the add() method indicates that the collection excludes
duplicates, and that the collection already contains an object equal to the argument object.

The contains() method checks for membership of the argument object in the collection
using object value equality.
Bulk Operations

These operations perform on a collection as a single unit.

boolean containsAll(Collection c)
boolean addAll(Collection c) // Optional
boolean removeAll(Collection c) // Optional
boolean retainAll(Collection c) // Optional
void clear() // Optional

These bulk operations can be used to perform the equivalent of set logic on arbitrary
collections (not just on sets). The containsAll() method returns true if all elements of
the specified collection are also contained in the current collection.

The addAll(), removeAll(), and retainAll() methods are destructive in the sense that
the collection on which they are invoked can be modified.

Array Operations

These operations convert collections to arrays.

Object[] toArray()
Object[] toArray(Object a[])

The first toArray() method returns an array with all the elements of the collection. The
second method stores the elements of a collection into an array of a specified type.

If the given array is big enough, the elements are stored in this array. If there is room to
spare in the array, that is, the length of the array is greater than the number of elements in
the collection, the spare room is filled with null values before the array is returned. If the
array is too small, a new array of the same runtime type and appropriate size is created. If
the runtime type of the specified array is not a supertype of the runtime type of every
element in the collection, an ArrayStoreException is thrown.

Iterators

A collection provides an iterator which allows sequential access to the elements of a


collection. An iterator can be obtained by calling the following method of the
Collection interface:

Iterator iterator()

Returns an object which implements the Iterator interface.

boolean hasNext()
Returns true if the underlying collection still has elements left to return. A future call to
the next() method will return the next element from the collection.

Object next()

Moves the iterator to the next element in the underlying collection, and returns the
current element. If there are no more elements left to return, it throws a
NoSuchElementException.

Object remove()

Removes the element that was returned by the last call to the next() method, from the
underlying collection. Invoking this method results in an IllegalStateException, if the
next() method has not yet been called, or when the remove() method has already been
called after the last call to the next() method. This method is optional for an iterator, that
is, it throws an UnsupportedOperationException if the remove operation is not
supported.

After obtaining the iterator for a collection, the methods provided by the Iterator
interface can be used to systematically traverse the elements of the underlying collection
one by one. Example 11.1 illustrates the use of an iterator.

Using an Iterator

The Collection interface allows arbitrary objects to be added to a collection. When


inserting an element, its reference is upcasted to the type Object. On retrieval, it might
be necessary to downcast the reference value of the object to invoke subtype-specific
behavior.

The concrete collection classes override the toString() method to provide a textual
representation of their contents. The standard textual representation generated by the
toString() method for a collection is where each elementi is the textual representation
generated by the toString() method of the individual elements in the collection. At (2)
and at (7) the toString() method of the collection class is used implicitly to generate a
textual representation for the collection

The majority of the iterators provided in the java.util package are said to be fail-fast.
When an iterator has already been obtained, structurally modifying the underlying
collection by other means will invalidate the iterator. Subsequent use of this iterator will
throw a ConcurrentModificationException. The remove() method of an iterator is
the only recommended way to delete elements from the underlying collection during
traversal with an iterator.

The order in which the iterator will return the elements from an underlying collection
depends on the traversal order supported by the collection. For example, an iterator for a
list will traverse the elements in the sequential order they have in the list, whereas the
traversal order for the elements in an ordinary set is not predetermined. An iterator for a
sorted collection will make the elements available in a given sorted order.

Sets

Unlike other implementations of the Collection interface, implementations of the Set


interface do not allow duplicate elements. This also means that a set can contain at most
one null value. The Set interface does not define any new methods, and its add() and
addAll() methods will not store duplicates. If an element is not currently in the set, two
consecutive calls to the add() method to insert the element will first return true, then
false

You might also like