Collection Freamework in java

 

Collection Framework

Initially, collection framework is simply called Java.util package or Collection API. Later on, Sun Microsystem had introduced the collection framework in Java 1.2. It was developed and designed by “Joshua Bloch”.


What is Collection 

- A collection is a group of objects. In Java, these objects are called elements of the collection.


Why Need of Collection 

- A collection in java is a container object that is used for storing multiple homogeneous and heterogeneous, duplicate, and unique elements without any size limitation.



What is a Framework?

-A framework is a set of classes and interfaces which provide a ready-made architecture.


Collections Framework in Java

-A collections framework is a class library to handle groups of objects.

- It allows us to store, retrieve, and update a group of objects.


Collections framework in Java supports two types of containers:

1. One for storing a collection of elements (objects), that is simply called a collection.

2. The other, for storing key/value pairs, which is called a map.



Difference between Arrays & Collections in Java

1. Arrays are fixed in size but collections are growable in nature. We can increase or decrease size.

2. Arrays can store only homogeneous data elements (similar type of data) but collections can hold both homogeneous and heterogeneous elements.

3. Arrays can have both hold primitives and object types but collections can hold only objects but not primitive.


Advantage of Collections Framework in Java

1.The collections framework reduces the development time .

2. It provides useful data structure and interfaces which reduce programming efforts.

3.The size of the container is growable in nature.

4.Increases program speed and quality



Collection Hierarchy in Java


All the interfaces and classes for the collection framework are located in java.util package. The diagram of Java collection hierarchy is shown in the below figure.


e➝ extends, I➝ implements


Map

A Map doesn't allow duplicate keys, but you can have duplicate values. HashMap and LinkedHashMap allow null keys and values, but TreeMap doesn't allow any null key or value.





The Java Collection Framework is a set of interfaces, classes, and algorithms that provides a unified and efficient way to handle collections of objects in Java. It provides implementations of various data structures, such as lists, sets, queues, maps, and more. The framework was designed to be easy to use, flexible, and high-performing.


Here are some key components of the Java Collection Framework:

1. Interfaces:

   - Collection: 

  • The root interface that defines basic operations for working with collections of objects, such as adding, removing, and querying elements.
  • List, Queue, and Set have three components which extends the Collection interface. A map is not inherited by Collection interface.

 - List: 

  • An ordered collection of elements that allows duplicates. Provides positional access to elements.
  • ArrayList, vector, and LinkedList are three concrete subclasses that implement the list interface.

Ex 

List <data-type> list1= new ArrayList();  

List <data-type> list2 = new LinkedList();  

List <data-type> list3 = new Vector();  

List <data-type> list4 = new Stack(); 


- Set:

  • A collection that contains unique elements. Does not allow duplicates.
  • Set interface does not maintain any order
  • HashSet, LinkedHashSet, TreeSet classes implements the set interface and sorted interface extends a set interface.

Ex :

Set<data-type> s1 = new HashSet<data-type>();  

Set<data-type> s2 = new LinkedHashSet<data-type>();  

Set<data-type> s3 = new TreeSet<data-type>();  


- Queue: 

  • A collection that represents a waiting list of objects. Elements are typically processed in a first-in, first-out (FIFO) order.
  •  A queue is an ordered of the homogeneous group of elements in which new elements are added at one end(rear) and elements are removed from the other end(front).

Ex :

Queue<String> q1 = new PriorityQueue();  

Queue<String> q2 = new ArrayDeque();  


- PriorityQueue

  • A PriorityQueue in Java is a queue or collection of elements in which elements are stored in order of their priority.
  • Java PriorityQueue doesn’t permit null elements.
  • PriorityQueue is not synchronized. That means it is not thread-safe.

 

- Deque : 

  • A deque (double-ended queue) is a sub-interface of queue interface. It is usually pronounced “deck”.
  • Java Deque interface orders elements in First In First Out or Last In First Out policy.
  • Deque does not allow to store null elements.
  • Elements can be added  & removed from both ends of the queue.

Ex :

Deque d = new ArrayDeque();  

   

- Map :

  • An object that maps keys to values. Each key must be unique.
  • Map interface is not inherited by the collection interface.
  • Map uses a hashing technique for storing key-value pairs. It doesn’t allow to store the duplicate keys but duplicate values are allowed.
  • HashMap, HashTable, LinkedHashMap, TreeMap classes implements Map interface.
Ex :
Map<String, Integer> map = new HashMap<>();
Map<String, Integer> map = new LinkedHashMap<>();
Map<String, Integer> map = new TreeMap<>();


2. Classes:

   - ArrayList

  •  A resizable array-based implementation of the List interface.
  •  Java ArrayList class can contain duplicate elements.
  • Java ArrayList class maintains insertion order.
  • Java ArrayList class is non synchronized.
  • Java ArrayList allows random access because the array works on an index basis.
  • In ArrayList, manipulation is a little bit slower than the LinkedList in Java .
  • We can not create an array list of the primitive types, such as int, float, char, etc. It is required to use the required wrapper class in such cases. 
  • For example:

ArrayList<int> al = ArrayList<int>(); // does not work  

ArrayList<Integer> al = new ArrayList<Integer>(); // works fine

   - LinkedList: 

  •     A doubly-linked list-based implementation of the List and Queue interfaces.
  •     LinkedList similar as Arraylist.

   - HashSet: 

  • A hash table-based implementation of the Set interface.
  • HashSet stores the elements by using a mechanism called hashing.
  • HashSet contains unique elements only.
  • HashSet allows null value.
  • HashSet class is non synchronized.
  • HashSet doesn't maintain the insertion order. Here, elements are inserted on the basis of their hashcode.
  • HashSet is the best approach for search operations.
  • The initial default capacity of HashSet is 16.

  - LinkedHashSet :
  • Java LinkedHashSet class contains unique elements only like HashSet.
  • Java LinkedHashSet class is non-synchronized.
  • Java LinkedHashSet class maintains insertion order.

   - TreeSet: 

  •  A red-black tree-based implementation of the Set interface, providing sorted sets.
  • Java TreeSet class contains unique elements only like HashSet.
  • Java TreeSet class access and retrieval times are quite fast.
  • Java TreeSet class doesn't allow null elements.
  • Java TreeSet class is non-synchronized.
  • Java TreeSet class maintains ascending order.


 

   - HashMap: 

        -A hash table-based implementation of the Map interface.

        -HashMap is the implementation of Map, but it doesn't maintain any order.  

   - LinkedHashMap :

   - LinkedHashMap is the implementation of Map. It inherits HashMap class. It maintains insertion order.

    - TreeMap :

    -TreeMap is the implementation of Map and SortedMap. It maintains ascending order.

    - A red-black tree-based implementation of the Map interface, providing sorted maps.



The Collection Framework is part of the Java Standard Library and is extensively used in Java programming. It provides a wide range of functionality for working with collections, making it easier to handle data and manipulate objects in Java applications.





Comments

Popular Posts