Professional Documents
Culture Documents
Collections Framework
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.