
Domains
Agile Management
Master Agile methodologies for efficient and timely project delivery.
View All Agile Management Coursesicon-refresh-cwCertifications
Scrum Alliance
16 Hours
Best Seller
Certified ScrumMaster (CSM) CertificationScrum Alliance
16 Hours
Best Seller
Certified Scrum Product Owner (CSPO) CertificationScaled Agile
16 Hours
Trending
Leading SAFe 6.0 CertificationScrum.org
16 Hours
Professional Scrum Master (PSM) CertificationScaled Agile
16 Hours
SAFe 6.0 Scrum Master (SSM) CertificationAdvanced Certifications
Scaled Agile, Inc.
32 Hours
Recommended
Implementing SAFe 6.0 (SPC) CertificationScaled Agile, Inc.
24 Hours
SAFe 6.0 Release Train Engineer (RTE) CertificationScaled Agile, Inc.
16 Hours
Trending
SAFe® 6.0 Product Owner/Product Manager (POPM)IC Agile
24 Hours
ICP Agile Certified Coaching (ICP-ACC)Scrum.org
16 Hours
Professional Scrum Product Owner I (PSPO I) TrainingMasters
32 Hours
Trending
Agile Management Master's Program32 Hours
Agile Excellence Master's ProgramOn-Demand Courses
Agile and ScrumRoles
Scrum MasterTech Courses and Bootcamps
Full Stack Developer BootcampAccreditation Bodies
Scrum AllianceTop Resources
Scrum TutorialProject Management
Gain expert skills to lead projects to success and timely completion.
View All Project Management Coursesicon-standCertifications
PMI
36 Hours
Best Seller
Project Management Professional (PMP) CertificationAxelos
32 Hours
PRINCE2 Foundation & Practitioner CertificationAxelos
16 Hours
PRINCE2 Foundation CertificationAxelos
16 Hours
PRINCE2 Practitioner CertificationSkills
Change ManagementMasters
Job Oriented
45 Hours
Trending
Project Management Master's ProgramUniversity Programs
45 Hours
Trending
Project Management Master's ProgramOn-Demand Courses
PRINCE2 Practitioner CourseRoles
Project ManagerAccreditation Bodies
PMITop Resources
Theories of MotivationCloud Computing
Learn to harness the cloud to deliver computing resources efficiently.
View All Cloud Computing Coursesicon-cloud-snowingCertifications
AWS
32 Hours
Best Seller
AWS Certified Solutions Architect - AssociateAWS
32 Hours
AWS Cloud Practitioner CertificationAWS
24 Hours
AWS DevOps CertificationMicrosoft
16 Hours
Azure Fundamentals CertificationMicrosoft
24 Hours
Best Seller
Azure Administrator CertificationMicrosoft
45 Hours
Recommended
Azure Data Engineer CertificationMicrosoft
32 Hours
Azure Solution Architect CertificationMicrosoft
40 Hours
Azure DevOps CertificationAWS
24 Hours
Systems Operations on AWS Certification TrainingAWS
24 Hours
Developing on AWSMasters
Job Oriented
48 Hours
New
AWS Cloud Architect Masters ProgramBootcamps
Career Kickstarter
100 Hours
Trending
Cloud Engineer BootcampRoles
Cloud EngineerOn-Demand Courses
AWS Certified Developer Associate - Complete GuideAuthorized Partners of
AWSTop Resources
Scrum TutorialIT Service Management
Understand how to plan, design, and optimize IT services efficiently.
View All DevOps Coursesicon-git-commitCertifications
Axelos
16 Hours
Best Seller
ITIL 4 Foundation CertificationAxelos
16 Hours
ITIL Practitioner CertificationPeopleCert
16 Hours
ISO 14001 Foundation CertificationPeopleCert
16 Hours
ISO 20000 CertificationPeopleCert
24 Hours
ISO 27000 Foundation CertificationAxelos
24 Hours
ITIL 4 Specialist: Create, Deliver and Support TrainingAxelos
24 Hours
ITIL 4 Specialist: Drive Stakeholder Value TrainingAxelos
16 Hours
ITIL 4 Strategist Direct, Plan and Improve TrainingOn-Demand Courses
ITIL 4 Specialist: Create, Deliver and Support ExamTop Resources
ITIL Practice TestData Science
Unlock valuable insights from data with advanced analytics.
View All Data Science Coursesicon-dataBootcamps
Job Oriented
6 Months
Trending
Data Science BootcampJob Oriented
289 Hours
Data Engineer BootcampJob Oriented
6 Months
Data Analyst BootcampJob Oriented
288 Hours
New
AI Engineer BootcampSkills
Data Science with PythonRoles
Data ScientistOn-Demand Courses
Data Analysis Using ExcelTop Resources
Machine Learning TutorialDevOps
Automate and streamline the delivery of products and services.
View All DevOps Coursesicon-terminal-squareCertifications
DevOps Institute
16 Hours
Best Seller
DevOps Foundation CertificationCNCF
32 Hours
New
Certified Kubernetes AdministratorDevops Institute
16 Hours
Devops LeaderSkills
KubernetesRoles
DevOps EngineerOn-Demand Courses
CI/CD with Jenkins XGlobal Accreditations
DevOps InstituteTop Resources
Top DevOps ProjectsBI And Visualization
Understand how to transform data into actionable, measurable insights.
View All BI And Visualization Coursesicon-microscopeBI and Visualization Tools
Certification
24 Hours
Recommended
Tableau CertificationCertification
24 Hours
Data Visualization with Tableau CertificationMicrosoft
24 Hours
Best Seller
Microsoft Power BI CertificationTIBCO
36 Hours
TIBCO Spotfire TrainingCertification
30 Hours
Data Visualization with QlikView CertificationCertification
16 Hours
Sisense BI CertificationOn-Demand Courses
Data Visualization Using Tableau TrainingTop Resources
Python Data Viz LibsCyber Security
Understand how to protect data and systems from threats or disasters.
View All Cyber Security Coursesicon-refresh-cwCertifications
CompTIA
40 Hours
Best Seller
CompTIA Security+EC-Council
40 Hours
Certified Ethical Hacker (CEH v12) CertificationISACA
22 Hours
Certified Information Systems Auditor (CISA) CertificationISACA
40 Hours
Certified Information Security Manager (CISM) Certification(ISC)²
40 Hours
Certified Information Systems Security Professional (CISSP)(ISC)²
40 Hours
Certified Cloud Security Professional (CCSP) Certification16 Hours
Certified Information Privacy Professional - Europe (CIPP-E) CertificationISACA
16 Hours
COBIT5 Foundation16 Hours
Payment Card Industry Security Standards (PCI-DSS) CertificationOn-Demand Courses
CISSPTop Resources
Laptops for IT SecurityWeb Development
Learn to create user-friendly, fast, and dynamic web applications.
View All Web Development Coursesicon-codeBootcamps
Career Kickstarter
6 Months
Best Seller
Full-Stack Developer BootcampJob Oriented
3 Months
Best Seller
UI/UX Design BootcampEnterprise Recommended
6 Months
Java Full Stack Developer BootcampCareer Kickstarter
490+ Hours
Front-End Development BootcampCareer Accelerator
4 Months
Backend Development Bootcamp (Node JS)Skills
ReactOn-Demand Courses
Angular TrainingTop Resources
Top HTML ProjectsBlockchain
Understand how transactions and databases work in blockchain technology.
View All Blockchain Coursesicon-stop-squareBlockchain Certifications
40 Hours
Blockchain Professional Certification32 Hours
Blockchain Solutions Architect Certification32 Hours
Blockchain Security Engineer Certification24 Hours
Blockchain Quality Engineer Certification5+ Hours
Blockchain 101 CertificationOn-Demand Courses
NFT Essentials 101: A Beginner's GuideTop Resources
Blockchain Interview QsProgramming
Learn to code efficiently and design software that solves problems.
View All Programming Coursesicon-codeSkills
Python CertificationInterview Prep
Career Accelerator
3 Months
Software Engineer Interview PrepOn-Demand Courses
Data Structures and Algorithms with JavaScriptTop Resources
Python TutorialProgramming
4.5 Rating 31 Questions 25 mins read17 Readers

The collection is an API, introduced in JDK 5. Initially, the collection got released with some of few classes with Vector, Stack, Hash table, Array. The collection is an API which has got various implementation of data structure List, Set, Stack, Queue, Map. The available implementation classes can group the N no of objects without specifying the size. Collection API provides the growable object. All these classes are a growable class which increases the size at runtime.
Expect to come across this popular question in java collection interview questions.
The collection framework provides all the implementation of the available data structures, which reduces the development time. All the classes are growable classes, that means we don’t need to provide the size at compile time. Later release Collection framework also starts supporting the Generics. Collection framework also starts supporting the thread collection class like ConcurrentHashMap and CopyOnWriteArrayList. Code quality also getting improved by using well-tested collection API. Reduce code maintenance by using the collection framework. Collection API also provides the utility class called Collections. Enhance the reusability and interpretability of the code base.
1. Set
2. List
3. Queue

This is a frequently asked question in Java collection interview questions and answers.
A map is an object that maps keys to values. The map works on key-value pair principal. A map doesn't allow to contain duplicate keys. Each key can map to at most one value. HashMap, TreeMap, ConcurentHashMap are the implementation of the map. The map has three kind of collection like key, values and key values and it doesn’t implement the collection interface that is the reason the Map interface doesn’t fall in the collection hierarchy. The order of the map depends on the specific implementation classes.
Code Snippet: Map map = new HashMap();.
The best use case is when we need to specify the pairing between the object and store accordingly.
Hashmap compose with an array of node and Node is an object of a class which has following below object:
Let’s see how it all together works, first will approach to hashing process, which uses a hashing algorithm to generate index position-taking as an input of hashkey value of the corresponding object. Let’s look on below code snippet:
Class KeyDemo {
String key;
KeyDemo(String key){
This.key = key;
}
@Override
public int hashCode()
{
return (int)key.charAt(0);
}
@Override
public booleanequals(Object obj)
{
return key.equals((String)obj);
}
}
The moment, when we passing the key object in the put method of hashmap like below
Map map = new HashMap();
map.put(key, value);
The HashMap calling the hashcode() method of object and get the 10 digit numeric value, which eventually passing to the hashing algorithms which is like below syntax
f(h(hashCode (key) % array_length)) = {index position} of hashmap, ,where x is a hashcode value, according to the index position the hashmap insert the value object in the corresponding bucket. HashMap uses the linkedlist to store the value object.

Below is the implementation of HashMap, where K is the Key and V is the Value, usually the initial capacity is always 16, whenever the map get instantiated and it's computed every time when its reach to a threshold value which is 0.75,
public class Map<K, V> {
private Entry<K, V>[] buckets;
private static final int INITIAL_CAPACITY = 1 << 4; // 16
private int size = 0;
public Map() {
this(INITIAL_CAPACITY);
}
public Map(int capacity) {
this.buckets = new Entry[capacity];
}
public void put(K key, V value) {
Entry<K, V> entry = new Entry<>(key, value, null);
int bucket = getHash(key) % getBucketSize();
Entry<K, V> existing = buckets[bucket];
if (existing == null) {
buckets[bucket] = entry;
size++;
} else {
// compare the keys see if key already exists
while (existing.next != null) {
if (existing.key.equals(key)) {
existing.value = value;
return;
}
existing = existing.next;
}
if (existing.key.equals(key)) {
existing.value = value;
} else {
existing.next = entry;
size++;
}
}
}
The retrieval process of HashMap is, it has to compute the hashcode whenever the get method call which along with the key, using the key, hashmap computes the index position and equal() plays the vital role to identify the correct object from the bucket or LinkedList. Below is the code snippet for same.
public V get(K key) {
Entry<K, V> bucket = buckets[getHash(key) % getBucketSize()];
while (bucket != null) {
if (bucket.key.equals(key)) {
return bucket.value;
}
bucket = bucket.next;
}
return null;
}
There is a collision issue happening which is usually taken care of by collision detection and collision prevention.
The collision problem is, whenever the hash function returns the same index position for the different key, then a collision occurs. The collision detection technique is also called collision detection.

Now, let's move to the collision prevention technique. The HashMap uses below algorithms to resolve the collision problem.
public V get(Object key) {
if (key == null)
return getForNullKey();
int hash = hash(key.hashCode());
for (Entry<K , V> e = table[indexFor(hash, table.length)]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
return e.value;
}
return null;
}One of the most frequently posed collection interview questions for experienced, be ready for it.
The time complexity of HashMap to getting the elements is usually from O(n) to O(Log(n)), when there is no collision occurs and the worst case is O(n) to O(1), because when collision occurs and HashMap needs to travel till the end of the linked list. Now in the case of putting the elements in HashMap, is always O(1). Below classes is the implementation of Map interface.
However, as per the memory performance basis, the WeakHashMap gives better performance. But it doesn’t provide the thread safety if one needs to achieve the thread safety, to achieve the thread safety one needs to use the ConcurrentHashMap because it provides thread safety without using synchronization.
Initial capacity in HashMap plays a vital role in the allocation of memory as we know that HashMap works on the key-value pair, that means the HashMap needed a large amount of memory to maintain the key and value, to overcome these challenges the HashMap works on the principal to allocate the optimum size, whenever the HashMap get instantiated, that is called Initial capacity, which is usually a 16.
Now let’s focus on load factor, so there is a concept of threshold value, which is always a 0.75 size of the total capacity, the significance of this is whenever any new record gets inserted in the HashMap, the HashMap computes the allocated size of the HashMap, then it checks whether this size reaches the threshold value then HashMap increased the size of 75% from the current size to augmented the storage for next incoming record.
Fail fast and fail-safe is one of the prominent features of collection iterator framework, which works on the principle of modification of a record in the collection object while iterating over it, however, both have a different way to handle.
Fail Fast iterator: The fail-fast iterator checks, If there is modification happening while iterating over the elements, it will check and throw the exception called a ConcurrentModificationException. Typically the fail-fast iterator implements the volatile counter whenever the iterator gets instantiated this counter embedded to the iterator and in the case of any modification it will increment the counter if it is greater than one it will throw the exception. Below is the code snippet for same.
public class FailFastDemo{
public static void main(String[] args)
{
Map<String, String> capital = new HashMap<String, String>();
capital.put("Delhi", "India");
capital.put("Moscow", "Russia");
capital.put("New York", "USA");
Iterator iterator = capital.keySet().iterator();
while (iterator.hasNext()) {
System.out.println(capital.get(iterator.next()));
// in this code we are modifying the element to Map
// exception will be thrown on next call
// of next() method.
capital.put("Istanbul", "Turkey");
}
}
}Fail-safe Iterator: Fail safe iterator is a part of the java.util.concurrent api, it works on the principal of the cloning, actually in this case it creates the clone of the object and iterate over it, if any modification happens, it wouldn’t touch to the actual object and continue to iterate without failure. One issue with the fail-safe iterator is, it doesn’t guarantee that updated elements will be reflected in the iteration. And another issue is it consumes more memory than fail fast because it requires to create the clone, whereas failing fast works only original one.
The CopyOnWriteArrayList and ConcurrentHashMap is the example of fail-safe iterator.
Below is the code snippet for same.
ConcurrentHashconcurrentHashMap<String, Integer> concurrentHashMap = new ConcurrentHashconcurrentHashMap<>();
concurrentHashMap.put("First", 10);
concurrentHashMap.put("Second", 20);
concurrentHashMap.put("Third", 30);
concurrentHashMap.put("Fourth", 40);
Iterator<String> iterator = concurrentHashMap.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next();
concurrentHashMap.put("Fifth", 50);
}