You are on page 1of 21

COLLECTIONS

INTERFACE IN
JAVA
What is Collections Framework?
•A Collection represents a group of objects. The Collections Framework
provides the functionality to manage collections and always represents a
combined architecture for storing the objects. 

Interfaces: Interface in Java refers to the abstract data types. They allow Java
collections to be manipulated independently from the details of their
representation.

Classes: Classes in Java are the implementation of the collection interface. It


basically refers to the data structures that are used again and again.

Algorithm: Algorithm refers to the methods which are used to perform


operations such as searching and sorting, on objects that implement collection
interfaces.
What is a Collection Interface?

•A collection is a generic interface which provides all the fundamental methods


required to work on collections. 

•Below is the basic collection framework interface hierarchy.


Methods in the Collection Interface
•int size(): Returns the number of elements in a collection.
•boolean isEmpty(): Identifies whether a collection is empty.
•boolean contains(Object element): Checks whether the specified element is
a part of the collection.
• boolean add(E element): Adds the specified element to the collection.
• boolean remove(Object element): Removes one occurrence of the
specified element from the collection. 
• Iterator<E> iterator(): Provides an iterator for the collection.
The List Interface
List interface is the subtype/child interface of the Collection interface. It stores
objects/elements in list type data structure in ordered form and can also store duplicate
values.
Implementation classes for List interface are ArrayList, LinkedList, Vector, and Stack.
For Example: To instantiate List Interface with Implementation classes follow:
ArrayList Class

•The ArrayList implements the List interface which allows duplicate objects


and elements.
•It uses a dynamic array data structure to store objects and elements.
•Maintains the insertion order.
•ArrayList is non-synchronized and its elements/objects can be accessed
randomly.

Syntax:
ArrayList object = new ArrayList ();
LinkedList Class

•LinkedList implements the List interface which allows storing the duplicate
elements.
•Uses a doubly linked list data structure to store elements.
•Maintains the insertion order.
•LinkedList is not synchronized and its manipulation is fast because no
shifting is required.

Syntax:
LinkedList obj= new LinkedList();
Vector Class

•Vector Class implements List interface which uses data structure as a


dynamic array to store the data elements.
•It is similar to the ArrayList class.
•Vector is synchronized.
•It contains many methods that are not the part of Collection Framework.

Syntax:
Vector object = new Vector(size ,
increment);
Stack Class

•The Stack is the subclass of the Vector class.


•Stack implements the Vector data structure with the (LIFO) last-in-first-out.
•It contains all of the methods of the Vector class.
•Stack also provides its methods like boolean push(), boolean
peek(), boolean push(object o), which defines its features.

Syntax:
Stack obj= new Stack();
The Set Interface
•A Set refers to a collection that cannot contain duplicate elements . It is a
generic interface that extends the Collection interface.
•Set is Immutable.
•Ability to compare the contents of two sets accurately.
• Set has its implementation in various classes such as HashSet, TreeSet and
LinkedHashSet.
HashSet class

•Hashset only contain unique elements and implements Set interface.


•HashSet stores elements in random order  

Linked Hashset class


•It is a Hash table and Linked list implementation of the set interface.
• It contains only unique elements like HashSet.
• Linked HashSet also provides all optional set operations and maintains insertion
order. 

TreeSet  class
 TreeSet class implements the Set interface that uses a tree for storage. The objects
of this class are stored in the ascending order. Also, it inherits AbstractSet class
and implements NavigableSet interface. It contains only unique elements like
HashSet. In TreeSet class, access and retrieval time are faster.
The Queue Interface
•The Queue interface provides the functionality to add, remove,
access, and examine the queue elements. Typically, queues implement
a First-In-First-Out(FIFO) behaviour. The Dequeue interface extends
the Queue interface to support double-ended queues.
ArrayList Vector

1) ArrayList is not synchronized. Vector is synchronized.

2) ArrayList increments 50% of Vector increments 100% means


current array size if the number of doubles the array size if the total
elements exceeds from its capacity. number of elements exceeds than its
capacity.

3) ArrayList is not a legacy class. It Vector is a legacy class.


is introduced in JDK 1.2.

4) ArrayList is fast because it is non- Vector is slow because it is


synchronized. synchronized, i.e., in a
multithreading environment, it holds
the other threads in runnable or
non-runnable state until current
thread releases the lock of the
object.

5) ArrayList uses A Vector can use


the Iterator interface to traverse the the Iterator interface
elements. or Enumeration interface to traverse
the elements.
ArrayList LinkedList

1) ArrayList internally uses a dynamic array to LinkedList internally uses a doubly linked list to
store the elements. store the elements.

2) Manipulation with ArrayList is slow because Manipulation with LinkedList is faster than


it internally uses an array. If any element is ArrayList because it uses a doubly linked list, so
removed from the array, all the bits are shifted no bit shifting is required in memory.
in memory.

3) An ArrayList class can act as a list only LinkedList class can act as a list and queue both
because it implements List only. because it implements List and Deque interfaces.

4) ArrayList is better for storing and LinkedList is better for manipulating data.


accessing data.
HashMap Hashtable
1) HashMap is non synchronized. It is Hashtable is synchronized. It is thread-
not-thread safe and can't be shared safe and can be shared with many
between many threads without proper threads.
synchronization code.
2) HashMap allows one null key and Hashtable doesn't allow any null key or
multiple null values. value.
3) HashMap is a new class introduced in Hashtable is a legacy class.
JDK 1.2.
4) HashMap is fast. Hashtable is slow.
5) We can make the HashMap as Hashtable is internally synchronized and
synchronized by calling this code can't be unsynchronized.
Map m =
Collections.synchronizedMap(hashMap)
;
6) HashMap is traversed by Iterator. Hashtable is traversed by Enumerator
and Iterator.
7) Iterator in HashMap is fail-fast. Enumerator in Hashtable is not fail-fast.

8) HashMap inherits AbstractMap class. Hashtable inherits Dictionary class.


Property HashMap TreeMap LinkedHashMap
no guaranteed order,
sorted according to
Iteration Order will remain constant insertion-order
the natural ordering
over time
Get / put / remove
O(1) O(log(n)) O(1)
/ containsKey
NavigableMap, Map,
Interfaces Map Map
SortedMap
Null values/keys allowed only values allowed
Fail-fast behavior of Fail-fast behavior of Fail-fast behavior of
an iterator cannot be an iterator cannot be an iterator cannot be
guaranteed, guaranteed, guaranteed,
impossible to make impossible to make impossible to make
Fail-fast behavior any hard guarantees any hard guarantees any hard guarantees
in the presence of in the presence of in the presence of
unsynchronized unsynchronized unsynchronized
concurrent concurrent concurrent
modification modification modification
double-linked
Implementation buckets Red-Black Tree
buckets
implementation is implementation is implementation is
Is synchronized
not synchronized not synchronized not synchronized
•All offer a key->value map and a way to iterate through the keys. The
most important distinction between these classes is the time guarantees and
the ordering of the keys.
•All three classes HashMap, TreeMap and LinkedHashMap implements 
java.util.Map interface, and represents mapping from unique key to
values.
 
Key Points
 
1.HashMap: HashMap offers 0(1) lookup and insertion. If you iterate
through the keys, though, the ordering of the keys is essentially arbitrary. It
is implemented by an array of linked lists. 
Syntax: 
 
public class HashMap extends AbstractMap implements
Map,Cloneable, Serializable
 
•A HashMap contains values based on the key.
•It contains only unique elements.
•It may have one null key and multiple null values.
•It maintains no order.
2. LinkedHashMap: LinkedHashMap offers 0(1) lookup and insertion. Keys are
ordered by their insertion order. It is implemented by doubly-linked buckets. 
Syntax: 
 
public class LinkedHashMap extends HashMap implements Map
 
•A LinkedHashMap contains values based on the key.
•It contains only unique elements.
•It may have one null key and multiple null values.
•It is same as HashMap instead maintains insertion order.
3.TreeMap: TreeMap offers O(log N) lookup and insertion. Keys are ordered, so if
you need to iterate through the keys in sorted order, you can. This means that keys
must implement the Comparable interface. TreeMap is implemented by a Red-Black
Tree. 
Syntax: 
 
public class TreeMap extends AbstractMap implements
NavigableMap, Cloneable, Serializable
 
•A TreeMap contains values based on the key. It implements the NavigableMap
interface and extends AbstractMap class.
•It contains only unique elements.
•It cannot have null key but can have multiple null values.
•It is same as HashMap instead maintains ascending order(Sorted using the natural
order of its key).
4. Hashtable: “Hashtable” is the generic name for hash-based maps. 
Syntax: 
public class Hashtable extends Dictionary implements Map,
Cloneable, Serializable
 
•A Hashtable is an array of list. Each list is known as a bucket. The position of bucket
is identified by calling the hashcode() method. A Hashtable contains values based on
the key.
•It contains only unique elements.
•It may have not have any null key or value.
•It is synchronized.
•It is a legacy class.

You might also like