Java Collection Framework - Apni Kaksha
Java Collection Framework - Apni Kaksha
Framework
Java Collection Framework Interfaces
Java Collection Framework
1. Introduction to collection interfaces
2. List
3. Set
4. Queue
5. Map
6. Natural Ordering and Sorting
7. Comparators
Collection Interface
● ArrayList
● LinkedList
● Vector
● Stack
Set Interface
The Set interface of the Java Collections framework
provides the features of the mathematical set in Java. It
extends the Collection interface.Unlike the List
interface, sets cannot contain duplicate elements.
● HashSet
● LinkedHashSet
● EnumSet
● TreeSet
Queue Interface
The Queue interface of the Java collections framework
provides the functionality of the queue data structure.
It extends the Collection interface.
● ArrayDeque
● LinkedList
● PriorityQueue
Map Interface
The Map interface of the Java collections framework provides the functionality of the map data
structure.
In order to use functionalities of the Map interface, we can use these classes:
● HashMap
● EnumMap
● LinkedHashMap
● WeakHashMap
● TreeMap
Java ArrayList &
Generics
ArrayList vs Array
The ArrayList class is an implementation of the List interface that allows us to create resizable-arrays.
In Java, we need to declare the size of an array before we can use it. Once the size of an array is declared, it's
hard to change it.
To handle this issue, we can use the ArrayList class. The ArrayList class present in the java.util package
allows us to create resizable arrays.
Unlike arrays, array lists (objects of the ArrayList class) can automatically adjust its capacity when we add or
remove elements from it. Hence, array lists are also known as dynamic arrays.
Creating an ArrayList
ArrayList<Type> arrayList= new ArrayList<>();
Linked List are linear data structures where the elements are not stored in contiguous locations and every
element is a separate object with a data part and address part. The elements are linked using pointers and
addresses. Each element is known as a node. Due to the dynamicity and ease of insertions and deletions,
It also has few disadvantages like the nodes cannot be accessed directly instead we need to start from the
head and follow through the link to reach to a node we wish to access.
LinkedList in Java
interface.
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. However, in array lists, methods are not synchronized.
Note: It is recommended to use ArrayList in place of Vector because vectors are not thread safe and are less
efficient.
Stack
The Java collections framework has a class named Stack that
provides the functionality of the stack data structure.
● ArrayDeque
● LinkedList
● PriorityQueue
Working of Queue Data Structure
add() offer()
remove() poll()
element() peek()
Map Interface
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.
Java ArrayDeque
ArrayDeque
pop() - returns and removes an element from the top of the stack
ArrayDeque as a Queue
(throw Exception) (return false / null)
add() offer()
remove() poll()
element() peek()
Java PriorityQueue
PriorityQueue
● add()
● remove()
● element()
Java Set
Set Interface
The Set interface of the Java Collections framework provides the
features of the mathematical set in Java. It extends the Collection
interface.Unlike the List interface, sets cannot contain duplicate
elements.
● HashSet
● LinkedHashSet
● EnumSet
● TreeSet
Methods of Set
● add(element)
● addAll(Collection)
● remove(element)
● removeAll()
● retainAll()
● clear()
● size()
● contains()
● containsAll()
● isEmpty()
● toArray()
Operations of Set
● Union - to get the union of two sets x and y, we can use x.addAll(y)
● Intersection - to get the intersection of two sets x and y, we can use x.retainAll(y)
● Subset - to check if x is a subset of y, we can use y.containsAll(x)
Implementation of Set
In order to use functionalities of the Set interface, we can use these classes:
● HashSet
● TreeSet
● LinkedHashSet
● EnumSet
Java Map
Map Implementation
Since Map is an interface, we cannot create
objects from it. In order to use functionalities of
the Map interface, we can use these classes:
● HashMap
● EnumMap
● LinkedHashMap
● WeakHashMap
● TreeMap
Methods of Map
● 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.
Methods of Map cntd.
● replace(K, oldValue, newValue) - Replaces the value of the key K with the new value new Value 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.
Internal Working of
HashMap
HashMap uses an array table to store its key value pairs. Each
element of the array holds the head of a linkedlist to avoid
collision. The hash of every key is calculated and the elements
are placed in the array using this hash function.
1. Always use same attributes of an object to generate hashCode () and equals() both.
2. equals() must be consistent (if the objects are not modified, then it must keep returning the same
value).
3. Whenever a.equals(b), then a.hashCode() must be same as b.hashCode().
4. If you override one, then you should override the other.
Seven Best Books For Programming
1. Introduction to Algorithms: CLRS (Advanced)
2. Cracking the Coding Interview by Gayle Laakmann Mcdowell (Advanced)
3. Algorithms by Sedgwick (Moderate)
4. Data Structures and Algorithms made easy by Narasimha Karumanchi (Beginners)
5. Java for Dummies by Barry A. Burd (Beginners)
6. Operating system concepts by Abraham Silberschatz (General)
7. Computer Networking - A top down approach (General)
Lists (and arrays) of objects that implement this interface can be sorted
automatically by Collections.sort (and Arrays.sort).