Java Collection Framework

Collection Framework provides an architecture to store and manipulate the group of objects.


All the operations that you perform on a data such as searching, sorting, insertion, deletion etc. can be performed by Java Collection Framework.Collection Framework provides an architecture to store and manipulate the group of objects.


Collection simply means a single unit of objects. Collection framework provides many interfaces (Set, List, Queue, Deque etc.) and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet etc).


What is Collection?

Collection represents a single unit of objects i.e. a group.


What is framework?

  • provides readymade architecture.
  • represents set of classes and interface.
  • is optional.

Collection framework

Collection framework represents a unified architecture for storing and manipulating group of object. It has:
  1. Interfaces and its implementations i.e. classes
  2. Algorithm

Hierarchy of Collection Framework


Let us see the hierarchy of collection framework.The java.util package contains all the classes and interfaces for Collection framework.


hierarchy of collection framework


Methods of Collection interface



There are many methods declared in the Collection interface. They are as follows:


No.MethodDescription
1public boolean add(Object element)is used to insert an element in this collection.
2public boolean addAll(collection c)is used to insert the specified collection elements in the invoking collection.
3public boolean remove(Object element)is used to delete an element from this collection.
4public boolean removeAll(Collection c)is used to delete all the elements of specified collection from the invoking collection.
5public boolean retainAll(Collection c)is used to delete all the elements of invoking collection except the specified collection.
6public int size()return the total number of elements in the collection.
7public void clear()removes the total no of element from the collection.
8public boolean contains(object element)is used to search an element.
9public boolean containsAll(Collection c)is used to search the specified collection in this collection.
10public Iterator iterator()returns an iterator.
11public Object[] toArray()converts collection into array.
12public boolean isEmpty()checks if collection is empty.
13public boolean equals(Object element)matches two collection.
14public int hashCode()returns the hashcode number for collection.


Iterator interface



Iterator interface provides the facility of iterating the elements in forward direction only.

Methods of Iterator interface

There are only three methods in the Iterator interface. They are:
  1. public boolean hasNext() it returns true if iterator has more elements.
  2. public object next() it returns the element and moves the cursor pointer to the next element.
  3. public void remove() it removes the last elements returned by the iterator. It is rarely used.

ArrayList class:



  • uses a dynamic array for storing the elements.It extends AbstractList class and implements List interface.
  • can contain duplicate elements.
  • maintains insertion order.
  • not synchronized.
  • random access because array works at the index basis.
  • manipulation slow because a lot of shifting needs to be occurred.

Two ways to iterate the elements of collection:



  1. By Iterator interface.
  2. By for-each loop.


LinkedList class:



  • uses doubly linked list to store the elements. It extends the AbstractList class and implements List and Deque interfaces.
  • can contain duplicate elements.
  • maintains insertion order.
  • not synchronized.
  • No random access.
  • manipulation fast because no shifting needs to be occurred.
  • can be used as list, stack or queue.

LinkedList class in collection framework


List Interface:


List Interface is the subinterface of Collection.It contains methods to insert and delete elements in index basis.It is a factory of ListIterator interface.

Commonly used methods of List Interface:

  1. public void add(int index,Object element);
  2. public boolean addAll(int index,Collection c);
  3. public object get(int Index position);
  4. public object set(int index,Object element);
  5. public object remove(int index);
  6. public ListIterator listIterator();
  7. public ListIterator listIterator(int i);


ListIterator Interface:

ListIterator Interface is used to traverse the element in backward and forward direction.


Commonly used methods of ListIterator Interface:

  1. public boolean hasNext();
  2. public Object next();
  3. public boolean hasPrevious();
  4. public Object previous();


Difference between List and Set:



List can contain duplicate elements whereas Set contains unique elements only.



HashSet class:

  • uses hashtable to store the elements.It extends AbstractSet class and implements Set interface.
  • contains unique elements only.

LinkedHashSet class:

  • contains unique elements only like HashSet. It extends HashSet class and implements Set interface.
  • maintains insertion order.

TreeSet class:

  • contains unique elements only like HashSet. The TreeSet class implements NavigableSet interface that extends the SortedSet interface.
  • maintains ascending order.

Queue Interface:

The Queue interface basically orders the element in FIFO(First In First Out)manner.

Methods of Queue Interface :

  1. public boolean add(object);
  2. public boolean offer(object);
  3. public remove();
  4. public poll();
  5. public element();
  6. public peek();

PriorityQueue class:

The PriorityQueue class provides the facility of using queue. But it does not orders the elements in FIFO manner.

Map Interface



A map contains values based on the key i.e. key and value pair.Each pair is known as an entry.Map contains only unique elements.


Commonly used methods of Map interface:

  1. public Object put(object key,Object value): is used to insert an entry in this map.
  2. public void putAll(Map map):is used to insert the specified map in this map.
  3. public Object remove(object key):is used to delete an entry for the specified key.
  4. public Object get(Object key):is used to return the value for the specified key.
  5. public boolean containsKey(Object key):is used to search the specified key from this map.
  6. public boolean containsValue(Object value):is used to search the specified value from this map.
  7. public Set keySet():returns the Set view containing all the keys.
  8. public Set entrySet():returns the Set view containing all the keys and values.


Entry

Entry is the subinterface of Map.So we will access it by Map.Entry name.It provides methods to get key and value.


Methods of Entry interface:

  1. public Object getKey(): is used to obtain key.
  2. public Object getValue():is used to obtain value.

HashMap class:

  • A HashMap contains values based on the key. It implements the Map interface and extends AbstractMap class.
  • It contains only unique elements.
  • It may have one null key and multiple null values.
  • It maintains no order.

What is difference between HashSet and HashMap?

HashSet contains only values whereas HashMap contains entry(key and value).

LinkedHashMap class:


  • A LinkedHashMap contains values based on the key. It implements the Map interface and extends HashMap class.
  • It contains only unique elements.
  • It may have one null key and multiple null values.
  • It is same as HashMap instead maintains insertion order.

TreeMap class

  • 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.


What is difference between HashMap and TreeMap?

1) HashMap is can contain one null key.TreeMap can not contain any null key.
2) HashMap maintains no order.TreeMap maintains ascending order.

Hashtable

  • 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 implements the Map interface and extends Dictionary class.
  • It contains only unique elements.
  • It may have not have any null key or value.
  • It is synchronized.

What is difference between HashMap and Hashtable?

1) HashMap is not synchronized.Hashtable is synchronized.
2) HashMap can contain one null key and multiple null values.Hashtable cannot contain any null key nor value.


Sorting

We can sort the elements of:
  1. String objects
  2. Wrapper class objects
  3. User-defined class objects

Collections class provides static methods for sorting the elements of collection.If collection elements are of Set type, we can use TreeSet.But We cannot sort the elements of List.Collections class provides methods for sorting the elements of List type elements.

Method of Collections class for sorting List elements

public void sort(List list): is used to sort the elements of List.List elements must be of Comparable type.

Note: String class and Wrapper classes implements the Comparable interface.So if you store the objects of string or wrapper classes, it will be Comparable.


Comparable interface



Comparable interface is used to order the objects of user-defined class.This interface is found in java.lang package and contains only one method named compareTo(Object).It provide only single sorting sequence i.e. you can sort the elements on based on single datamember only.For instance it may be either rollno,name,age or anything else.


Syntax:

public int compareTo(Object obj): is used to compare the current object with the specified object.


We can sort the elements of:
  1. String objects
  2. Wrapper class objects
  3. User-defined class objects

Collections class provides static methods for sorting the elements of collection.If collection elements are of Set type, we can use TreeSet.But We cannot sort the elements of List.Collections class provides methods for sorting the elements of List type elements.

Method of Collections class for sorting List elements

public void sort(List list): is used to sort the elements of List.List elements must be of Comparable type.

Note: String class and Wrapper classes implements the Comparable interface.So if you store the objects of string or wrapper classes, it will be Comparable.


Comparator interface



Comparator interface is used to order the objects of user-defined class.
This interface is found in java.util package and contains 2 methods compare(Object obj1,Object obj2) and equals(Object element).

It provides multiple sorting sequence i.e. you can sort the elements based on any data member. For instance it may be on rollno, name, age or anything else.


Syntax of compare method

public int compare(Object obj1,Object obj2): compares the first object with second object.



Collections class provides static methods for sorting the elements of collection. If collection elements are of Set type, we can use TreeSet. But We cannot sort the elements of List. Collections class provides methods for sorting the elements of List type elements.


Method of Collections class for sorting List elements



public void sort(List list,Comparator c): is used to sort the elements of List by the given comparator.




Leave a Reply

Subscribe to Posts | Subscribe to Comments

About This Site

Howdy! My name is Suersh Rohan and I am the developer and maintainer of this blog. It mainly consists of my thoughts and opinions on the technologies I learn,use and develop with.

Blog Archive

Powered by Blogger.

- Copyright © My Code Snapshots -Metrominimalist- Powered by Blogger - Designed by Suresh Rohan -