info@tutsfinder.in



Java Collection

In java, all dynamically allocated data structures(such as ArrayList, Vector, LinkedList, Stack, Queue,HashSet) are unites in a integrated architecture called as Java collection framework.

Java collection, a name define itself is a collection of interfaces and classes that are used in to manipulate the group of objects.

Java collection interface provides a interface to store, update, delete the data.

To use collection framework we have to import java.util.* package.

Java collection framework package contains-


1. Interfaces-


• Iterable

Iterbale is the root interface of Java collection API. It is used to travers the list and modify the elements .

The Collection interface extends the iterable interface and List , Queue , Set extends the Collection interface so therefore all the sub types of collection implements iterable.

An object of a class that implements the Iterable interface can be used with the Java for-each loop.

Iterable itr = List.Iterator();
while(itr.hasNext()))
{
	System.out.println(itr.next());
}

• Collection

Collection interface defines several interface, methods and classes.

Interfaces - List, Queue, Set, Deque, SortedSet.
Methods - public int size(), public void clear(), public Iterator iterator().
Classes - ArrayList, LinkedList, Vector, Stack, PrirorityQueue, ArrayDeque, HashSet, LinkedHashSet, TreeSet etc.

• List

It contains list type data structure in which data can be store in order collection and also they can contain duplicate values.

Syntax

List <data-type> name-of-list=new ArrayList<data-type>();

• Queue

It is based on first-come-first-serve.It can contain order list of elements.

Classes which implements the Queue interface are PriorityQueue, Deque, and ArrayDeque.

Syntax

Queue <data-type> name-of-list=new PriorityQueue<data-type>();

• Set

Set interface stores data in inordered set of elements and does not contain duplicate items.

Classes which implements Set interface are HashSet, LinkedHashSet, and TreeSet.

Syntax

Set <data-type> name-of-list=new HashSet<data-type>();

• Deque

Deque is double ended queue where can be add and remove from both the ends of queue. Deque extends Queue interface

Syntax

Deque <data-type> name-of-list=new Deque<data-type>();

• SortedSet

It is an ordered version of Set interface where elements are arranged in increasing order.

Syntax

SortedSet <data-type> name-of-list=new TreeSet<data-type>();


2. Class –


• ArrayList

Implements dynamic array that can store duplicate elements of different data types. It maintains insertion order and elements that are stored can be accessed randomly.

• LinkedList

In linked list elements are not stored in contiguous location. It uses doubly linked list to stor data.

Maintains insertion order and store duplicate items.

• Vector

It is same as ArrayList but it is synchronized. Implements dynamic array to store the elements.

• Stack

It is based on last-in-first-out. It is a subclass of vector where stack have same methods of vector as well as its own.

• PrirorityQueue

A PriorityQueue is used when the objects are supposed to be processed based on the priority of elements. Queue follows first-in-first-out.

• ArrayDeque

In ArrayDeque, we can add and remove the data from both the ends. This class implements the Deque interface.

• HashSet

It stores unique elements and does not maintain insertion order, it uses a hash table for storage.

• LinkedHashSet

It provides the facilties of both hash table and linked list data structure. It contains unique elements and maintains insertion order.

• TreeSet

TreeSet stores unique elements in ascending order and it is not thread safe. It stores data in tree format storage.



Program in which we can store all types of data. This will give us an overview how we can implements all data structures.

import java.util.*;
class Collection{
	public static void main(String[] args)
	{
		ArrayList al=new ArrayList(); 
		al.add("Trish");  
		al.add("Rash");

		Iterator itr1=al.iterator();  
		while(itr1.hasNext()){  
			System.out.println(itr1.next());  
		}

		LinkedList li=new LinkedList();  
		li.add("Vinay");  
		li.add("Vikul");

		Iterator itr2=li.iterator();  
		while(itr2.hasNext()){  
			System.out.println(itr2.next());  
		}


		Vector vt=new Vector();  
		vt.add("Vishal");  
		vt.add("Akash");

		Iterator itr3=vt.iterator();  
		while(itr3.hasNext()){  
			System.out.println(itr3.next());  
		}

		Stack st = new Stack();  
		st.push("Deepak");  
		st.push("Mahesh");

		Iterator itr4=st.iterator();  
		while(itr4.hasNext()){  
			System.out.println(itr4.next());  
		}

		PriorityQueue qe=new PriorityQueue();  
		qe.add("Suman");  
		qe.add("Swapnil");

		Iterator itr5=qe.iterator();  
		while(itr5.hasNext()){  
			System.out.println(itr5.next());  
		}
		
		Deque de = new ArrayDeque();  
		de.add("Pinky");  
		de.add("Choti");

		Iterator itr6=de.iterator();  
		while(itr6.hasNext()){  
			System.out.println(itr6.next());  
		}

		HashSet hs=new HashSet();  
		hs.add("Shubham");  
		hs.add("Prashant"); 
		
		Iterator itr7=hs.iterator();  
		while(itr7.hasNext()){  
			System.out.println(itr7.next());  
		}
	
		LinkedHashSet lhs=new LinkedHashSet();  
		lhs.add("Piyush");  
		lhs.add("Rohit");

		Iterator itr8=lhs.iterator();  
		while(itr8.hasNext()){  
			System.out.println(itr8.next());  
		}

		TreeSet ts=new TreeSet();  
		ts.add("Ajju");  
		ts.add("Laddoo");   

		Iterator itr9=ts.iterator();  
		while(itr9.hasNext()){  
			System.out.println(itr9.next());  
		}
	}
}