0% found this document useful (0 votes)
78 views

Collections in Java Are A Framework That Provides Architecture To Store and Manipulate The Group of Objects

The Java Collections framework provides interfaces and classes to store and manipulate groups of objects. It includes the Collection interface and its subinterfaces List, Set, and Queue. List stores ordered elements and allows duplicates. Common List classes are ArrayList and LinkedList. Set stores unique elements. Common Set classes are HashSet, LinkedHashSet, and TreeSet. Queue follows first-in, first-out ordering.

Uploaded by

KARANAM SANTOSH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views

Collections in Java Are A Framework That Provides Architecture To Store and Manipulate The Group of Objects

The Java Collections framework provides interfaces and classes to store and manipulate groups of objects. It includes the Collection interface and its subinterfaces List, Set, and Queue. List stores ordered elements and allows duplicates. Common List classes are ArrayList and LinkedList. Set stores unique elements. Common Set classes are HashSet, LinkedHashSet, and TreeSet. Queue follows first-in, first-out ordering.

Uploaded by

KARANAM SANTOSH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Collections in java are a framework that provides architecture to store and manipulate the group of

objects. It is a part of java.util package.

Java Collections can achieve all the operations that you perform on a data such as searching, sorting,
insertion, manipulation, and deletion.

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

Collection interface

The Collection interface is a member of the Java Collections Framework . It is a part


of java.util package. It is one of the root interfaces of the Collection Hierarchy. The Collection
interface is not directly implemented by any class. However, it is implemented indirectly via its
subtypes or subinterfaces like List, Queue, and Set. 
List interface
The List interface provides a way to store the ordered collection. It is a child interface
of Collection . It is an ordered collection of objects in which duplicate values can be stored.
Since List preserves the insertion order, it allows positional access and insertion of elements.

The List interface is found in the java.util package and inherits the Collection interface. It is a
factory of ListIterator interface. Through the ListIterator, we can iterate the list in forward and
backward directions. The implementation classes of List interface are ArrayList, LinkedList, Stack and
Vector. The ArrayList and LinkedList are widely used in Java programming. 

List Interface
List interface is the child interface of Collection interface. It inhibits a list type data structure in which
we can store the ordered collection of objects. It can have duplicate values.

List interface is implemented by the classes ArrayList, LinkedList, Vector, and Stack.

To instantiate the List interface, we must use :

1. List <data-type> list1= new ArrayList();  
2. List <data-type> list2 = new LinkedList();  
3. List <data-type> list3 = new Vector();  
4. List <data-type> list4 = new Stack();  

ArrayList
The ArrayList class implements the List interface. It uses a dynamic array to store the duplicate
element of different data types. The ArrayList class maintains the insertion order and is non-
synchronized. The elements stored in the ArrayList class can be randomly accessed. Consider the
following example.

import java.util.*;

class TestJavaCollection1{

public static void main(String args[]){

ArrayList<String> list=new ArrayList<String>();//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());

LinkedList
LinkedList implements the Collection interface. It uses a doubly linked list internally to store the
elements. It can store the duplicate elements. It maintains the insertion order and is not
synchronized. In LinkedList, the manipulation is fast because no shifting is required.

Consider the following example.

import java.util.*;

public class TestJavaCollection2

public static void main(String args[])

LinkedList<String> al=new LinkedList<String>();

al.add("Ravi");

al.add("Vijay");

al.add("Ravi");

al.add("Ajay");

Iterator<String> itr=al.iterator();

while(itr.hasNext())

{
System.out.println(itr.next());

Queue Interface
Queue interface maintains the first-in-first-out order. It can be defined as an ordered list that is used
to hold the elements which are about to be processed. There are various classes like PriorityQueue,
Deque, and ArrayDeque which implements the Queue interface.

Queue interface can be instantiated as:

1. Queue<data-type> q1 = new PriorityQueue();  
2. Queue<data-type> q2 = new ArrayDeque();  

import java.util.*;

public class Testcol

public static void main(String args[])

Queue<String> al=new PriorityQueue<String>();

al.add("Ravi");

al.add("Vijay");

al.add("Ravi");

al.add("Ajay");

Iterator<String> itr=al.iterator();

while(itr.hasNext())

System.out.println(itr.next());

}
}

Set Interface
Set Interface in Java is present in java.util package. It extends the Collection interface. It represents
the unordered set of elements which doesn't allow us to store the duplicate items. We can store at
most one null value in Set. Set is implemented by HashSet, LinkedHashSet, and TreeSet.

Set can be instantiated as:

1. Set<data-type> s1 = new HashSet<data-type>();  
2. Set<data-type> s2 = new LinkedHashSet<data-type>();  
3. Set<data-type> s3 = new TreeSet<data-type>();  

HashSet
HashSet class implements Set Interface. It represents the collection that uses a hash table for
storage. Hashing is used to store the elements in the HashSet. It contains unique items.

import java.util.*;

public class Testcol

public static void main(String args[])

Set<String> al=new HashSet<String>();

al.add("Ravi");

al.add("Vijay");

al.add("Ravi");

al.add("Ajay");

Iterator<String> itr=al.iterator();

while(itr.hasNext())

System.out.println(itr.next());

}
}

TreeSet
Java TreeSet class implements the Set interface that uses a tree for storage. Like HashSet, TreeSet
also contains unique elements. However, the access and retrieval time of TreeSet is quite fast. The
elements in TreeSet stored in ascending order.

Consider the following example:

import java.util.*;

public class Testcol

public static void main(String args[])

TreeSet<String> al=new TreeSet<String>();

al.add("Ravi");

al.add("Vijay");

al.add("Ravi");

al.add("Ajay");

Iterator<String> itr=al.iterator();

while(itr.hasNext())

System.out.println(itr.next());

You might also like