Collection framework Overview
• The collection is an object that represents a group of objects.
• A collections framework is a unified architecture for representing and
manipulating collections, allowing them to be manipulated
The collection framework consists
• Collection interfaces
• General purpose implementations
• Legacy implementations
• Special purpose implementations
• Concurrent implementations
• Wrapper implementations
• Convenience implementations
• Abstract implementations
• Array utilities
Advantages of collection framework
• Reduces programming effort
• Increases performance
• Provides interoperability between unrelated APIs
• Reduces the effort required to learn APIs
• Reduces the effort required to design and implement APIs
• Fosters software reuse
• The collection interface is the primary which is implemented by all
the collection classes.
• There are totally 9 collection interfaces.
• Some of them are
• List interface
• Set interface
• Sortedset interface
AbstractCollection Implements most of the collections interface
AbstractList Extends AbstractCollection and implements most of
the List interface
AbstractSequentialList Extends AbstractList for use by a collection that uses
sequential rather than random access of its elements
LinkedList Implements a linked list by extending
ArrayList Implements a dynamic array by extending AbstractList
AbstractSet Extends AnstractCollection and Implements most of
the Set interfaces
HashSet Extends AbstractSet for use with a hash table
TreeSet Implements a set stored in a tree. Extends AbstractSet
• The ArrayList class extends AbstractList and implements the List
• The constructors provided by the ArrayList class is
• ArrayList() – empty constructor
• ArrayList(Collection c) - initialized with the elements of the collection c
• ArrayList(int capacity) – specifying the initial capacity
• ArrayList<String> stringList = new ArrayList<String>();
• LinkedList class implements the List interface.
• The LinkedList class provides uniformly named methods to get,
remove and insert an element at the beginning and end of the list.
• LinkedList also can be used as stack, queue, double ended queue
using these operations.
• The LinkedList() and LinkedList(collection c) are the constructors used
in this class.
• LinkedList<type> obj = new LinkedList<type>();
Methods used in LinkedList
Void addFirst(Object ob) To add elements to the start of the list
Void addLast(Object ob) To add elements to the end of the list
Object getFirst() To obtain the first element
Object getLast() To obtain the last element
Object removeFirst() To remove the first element in the list
Object removeLast() To remove the last element in the list
• The TreeSet class implements the set interface, backed by a TreeMap
• TreeSet is an excellent choice when storing large amounts of sorted
information that must be found quickly.
Iterator and ListIterator interface
• The iterator enables you to cycle through a collection obtaining or
• The ListIterator extends Iterator to allow multidirectional traversal of
a list and the modification of the element.
Methods of iterator interface
Boolean hasNext() Returns true if the iteration has more elements
Object next() Returns the next element in the iteration
Void remove() Removes the underlying collection the last element
returned by the iterator
Methods of ListIterator interface
Void add(Object e) Inserts the specified element into the list
Boolean hasNext() Returns true if this list iterator has more elements
when traversing the list in the forward direction
Boolean hasPrevious() Returns true if this list iterator has more elements
when traversing the list in the reverse direction
Object next() Returns the next element in the list
Int nextIndex() Returns the index of the element that would be
returned by a subsequent call to next
Object previous() Returns the previous element in the list
Int previousIndex() Returns the index of the element that would e
returned by a subsequent call to previous
Void remove() Removes from the list the last element that was
returned by next or previous
Void set(Object o) Replaces the last element returned by next or
previous with the specified element
Random Access Interface
• The RandomAccess interface identifies List implementations which
are faster to iterate using the List.get() method rather than using the
Working with maps
• Map interface:
• Map interface is used to map keys to value.
• A map cannot contain duplicate keys, each key can map to at most one value.
• A key is an object that is used to retrieve a value later.
• Given a key and a value, one can store the value in a map object.
• The value can be retrieved using the key.
• The get() and put() are two basic operations in map interface
• The put() is used to put a value into the map
• The get() is used to obtain a value passing the key as an argument
Methods provided by map interface
Void clear() Removes all mappings from this map
Boolean containsKey(Object key) Returns true if the map contains a mapping for the
Boolean containsValue(Object key) Returns true if this map maps one or more keys to the
Set entrySet() Returns a set view of the mappings contained in this
Boolean equals(Object o) Compares the specified object with this map for
Object get(Object key) Returns the value to which this map mpas the
Int hashCode() Returns the hash code value for this map
Boolean isEmpty() Returns true if this map contains no key-value
Set keyset() Returns a set view of the keys contained in this map
Object put(Object key, Object value) Associates the specified value with the specified key in
Void putAll(Map t) Copies all of the mappings from the specified map to
Object remove(Object key) Removes the mappings for this key from this map if it
Int size() Returns number of key-value mappings in this map
Collection values() Returns a collection view of the values contained in
• HashMap class is used for hash table based implementation of the
• This implementation provides all the optional map operations and
permits null values and the null key.
• The HashMap class has two parameters
• Capacity – the capacity is the number of buckets in the hash table
• Load factor – is the measure of how full the hash table is allowed to get
before its capacity is automatically increased