Collection Framework

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 25

Java

Collection Framework
Collections 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).
Collections 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 Collection in Java ?
 A Collection represents a single unit of objects, i.e., a group.
Collections in Java (Contd…)

 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
Hierarchy of Collection Framework
Methods of Collection interface
S. No. Method Description

1 public boolean add(E e) It is used to insert an element in this collection.

public boolean It is used to insert the specified collection elements in the


2
addAll(Collection<? extends E> c) invoking collection.
public boolean remove(Object
3 It is used to delete an element from the collection.
element)
public boolean It is used to delete all the elements of the specified
4
removeAll(Collection<?> c) collection from the invoking collection.
default boolean
It is used to delete all the elements of the collection that
5 removeIf(Predicate<? super E>
satisfy the specified predicate.
filter)
public boolean It is used to delete all the elements of invoking collection
6
retainAll(Collection<?> c) except the specified collection.
7 public int size() It returns the total number of elements in the collection.
8 public void clear() It removes the total number of elements from the collection.
public boolean contains(Object
9 It is used to search an element.
element)
public boolean
10 It is used to search the specified collection in the collection.
containsAll(Collection<?> c)
Methods of Collection interface
S. No. Method Description
11 public Iterator iterator() It returns an iterator.
12 public Object[] toArray() It converts collection into array.
It converts collection into array.
Here, the runtime type of the
13 public <T> T[] toArray(T[] a)
returned array is that of the
specified array.
14 public boolean isEmpty() It checks if collection is empty.
It returns a possibly parallel
default Stream<E>
15 Stream with the collection as its
parallelStream()
source.
It returns a sequential Stream with
16 default Stream<E> stream()
the collection as its source.
It generates a Spliterator over the
default Spliterator<E>
17 specified elements in the
spliterator()
collection.
public boolean equals(Object
18 It matches two collections.
element)
It returns the hash code number of
19 public int hashCode()
the collection.
Iterator interface

 Iterator interface provides the facility of iterating the elements


in a forward direction only.
 Methods of Iterator interface

No. Method Description


It returns true if the iterator has
public boolean
1 more elements otherwise it returns
hasNext()
false.
It returns the element and moves
public Object
2 the cursor pointer to the next
next()
element.
It removes the last elements
public void
3 returned by the iterator. It is less
remove()
used.
Iterator interface (Contd…)

 The Iterable interface is the root interface for all the collection
classes. The Collection interface extends the Iterable interface
and therefore all the subclasses of Collection interface also
implement the Iterable interface.
 It contains only one abstract method. i.e.,
 Iterator<T> iterator()
 It returns the iterator over the elements of type T.
Collection Interface

 The Collection interface is the interface which is implemented


by all the classes in the collection framework. It declares the
methods that every collection will have. In other words, we
can say that the Collection interface builds the foundation on
which the collection framework depends.
 Some of the methods of Collection interface are Boolean add
( Object obj), Boolean addAll ( Collection c), void clear(), etc.
which are implemented by all the subclasses of Collection
interface.
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

1.import java.util.*;
 The ArrayList class 2.class TestJavaCollection1{
implements the List interface. 3.public static void main(String args[]){
4.ArrayList<String> list=new ArrayList<String
It uses a dynamic array to
>();//Creating arraylist
store the duplicate element
5.list.add("Ravi");//Adding object in arraylist
of different data types. The 6.list.add("Vijay");
ArrayList class maintains the 7.list.add("Ravi");
insertion order and is non- Output:
8.list.add("Ajay");
synchronized. The elements 9.//Traversing list through Iterator
stored in the ArrayList class Ravi
10.Iterator itr=list.iterator();
Vijay
can be randomly accessed. 11.while(itr.hasNext()){
Ravi
Consider the following 12.System.out.println(itr.next());
Ajay
example. 13.}
14.}
15.}
LinkedList
1.import java.util.*;
2.public class TestJavaCollection2{
 LinkedList implements the 3.public static void main(String args[]){
Collection interface. It uses a 4.LinkedList<String> al=new LinkedList<String>()
doubly linked list internally ;
to store the elements. It can 5.al.add("Ravi");
store the duplicate 6.al.add("Vijay");
elements. It maintains the 7.al.add("Ravi");
8.al.add("Ajay"); Output:
insertion order and is not
9.Iterator<String> itr=al.iterator();
synchronized. In LinkedList, 10.while(itr.hasNext()){ Ravi
the manipulation is fast 11.System.out.println(itr.next()); Vijay
because no shifting is 12.} Ravi
required. 13.} Ajay
14.}
Vector

1.import java.util.*;
 Vector uses a dynamic array 2.public class TestJavaCollection3{
to store the data elements. It 3.public static void main(String args[]){
4.Vector<String> v=new Vector<String>();
is similar to ArrayList.
5.v.add("Ayush");
However, It is synchronized 6.v.add("Amit");
and contains many methods 7.v.add("Ashish");
that are not the part of 8.v.add("Garima");
Collection framework. Output:
9.Iterator<String> itr=v.iterator();
10.while(itr.hasNext()){
Ayush
11.System.out.println(itr.next());
Amit
12.}
Ashish
13.}
Garima
14.}
Stack
1.import java.util.*;
2.public class TestJavaCollection4{
3.public static void main(String args[]){
 The stack is the subclass of 4.Stack<String> stack = new Stack<String>();
Vector. It implements the 5.stack.push("Ayush");
last-in-first-out data 6.stack.push("Garvit");
structure, i.e., Stack. The 7.stack.push("Amit");
stack contains all of the 8.stack.push("Ashish");
methods of Vector class and 9.stack.push("Garima");
10.stack.pop(); Output:
also provides its methods
11.Iterator<String> itr=stack.iterator();
like boolean push(), boolean 12.while(itr.hasNext()){ Ayush
peek(), boolean push(object 13.System.out.println(itr.next()); Garvit
o), which defines its 14.} Amit
properties. 15.} Ashish
16.}
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<String> q1 = new PriorityQueue();
2. Queue<String> q2 = new ArrayDeque();
1.import java.util.*;
2.public class TestJavaCollection5{

PriorityQueue 3.public static void main(String args[]){


4.PriorityQueue<String> queue=new PriorityQueue<Stri
ng>();
5.queue.add("Amit Sharma");
6.queue.add("Vijay Raj");
7.queue.add("JaiShankar");
 The PriorityQueue class 8.queue.add("Raj");
9.System.out.println("head:"+queue.element());
implements the Queue 10.System.out.println("head:"+queue.peek());
interface. It holds the 11.System.out.println("iterating the queue elements:");
elements or objects which 12.Iterator itr=queue.iterator(); Output:
are to be processed by their 13.while(itr.hasNext()){
14.System.out.println(itr.next()); head:Amit
priorities. PriorityQueue 15.} Sharma
doesn't allow null values to 16.queue.remove(); head:Amit
be stored in the queue. 17.queue.poll(); Sharma
iterating the
18.System.out.println("after removing two elements:");
19.Iterator<String> itr2=queue.iterator(); queue elements:
20.while(itr2.hasNext()){ Amit Sharma
21.System.out.println(itr2.next()); Raj
22.} JaiShankar
23.} Vijay Raj
24.} after removing
two elements:
Raj
Deque Interface

 Deque interface extends the Queue interface. In Deque, we can


remove and add the elements from both the side. Deque stands
for a double-ended queue which enables us to perform the
operations at both the ends.
 Deque can be instantiated as:
1. Deque d = new ArrayDeque();
ArrayDeque
1.import java.util.*;
2.public class TestJavaCollection6{
3.public static void main(String[] args) {
 ArrayDeque class 4.//Creating Deque and adding elements
implements the Deque 5.Deque<String> deque = new ArrayDeque<String>(
interface. It facilitates us to );
use the Deque. Unlike 6.deque.add("Gautam"); Output:
queue, we can add or delete 7.deque.add("Karan");
Gautam
the elements from both the 8.deque.add("Ajay"); Karan
ends. 9.//Traversing elements Ajay
10.for (String str : deque) {
 ArrayDeque is faster than 11.System.out.println(str);
ArrayList and Stack and has 12.}
no capacity restrictions. 13.}
14.}
ArrayDeque
1.import java.util.*;
2.public class TestJavaCollection6{
3.public static void main(String[] args) {
 ArrayDeque class 4.//Creating Deque and adding elements
implements the Deque 5.Deque<String> deque = new ArrayDeque<String>(
interface. It facilitates us to );
use the Deque. Unlike 6.deque.add("Gautam"); Output:
queue, we can add or delete 7.deque.add("Karan");
Gautam
the elements from both the 8.deque.add("Ajay"); Karan
ends. 9.//Traversing elements Ajay
10.for (String str : deque) {
 ArrayDeque is faster than 11.System.out.println(str);
ArrayList and Stack and has 12.}
no capacity restrictions. 13.}
14.}
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 1.import java.util.*;
2.public class TestJavaCollection7{
3.public static void main(String args[]){
4.//Creating HashSet and adding elements
 HashSet class implements 5.HashSet<String> set=new HashSet<String>();
Set Interface. It represents 6.set.add("Ravi");
7.set.add("Vijay");
the collection that uses a
8.set.add("Ravi"); Output:
hash table for storage. 9.set.add("Ajay");
Hashing is used to store the 10.//Traversing elements Vijay
elements in the HashSet. It 11.Iterator<String> itr=set.iterator(); Ravi
contains unique items. 12.while(itr.hasNext()){ Ajay
13.System.out.println(itr.next());
14.}
15.}
16.}
LinkedHashSet 1.import java.util.*;
2.public class TestJavaCollection8{
3.public static void main(String args[]){
4.LinkedHashSet<String> set=new LinkedHashS
et<String>();
 LinkedHashSet class 5.set.add("Ravi");
represents the LinkedList 6.set.add("Vijay");
implementation of Set 7.set.add("Ravi");
Interface. It extends the 8.set.add("Ajay");
HashSet class and 9.Iterator<String> itr=set.iterator();
implements Set interface. 10.while(itr.hasNext()){
Like HashSet, It also contains 11.System.out.println(itr.next());
12.}
unique elements. It
13.} Output:
maintains the insertion order 14.}
and permits null elements. Ravi
Vijay
Ajay
SortedSet Interface

 SortedSet is the alternate of Set interface that provides a


total ordering on its elements. The elements of the SortedSet
are arranged in the increasing (ascending) order. The
SortedSet provides the additional methods that inhibit the
natural ordering of the elements.
 The SortedSet can be instantiated as:
 SortedSet<data-type> set = new TreeSet();
1.import java.util.*;
TreeSet 2.public class TestJavaCollection9{
3.public static void main(String args[]){
4.//Creating and adding elements
5.TreeSet<String> set=new TreeSet<String>();
 Java TreeSet class implements 6.set.add("Ravi");
the Set interface that uses a 7.set.add("Vijay");
tree for storage. Like HashSet, 8.set.add("Ravi");
9.set.add("Ajay");
TreeSet also contains unique
10.//traversing elements
elements. However, the access 11.Iterator<String> itr=set.iterator();
and retrieval time of TreeSet is 12.while(itr.hasNext()){
quite fast. The elements in 13.System.out.println(itr.next());
TreeSet stored in ascending 14.}
Output:
order. 15.}
16.} Ajay
Ravi
Vijay

You might also like