What is Collection in Java? What is a framework in Java? Need for Collection Framework ?Advantages of Collection Framework

0
1375
What is Collection in Java
The Collection in Java is a framework that provides an architecture to store and manipulate the group of objects. Java Collections can achieve all the operations that you perform on a data such as searching, sorting, insertion, manipulation, and deletion. Java Collection means a single unit of objects. Java Collection framework provides many interfaces (Set, List, Queue, Deque) and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet).

What is a framework in Java

  • It provides readymade architecture.
  • It represents a set of classes and interfaces.
  • It is optional.

What is Collection Framework
The Collection framework represents a unified architecture for storing and manipulating a group of objects. It has:

    1. Interfaces and its implementations, i.e., classes
    2. Algorithm

The Collection interface (java.util.Collection) and Map interface (java.util.Map) are the two main “root” interfaces of Java collection classes.

Need for Collection Framework :
  • Arrays are not resizeable.
  • Java Collections Framework provides lots of different useful data types, such as linked lists (allows insertion anywhere in constant time), resizeable array lists (like Vector but cooler), red-black trees, hash-based maps (like Hashtable but cooler).
  • Java Collections Framework provides abstractions, so you can refer to a list as a List, whether backed by an array list or a linked list; and you can refer to a map/dictionary as a Map, whether backed by a red-black tree or a hashtable. 
Advantages of Collection Framework:
  1. Consistent API : The API has a basic set of interfaces like Collection, Set, List, or Map. All classes (ArrayList, LinkedList, Vector, etc) that implement these interfaces have some common set of methods.
  2. Reduces programming effort: A programmer doesn’t have to worry about the design of Collection, and he can focus on its best use in his program.
  3. Increases program speed and quality: Increases performance by providing high-performance implementations of useful data structures and algorithms.
Hierarchy of Collection Framework:

 Collection : Root interface with basic methods like add(), remove(), contains(), isEmpty(), addAll(), … etc.  Set : Doesn’t allow duplicates. Example implementations of Set interface are HashSet (Hashing based) and TreeSet (balanced BST based). Note that TreeSet implements SortedSet.   List : Can contain duplicates and elements are ordered. Example implementations are LinkedList (linked list based) and ArrayList (dynamic array based)  Queue : Typically order elements in FIFO order except exceptions like PriorityQueue.   Deque : Elements can be inserted and removed at both ends. Allows both LIFO and FIFO.  Map : Contains Key value pairs. Doesn’t allow duplicates.  Example : implementation are HashMap and TreeMap.
TreeMap implements SortedMap. 
  

The difference between Set and Map interface is that in Set we have only keys, whereas in Map, we have key, value pairs.


Examples:

  1. ArrayList

import java.util.*;
class TestJavaCollection1
{
public static void main(String args[])
{
ArrayList list=new ArrayList();//Creating arraylist                            list.add(“Ravi”);//Adding object in arraylist
list.add(“Vijay”);
list.add(“Ravi”);
list.add(“Ajay”);   //Traversing list through Iterator
Iterator itr=list.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
2) LinkedList import java.util.*;
public class TestJavaCollection2
{
public static void main(String args[])
{
LinkedList al=new LinkedList();
al.add(“Ravi”);
al.add(“Vijay”);
al.add(“Ravi”);
al.add(“Ajay”);
Iterator itr=al.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}   3) TreeSet import java.util.*;
public class TestJavaCollection9
{
public static void main(String args[])
{
//Creating and adding elements
TreeSet set=new TreeSet();
set.add(“Ravi”);
set.add(“Vijay”);
set.add(“Ravi”);
set.add(“Ajay”);   //traversing elements
Iterator itr=set.iterator();
while(itr.hasNext())
{   System.out.println(itr.next());
}
}
}