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

Collections Framework

Collection :
Group of objects is known as collection. It is a single entity representing a group of object.
Arrays Vs Collections:
Collections in Java:
 The Collections in Java is a framework which provide a ready-made
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, i.e., a group.Java Collection
framework provides many interfaces and classes.
Advantages of the Collection Framework
Consistency: The Collection framework provides a consistent set of interfaces and classes for
representing and manipulating collections of data, making it easier to work with different
types of collections in a consistent and predictable way.
Reusability: The Collection framework provides a set of common functionalities that can be
reused across multiple applications, reducing the amount of code that needs to be written
from scratch.
Performance: The Collection framework provides optimized implementations of common
functionality, such as searching, sorting, and iterating through collections, which can help to
improve the performance of the software.
Flexibility: The Collection framework provides a wide range of classes and interfaces for
working with different types of collections, such as lists, sets, and maps, making it easy to
choose the right type of collection for a specific use case.
Extensibility: The Collection framework is designed to be extensible, making it easy to create
new classes and interfaces that extend or adapt the existing functionality.
Iteration: The Collection framework provides the Iterator and the ListIterator interfaces which
allows the user to traverse the collection in both the forward and backward direction.
Iterable Interface
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.
Java Collection Interface
The Collection interface is the root interface of the Java collections framework.
There is no direct implementation of this interface. However, it is implemented
through its subinterfaces like List, Set, and Queue.
For example, the ArrayList class implements the List interface which is a
subinterface of the Collection Interface.
Collection Methods :
1.add(Object O)---->add new object to the collection.
2.addAll(Collection C)---->It will add multiple objects to the collection.
3.remove(Object O)---->It will remove single object from the collection.
4.removeAll(Collection C)---->It will remove all objects from the collection.
5.retainAll(Collection C)---->accept the group of objects(whatever we mention)rest of the objects
will be removed.
6.clear()---->It will remove objects from the list.
7.isEmpty()---->It will check the collection is empty or not.
8.size()---->It will give how many objects in the collection.
9.contains(Object O)---->It will check the particular object is in the collection or not.
10.containsAll(Collection C)---->It will check the group of objects are present in the list or not.
11.toArray(Collection C)---->Which will convert the entire collection into array formate.
Note:these methods aplicable for Set,Queue,List interfaces.
(these interfaces extends the collection)
List Interface
The list is a child interface of Collections in java.
Insertion order preserved i.e., They appear in the same order in which we inserted.
Duplicate elements are allowed.
List Interface is implemented by
ArrayList,
LinkedList,
Vector,
and Stack class.
List Methods:
Collection methods also avalible in the list.
In list we have additional methods:
1.add(index,Object O)--->It will add based on index
2.addAll(index,Collection C)--->based on index we can add the group of objects
3.remove(index)--->based on index we can remove the object
4.get(index)--->It will retrieve the object by based on index
5.set(index,Object O)--->It will replace the existing object into the new object
(It updates the value)
ArrayList:
The ArrayList class implements the List interface.
It is an array but there is no size limit.We can add or remove elements easily.
ArrayList allows null values.It allows duplicates.
 ArrayList is not synchronized
 ArrayList maintains the insertion order
 In the ArrayList elements are randomly accesed
Default capacity is 0(Capacity when elements are not added to the list)and initial capacity is
10.
 50% incrementation.
(For example 10 15 22 33)//(10/2)=5+10=15,(15/2)=7+15=22,(22/2)=11+22=33
Declaration of ArrayList:
ArrayList<DataType> VariableName=new ArrayList<DataTYpe>();(Same data type)
ArrayList VariableName= new ArrayList();(different type of data)
Methods in ArrayList:
Sr.no Method Description
1 get(object o) It prints the value at a specific index.
2 set(index, object o) It updates the value. In that, we need to provide an
index.
3 add(index, object o) It adds an element at a specific index.
4 remove(Object o) It removes elements at specific indexes.
5 addAll(Collection c) It is used to add another collection.
6 removeAll(Collection c) It is used to remove another collection.
7 clear() removes all the elements from the list (more
efficient than removeAll())
8 contains() returns true if a list contains specified element
Vector:
vector class implements the List interface.
vector allows the duplicates. It allows the null values.
 It maintains the insertion order.
It is synchronized .The vector object is Thread safe.At a time only one thread can
operate on the Vector object.
performance is low because Threads are needed to wait.
Default and initial capacity 10.
100% incrementation.
(For example 10 20 40 80)//10+10=20,20+20=40,40+40=80
Declaration of Vector:
Vector < DataType> VariableName = new Vector<DataType> ();
Vector VariableName=new Vector();
Methods in vector:
Methods in Vector is same as Arraylist.
Stack:
 It is the child class of Vector.
 Stack allows duplicates
 Stack allows null value
 Stack is synchronized
 It is based on LIFO (Last In First Out) i.e., Element inserted in last will come first.
 This is a legacy class. It is also a thread-safe class.
 100% incrementation.
 Declaration of Stack:
Stack<DataType> VariableName = new Stack< DataType>();
Stack VariableName= new Stack();
Special methods :
push--Specified object into the top of the Stack
pop-- Remove the object from the top of the Stack
peek--Access the top most element from the Stack
LinkedList:
LinkedList class uses a doubly LinkedList to store element. i.e., the user can add
data at the first position as well as the last position.
The dequeue interface is implemented using the LinkedList class.
Null insertion is possible.
It can store the duplicate elements.
If we need to perform insertion /Deletion operation the LinkedList is preferred.
 It maintains the insertion order and is not synchronized.
 In LinkedList, the manipulation is fast because no shifting is required.
Declaration of LinkedList:
LinkedList<DataType> VariableName = new LinkedList < DataType>();
LinkedList VariableName= new LinkedList();
 Singly Linked List: In a singly LinkedList, each node in this
list stores the data of the node and a pointer or reference to
the next node in the list.

 Doubly Linked List: In a doubly LinkedList, it has two


references, one to the next node and another to the previous
node.
Methods in LinkedList:
Some methods in LinkedList are the same as ArrayList.
Other methods in LinkedList are:
• addFirst()
• addLast()
• removeFirst()
• removeLast()
• getFirst()
• getLast()
How does Insertion work in Java LinkedList?
How does Deletion work in Java LinkedList?
Queue Interface:
(If we want to represent a group of elements which are prior to proccessing then
we use this queue)
Queue represents a data structure which follows First-In-First-Out (FIFO)
ordering of elements(which means the elements entered first comes out first).
Queue interface is provided in java. util package and extends the collection
interface.
It allows Diplicate Elements.
The queue is implemented by LinkedList, priority queue , and ArrayDequeue.
PriorityQueue:
 The PriorityQueue class implements the Queue interface.
 It does not maintains the order.
 PriorityQueue is allowed homogeneous data .(LinkedList allows heterogeneous as well as
homogeneous data)
 It doesn't allow null values. It allows duplicates.
 It is not synchronized.
 Default capacity is 11.
Methods:
• offer():It is used to insert data into the queue. If data is not inserted successfully it returns false.
• add():It used to insert data into queue. If data is not inserted successfully it throws an exception.
• element(): Returns the element at the front of the queue without removing it. If the queue is
empty, it throws an exception.
• peek(): It returns head elements from the queue. If Queue is empty it will return Null.
• remove(): It removes an element from the queue. If Queue is empty it will throw exception
NoSuchElementException.
• poll(): It removes the element from the queue. If Queue is empty it will return Null.
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.
 Null elements are not allowed in the dequeue.
ArrayDeque:
ArrayDeque class implements the Deque interface.
Unlike queue, we can add or delete the elements from both the ends.
It maintains the insertion order.
Null elements are not allowed . It allows duplicates.
ArrayDeque is not synchronized.
ArrayDeque is faster than LinkedList .
Default capacity is 17.
Methods:
• offer,offerFirst,offerLast,
• add,addFirst,addLast,addAll,
• peek,peekFirst,peekLast,
• poll,pollFirst,pollLast,
• remove,removeFirst,removeLast
Set Interface:
Set is a child interface of Collection.
 Set doesn’t maintains the order
Duplicate elements are not allowed.
Heterogeneous objects are allowed.
We can store one null value.
 Set is implemented by HashSet, LinkedHashSet, and TreeSet.
Methods:
add(E e):add the specified element if it is not presentthen return false.
clear():remove all the elements from the set.
contains(Object o): return true if an element is present in a set.
remove(Object o):remove the element if it is present in set.
iterator():return an iterator over the element in the set.
isEmpty():check whether the set is empty or not. Returns true for empty and
false for a non-empty condition for set.
size():return the size of the set.
clone():create a shallow copy of the set.
HashSet:
HashSet implemts the Set interface
HashSet deos not allow duplicates
HashSet allows null value
HashSet in not synchronized
HashSet doesn’t maintain order
 Initial default capacity is 16
LinkedHashSet:
LinkedHashSet implemts the Set interface
LinkedHashSet deos not allow duplicates
It allows null value
It is not synchronized
Insertion order is preserved.
 Initial default capacity is 16
SortedSet:
SortedSet is child interface of Set Interface.
If we want to insert unique elements where duplicates are not allowed and all
elements should be inserted according to some sorting order then we should go
for the SortedSet interface.
TreeSet:
TreeSet class implemts the Set interface
TreeSet deos not allow duplicates
TreeSet does not allow null value
TreeSet in not synchronized
TreeSet maintains sorted order
TreeSet initial default capacity is 0
Map Interface:
The Map interface of the Java collections framework provides the functionality
of the map data structure.
A map stores values based on Key and value Pair.
We can access and modify values using the keys associated with them.
Duplicate value of the key is not allowed. (Key must be unique while duplicates
values are allowed)
Methods:
put(K, V) - Inserts the association of a key K and a value V into the map. If the key is already
present, the new value replaces the old value.
putAll() - Inserts all the entries from the specified map to this map.
putIfAbsent(K, V) - Inserts the association if the key K is not already associated with the value V.
get(K) - Returns the value associated with the specified key K. If the key is not found, it returns null.
getOrDefault(K, defaultValue) - Returns the value associated with the specified key K. If the key is
not found, it returns the defaultValue.
containsKey(K) - Checks if the specified key K is present in the map or not.
containsValue(V) - Checks if the specified value V is present in the map or not.
replace(K, V) - Replace the value of the key K with the new specified value V.
replace(K, oldValue, newValue) - Replaces the value of the key K with the new value newValue only
if the key K is associated with the value oldValue.
remove(K) - Removes the entry from the map represented by the key K.
remove(K, V) - Removes the entry from the map that has key K associated with value V.
keySet() - Returns a set of all the keys present in a map.
values() - Returns a set of all the values present in a map.
entrySet() - Returns a set of all the key/value mapping present in a map.
HashMap:
HashMap implements the Map interface.
HashMap store values based on keys.
HashMap contains unique keys.It allows duplicate values.
It maintains sorted order.
HashMap class allows only one null key and multiple null values.
HashMap is not synchronized.
HashMap initial default capacity is 16.
LinkedHashMap:
LinkedHashMap implements the Map interface.
LinkedHashMap store values based on keys.
LinkedHashMap contains unique elements.It allows duplicate values.
It maintains the insertion order.
LinkedHashMap only one null key and multiple null values.
LinkedHashMap is not synchronized.
LinkedHashMap default initial capacity is 16.
TreeMap:
TreeMap implements the SortedMap interface.
TreeMap contains values based on the key.
TreeMap contains only unique elements.It allows duplicate values.
TreeMap maintains sorted order.
TreeMap doesn’t allow null keys and values.
TreeMap is not synchronized.
HashTable:
Hashtable implements Map interface.
HashTable store values based on key-value pair.
It contains unique elements only.It allows duplicate values.
It maintains descending oreder.
Hashtable does not allow null key and value. (otherwise it will throw NullPointerException)
It is synchronized.
Hashtable initial default capacity is 11.

You might also like