Lesson:
Collection framework in Java
Pre Requisites:
Basics of Jav
OOPS in Jav
Packages, Classes, Interfaces
List of concepts involved :
Collections in Jav
What is a Framewor
Need for a separate Collection framewor
Collection framework hierarch
Collection interface hierarch
Methods of Collection interfac
Exampl
Map interfac
Hierarchy of Map interfac
Methods of Map interfac
Example
Topic - Collections in Java
A group of individual objects which are represented as a single unit is known as the collection of the
objects.
Java Collection means a single unit of objects.
Java Collections can achieve all the operations that you perform on a data such as searching, sorting,
insertion, manipulation, and deletion
The Collection in Java is a framework that provides an architecture to store and manipulate the group of
objects
This Collection framework provides a set of interfaces and classes to implement various data structures and
algorithms.
What is Framework ?
A framework is a set of classes and interfaces which provide a ready-made architecture
Using frameworks saves time and reduces the risk of errors. You don't need to write everything from scratch.
Need for a separate Collection framework
The Collection framework represents a unified architecture for storing and manipulating a group of objects.
It has Interfaces and its implementations, i.e., classes and the algorithm
Before the Collection framework was introduced, the standard methods for grouping Java objects were
Arrays or Vectors, or Hash Tables which had no common interface
Therefore, though the main aim of all the collections is the same, the implementation of all these collections
was defined independently and had no correlation among them.
Cracking the Coding Interview in JAVA - Foundation
And also, it is very difficult for the users to remember all the different methods, syntax, and constructors
present in every collection class.
The Collection interface (java.util.Collection) and Map interface (java.util.Map) are the two main “root”
interfaces of Java collection classes.
Topic: The Collection Interface Hierarchy
What is Framework ?
e-> extends & i-> implements
Here, we are seeing the hierarchy of List Interface and a similar hierarchy is followed for all Interfaces
The java.util package contains all the classes and interfaces for the Collections framework
It has interfaces that can have methods, but the methods declared in an interface are by default abstract
(only method signature, no body). Interfaces specify what a class must do and not how. It is the blueprint of
the class. For example: List, Queue
These interfaces are extended by the collection framework.
Cracking the Coding Interview in JAVA - Foundation
Classes like ArrayList class implements the List interface which is a subinterface of the Collection Interface
A class is a user-defined blueprint or prototype from which objects are created. It represents the set of
properties or methods that are common to all objects of one type.
For example: ArrayList, Linkedlist.
There are many implementations of each of the interfaces present inside Java Collection framework.
Few of the most commonly used classes of Collection framework are -
HastSe
TreeSe
ArrayLis
LinkedLis
Stac
Vecto
HashMa
TreeMa
Collections
Methods of Collection Interface
Cracking the Coding Interview in JAVA - Foundation
EXAMPLE:
Let’s understand how to use collections framework to implement an ArrayList class.
We will declare an arraylist, add elements to it, display all the elements, remove the third element, display the
final elements.
https://pastebin.com/4Z91cy57
MAP INTERFACE
The Map interface is present in java.util package represents a mapping between a key and a value.
The Map interface is not a subtype of the Collection interface.
Therefore it behaves a bit differently from the rest of the collection types.
A map contains unique keys.
Hierarchy of Map Interface
There are two interfaces for implementing Map in java. They are Map and SortedMap, and three classes:
HashMap, TreeMap, and LinkedHashMap
A Map cannot contain duplicate keys and each key can map to at most one value.
The order of a map depends on the specific implementations. For example, TreeMap and LinkedHashMap
have predictable orders, while HashMap does not.
Cracking the Coding Interview in JAVA - Foundation
A HashMap stores key-value pairs in no particular order, a TreeMap stores key-value pairs in a sorted order
by the keys and a LinkedHashMap stores key-value pairs in the order of insertion.
clear()
containsKey(Object)
containsValue(Object)
entrySet()
equals(Object)
get(Object)
isEmpty()
keySet()
put(Object, Object)
putAll(Map)
remove(Object)
Cracking the Coding Interview in JAVA - Foundation
size()
values()
getOrDefault(Object
key, V defaultValue)
EXAMPLE:
Let’s use a HashMap to create key value pairs and display their values using java Map classes’ methods.
https://pastebin.com/TVHiDuMJ
Upcoming Class Teasers
Linked Lists
Cracking the Coding Interview in JAVA - Foundation