4th (Java Collections Interview Questions (2022) - Javatpoint)
4th (Java Collections Interview Questions (2022) - Javatpoint)
https://www.javatpoint.com/java-collections-interview-questions 1/22
2/1/22, 7:17 PM Java Collections Interview Questions (2022) - javatpoint
Collection Framework is a combination of classes and interface, which is used to store and
manipulate the data in the form of objects. It provides various classes such as ArrayList, Vector,
Stack, and HashSet, etc. and interfaces such as List, Queue, Set, etc. for this purpose.
Array and Collection are somewhat similar regarding storing the references of objects and
manipulating the data, but they differ in many ways. The main differences between the array and
Collection are defined below:
Arrays are always of fixed size, i.e., a user can not increase or decrease the length of the array
according to their requirement or at runtime, but In Collection, size can be changed
dynamically as per need.
Arrays can only store homogeneous or similar type objects, but in Collection, heterogeneous
objects can be stored.
Arrays cannot provide the ?ready-made? methods for user requirements as sorting,
searching, etc. but Collection includes readymade methods to use.
Collection framework implements various interfaces, Collection interface and Map interface
( java.util.Map) are the mainly used interfaces of Java Collection Framework. List of interfaces of
Collection Framework is given below:
https://www.javatpoint.com/java-collections-interview-questions 2/22
2/1/22, 7:17 PM Java Collections Interview Questions (2022) - javatpoint
1. Collection interface: Collection ( java.util.Collection) is the primary interface, and every collection
must implement this interface.
Syntax:
public interface Collection<E>extends Iterable
2. List interface: List interface extends the Collection interface, and it is an ordered collection of
objects. It contains duplicate elements. It also allows random access of elements.
Syntax:
public interface List<E> extends Collection<E>
3. Set interface: Set ( java.util.Set) interface is a collection which cannot contain duplicate elements.
It can only include inherited methods of Collection interface
Syntax:
public interface Set<E> extends Collection<E>
Queue interface: Queue ( java.util.Queue) interface defines queue data structure, which stores the
elements in the form FIFO (first in first out).
https://www.javatpoint.com/java-collections-interview-questions 3/22
2/1/22, 7:17 PM Java Collections Interview Questions (2022) - javatpoint
Syntax:
public interface Queue<E> extends Collection<E>
Syntax:
public interface Dequeue<E> extends Queue<E>
5. Map interface: A Map ( java.util.Map) represents a key, value pair storage of elements. Map
interface does not implement the Collection interface. It can only contain a unique key but can have
duplicate elements. There are two interfaces which implement Map in java that are Map interface
and Sorted Map.
3) ArrayList increases its size by 50% of the Vector increases its size by doubling the array
array size. size.
4) ArrayList is not ?thread-safe? as it is not Vector list is ?thread-safe? as it?s every method
synchronized. is synchronized.
https://www.javatpoint.com/java-collections-interview-questions 4/22
2/1/22, 7:17 PM Java Collections Interview Questions (2022) - javatpoint
4) ArrayList provides random access. LinkedList does not provide random access.
Iterator traverses the elements in the forward direction only whereas ListIterator traverses the
elements into forward and backward direction.
1) The Iterator traverses the elements in ListIterator traverses the elements in backward
the forward direction only. and forward directions both.
https://www.javatpoint.com/java-collections-interview-questions 5/22
2/1/22, 7:17 PM Java Collections Interview Questions (2022) - javatpoint
2) The Iterator can be used in List, Set, and ListIterator can be used in List only.
Queue.
3) The Iterator can only perform remove ListIterator can perform ?add,? ?remove,? and ?
operation while traversing the set? operation while traversing the collection.
collection.
1) The Iterator can traverse legacy and non- Enumeration can traverse only legacy
legacy elements. elements.
4) The Iterator can perform remove operation The Enumeration can perform only
while traversing the collection. traverse operation on the collection.
The List and Set both extend the collection interface. However, there are some differences between
the both which are listed below.
The List can contain duplicate elements whereas Set includes unique items.
The List is an ordered collection which maintains the insertion order whereas Set is an
unordered collection which does not preserve the insertion order.
The List interface contains a single legacy class which is Vector class whereas Set interface
does not have any legacy class.
The List interface can allow n number of null values whereas Set interface only allows a single
null value.
https://www.javatpoint.com/java-collections-interview-questions 6/22
2/1/22, 7:17 PM Java Collections Interview Questions (2022) - javatpoint
The HashSet and TreeSet, both classes, implement Set interface. The differences between the both
are listed below.
The differences between the Set and Map are given below.
Set contains values only whereas Map contains key and values both.
Set contains unique values whereas Map can contain unique Keys with duplicate values.
Set holds a single number of null value whereas Map can include a single null key with n
number of null values.
The differences between the HashSet and HashMap are listed below.
HashSet contains only values whereas HashMap includes the entry (key, value). HashSet can
be iterated, but HashMap needs to convert into Set to be iterated.
HashSet implements Set interface whereas HashMap implements the Map interface
HashSet cannot have any duplicate value whereas HashMap can contain duplicate values with
unique keys.
HashSet contains the only single number of null value whereas HashMap can hold a single
null key with n number of null values.
The differences between the HashMap and TreeMap are given below.
HashMap can be sorted by Key or value whereas TreeMap can be sorted by Key.
HashMap may contain a null key with multiple null values whereas TreeMap cannot hold a
null key but can have multiple null values.
2) HashMap can contain one null key and Hashtable cannot contain any null key or
multiple null values. null value.
4) 4) HashMap inherits the AbstractMap class Hashtable inherits the Dictionary class.
https://www.javatpoint.com/java-collections-interview-questions 8/22
2/1/22, 7:17 PM Java Collections Interview Questions (2022) - javatpoint
The differences between the Collection and Collections are given below.
The Collection interface provides the standard functionality of data structure to List, Set, and
Queue. However, Collections class is to sort and synchronize the collection elements.
The Collection interface provides the methods that can be used for data structure whereas
Collections class provides the static methods which can be used for various operation on a
collection.
1) Comparable provides only one sort of sequence. The Comparator provides multiple
sorts of sequences.
4) If we implement the Comparable interface, The The actual class is not changed.
actual class is modified.
BlockingQueue is an interface which extends the Queue interface. It provides concurrency in the
operations like retrieval, insertion, deletion. While retrieval of any element, it waits for the queue to
be non-empty. While storing the elements, it waits for the available space.
BlockingQueue cannot
contain null elements, and implementation of BlockingQueue is thread-safe.
Syntax:
public interface BlockingQueue<E> extends Queue <E>
https://www.javatpoint.com/java-collections-interview-questions 9/22
2/1/22, 7:17 PM Java Collections Interview Questions (2022) - javatpoint
If you change the value in the properties file, you don't need to recompile the java class. So, it makes
the application easy to manage. It is used to store information which is to be changed frequently.
Consider the following example.
import java.util.*;
import java.io.*;
public class Test {
public static void main(String[] args)throws Exception{
FileReader reader=new FileReader("db.properties");
Properties p=new Properties();
p.load(reader);
System.out.println(p.getProperty("user"));
System.out.println(p.getProperty("password"));
}
}
Output
system
oracle
The hashCode() method returns a hash code value (an integer number).
The hashCode() method returns the same integer number if two keys (by calling equals() method)
are identical.
https://www.javatpoint.com/java-collections-interview-questions 10/22
2/1/22, 7:17 PM Java Collections Interview Questions (2022) - javatpoint
However, it is possible that two hash code numbers can have different or the same keys.
If two objects do not produce an equal result by using the equals() method, then the hashcode()
method will provide the different integer result for both the objects.
The equals method is used to check whether two objects are the same or not. It needs to be
overridden if we want to check the objects based on the property.
For example, Employee is a class that has 3 data members: id, name, and salary. However, we want
to check the equality of employee object by the salary. Then, we need to override the equals()
method.
Yes, Collections class provides methods to make List, Set or Map elements as synchronized:
Generic confirms the stability of the code by making it bug detectable at compile time.
Two different keys with the same hash value are known as hash-collision. Two separate entries will
be kept in a single hash bucket to avoid the collision. There are two ways to avoid hash-collision.
Separate Chaining
Open Addressing
24) What is the default size of load factor in hashing based collection?
The default size of load factor is 0.75. The default capacity is computed as initial capacity * load
factor. For example, 16 * 0.75 = 12. So, 12 is the default capacity of Map.
The main differences between the Array and ArrayList are given below.
SN Array ArrayList
1 The Array is of fixed size, means we cannot ArrayList is not of the fixed size we can
resize the array as per need. change the size dynamically.
https://www.javatpoint.com/java-collections-interview-questions 12/22
2/1/22, 7:17 PM Java Collections Interview Questions (2022) - javatpoint
3 Arrays can store primitive data types as ArrayList cannot store the primitive data
well as objects. types it can only store the objects.
27) What is the difference between the length of an Array and size of
ArrayList?
The length of an array can be obtained using the property of length whereas ArrayList does not
support length property, but we can use size() method to get the number of objects in the list.
Int [] array = new int[4];
System.out.println("The size of the array is " + array.length);
ArrayList<String> list=new ArrayList<String>();
list.add("ankit");
list.add("nippun");
System.out.println(list.size());
We can convert an Array to ArrayList by using the asList() method of Arrays class. asList() method is
the static method of Arrays class and accepts the List object. Consider the following syntax:
Arrays.asList(item)
We can convert an ArrayList to Array using toArray() method of the ArrayList class. Consider the
following syntax to convert the ArrayList to the List object.
List_object.toArray(new String[List_object.size()])
https://www.javatpoint.com/java-collections-interview-questions 13/22
2/1/22, 7:17 PM Java Collections Interview Questions (2022) - javatpoint
Using HashSet: By using HashSet we can remove the duplicate element from the ArrayList,
but it will not then preserve the insertion order.
Using LinkedHashSet: We can also maintain the insertion order by using LinkedHashSet
instead of HashSet.
The Process to remove duplicate elements from ArrayList using the LinkedHashSet:
Empty the ArrayList using clear() method, which will remove all the elements from the list.
To reverse an ArrayList, we can use reverse() method of Collections class. Consider the following
example.
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class ReverseArrayList {
public static void main(String[] args) {
List list = new ArrayList<>();
list.add(10);
list.add(50);
https://www.javatpoint.com/java-collections-interview-questions 14/22
2/1/22, 7:17 PM Java Collections Interview Questions (2022) - javatpoint
list.add(30);
Iterator i = list.iterator();
System.out.println("printing the list....");
while(i.hasNext())
{
System.out.println(i.next());
}
Iterator i2 = list.iterator();
Collections.reverse(list);
System.out.println("printing list in reverse order....");
while(i2.hasNext())
{
System.out.println(i2.next());
}
}
}
Output
10
50
30
30
50
10
To sort the ArrayList in descending order, we can use the reverseOrder method of Collections class.
Consider the following example.
import java.util.ArrayList;
import java.util.Collection;
https://www.javatpoint.com/java-collections-interview-questions 15/22
2/1/22, 7:17 PM Java Collections Interview Questions (2022) - javatpoint
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
public class ReverseArrayList {
public static void main(String[] args) {
List list = new ArrayList<>();
list.add(10);
list.add(50);
list.add(30);
list.add(60);
list.add(20);
list.add(90);
Iterator i = list.iterator();
System.out.println("printing the list....");
while(i.hasNext())
{
System.out.println(i.next());
}
Comparator cmp = Collections.reverseOrder();
Collections.sort(list,cmp);
System.out.println("printing list in descending order....");
Iterator i2 = list.iterator();
while(i2.hasNext())
{
System.out.println(i2.next());
}
}
}
https://www.javatpoint.com/java-collections-interview-questions 16/22
2/1/22, 7:17 PM Java Collections Interview Questions (2022) - javatpoint
Output
10
50
30
60
20
90
90
60
50
30
20
10
Using CopyOnWriteArrayList<T>
LinkedLists are better to use for the update operations whereas ArrayLists are better to use for the
search operations.
← Prev
Next →
1
2
3
4
5
6
7
8
https://www.javatpoint.com/java-collections-interview-questions 17/22
2/1/22, 7:17 PM Java Collections Interview Questions (2022) - javatpoint
https://www.javatpoint.com/java-collections-interview-questions 18/22
2/1/22, 7:17 PM Java Collections Interview Questions (2022) - javatpoint
https://www.javatpoint.com/java-collections-interview-questions 19/22
2/1/22, 7:17 PM Java Collections Interview Questions (2022) - javatpoint
Preparation
Company
Interview
Questions
Company Questions
Trending Technologies
https://www.javatpoint.com/java-collections-interview-questions 20/22
2/1/22, 7:17 PM Java Collections Interview Questions (2022) - javatpoint
B.Tech / MCA
https://www.javatpoint.com/java-collections-interview-questions 21/22
2/1/22, 7:17 PM Java Collections Interview Questions (2022) - javatpoint
Data Warehouse
https://www.javatpoint.com/java-collections-interview-questions 22/22