OOP Unit 10 Notes
OOP Unit 10 Notes
Let us see the hierarchy of Collection framework. The java.util package contains all
the classes and interfaces for the Collection framework.
1
COLLECTIONS FRAMEWORK
The Java collections framework provides various interfaces. These interfaces include
several methods to perform different operations on collections.
The Collection interface is the root interface of the collections framework hierarchy.
Java does not provide direct implementations of the Collection interface but provides
implementations of its subinterfaces like List, Set, and Queue .
2
COLLECTIONS FRAMEWORK
The List interface is an ordered collection that allows us to add and remove elements
like an array.
Set Interface
The Set interface allows us to store elements in different sets similar to the set in
mathematics. It cannot have duplicate elements.
Queue Interface
The Queue interface is used when we want to store and access elements in First In,
First Out manner.
Methods of Collection
The Collection interface includes various methods that can be used to perform
different operations on objects. These methods are available in all its subinterfaces.
add() - inserts the specified element to the collection
size() - returns the size of the collection
remove() - removes the specified element from the collection
iterator() - returns an iterator to access elements of the collection
addAll() - adds all the elements of a specified collection to the collection
removeAll() - removes all the elements of the specified collection from the
collection
clear() - removes all the elements of the collection
3
COLLECTIONS FRAMEWORK
In Java, the Map interface allows elements to be stored in key/value pairs. Keys are
unique names that can be used to access a particular element in a map. And, each
key has a single value associated with it.
In Java, the Iterator interface provides methods that can be used to access elements
of collections.
The Java collections framework provides various data structures and algorithms that
can be used directly. This has two main advantages:
We do not have to write code to implement these data structures and algorithms
manually.
Our code will be much more efficient as the collections framework is highly
optimized.
Moreover, the collections framework allows us to use a specific data structure for a
particular type of data. Here are a few examples,
If we want our data to be unique, then we can use the Set interface provided
by the collections framework.
To store data in key/value pairs, we can use the Map interface.
The ArrayList class provides the functionality of resizable arrays.
4
COLLECTIONS FRAMEWORK
The ArrayList class allows us to create resizable arrays. The class implements
the List interface (which is a subinterface of the Collection interface).
class Main {
public static void main(String[] args){
ArrayList<String> animals = new ArrayList<>();
// Add elements
animals.add("Dog");
animals.add("Cat");
animals.add("Horse");
class Main {
public static void main(String[] args) {
ArrayList<String> animals = new ArrayList<>();
5
COLLECTIONS FRAMEWORK
In the above example, we have used the get() method with parameter 1. Here, the
method returns the element at index 1.
Java Vector
The Vector class is an implementation of the List interface that allows us to create
resizable-arrays similar to the ArrayList class.
In Java, both ArrayList and Vector implements the List interface and provides the same
functionalities. However, there exist some differences between them.
The Vector class synchronizes each individual operation. This means whenever we
want to perform some operation on vectors, the Vector class automatically applies a
lock to that operation.
It is because when one thread is accessing a vector, and at the same time another
thread tries to access it, an exception called ConcurrentModificationException is
generated. Hence, this continuous use of lock for each operation makes vectors less
efficient.
However, in array lists, methods are not synchronized. Instead, it uses
the Collections.synchronizedList() method that synchronizes the list as a whole.
6
COLLECTIONS FRAMEWORK
import java.util.Vector;
class Main {
public static void main(String[] args) {
Vector<String> mammals= new Vector<>();
// Using addAll()
Vector<String> animals = new Vector<>();
animals.add("Crocodile");
animals.addAll(mammals);
System.out.println("New Vector: " + animals);
}
}
Output
-remove(index)
removes an element from specified position
7
COLLECTIONS FRAMEWORK
import java.util.Vector;
class Main {
public static void main(String[] args) {
Vector<String> animals= new Vector<>();
animals.add("Dog");
animals.add("Horse");
animals.add("Cat");
// Using remove()
String element = animals.remove(1);
System.out.println("Removed Element: " + element);
System.out.println("New Vector: " + animals);
// Using clear()
animals.clear();
System.out.println("Vector after clear(): " + animals);
}
}
Output
The Map interface of the Java collections framework provides the functionality of the
Working of Map
In Java, elements of Map are stored in key/value pairs. Keys are unique values
associated with individual Values.
8
COLLECTIONS FRAMEWORK
A map cannot contain duplicate keys. And, each key is associated with a single value.
We can access and modify values using the keys associated with them.
In the above diagram, we have values: United States, Brazil, and Spain. And we have
corresponding keys: us, br, and es.
Now, we can access those values using their corresponding keys.
HashMap
EnumMap
LinkedHashMap
WeakHashMap
TreeMap
9
COLLECTIONS FRAMEWORK
In Java, we must import the java.util.Map package in order to use Map . Once we
In the above code, we have created a Map named numbers . We have used
the HashMap class to implement the Map interface.
Here,
class Main {
10
COLLECTIONS FRAMEWORK
Output
The Iterator interface of the Java collections framework allows us to access elements
of a collection. It has a subinterface ListIterator.
BY LOVELEEN KAUR 11
COLLECTIONS FRAMEWORK
All the Java collections include an iterator() method. This method returns an instance
of iterator used to iterate over elements of collections.
Methods of Iterator
The Iterator interface provides 4 methods that can be used to perform various
operations on elements of collections.
hasNext() - returns true if there exists an element in the collection
next() - returns the next element of the collection
remove() - removes the last element returned by the next()
forEachRemaining() - performs the specified action for each remaining
element of the collection
class Main {
public static void main(String[] args) {
// Creating an ArrayList
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(3);
numbers.add(2);
System.out.println("ArrayList: " + numbers);
BY LOVELEEN KAUR 12
COLLECTIONS FRAMEWORK
ArrayList: [1, 3, 2]
Acessed Element: 1
Removed Element: 1
Updated ArrayList: 3, 2,
For example,
import java.util.Iterator;
import java.util.Vector;
class Main {
public static void main(String[] args) {
Vector<String> animals= new Vector<>();
animals.add("Dog");
animals.add("Horse");
animals.add("Cat");
// Using get()
BY LOVELEEN KAUR 13
COLLECTIONS FRAMEWORK
// Using iterator()
Iterator<String> iterate = animals.iterator();
System.out.print("Vector: ");
while(iterate.hasNext()) {
System.out.print(iterate.next());
System.out.print(", ");
}
}
}
Output
BY LOVELEEN KAUR 14