Collection Fram Work in Java Interview Questions
Collection Fram Work in Java Interview Questions
*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.
1. Collection interface: Collection (java.util.Collection) is the primary interface, and every collection must implement this
interface.
Syntax:
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:
Syntax:
Syntax:
3) ArrayList increases its size by 50% of the array size. Vector increases its size by doubling the array size.
ArrayList is not ?thread-safe? as it is not Vector list is ?thread-safe? as it?s every method is
4)
synchronized. synchronized.
3) ArrayList is better to store and fetch data. LinkedList is better to manipulate data.
4) ArrayList provides random access. LinkedList does not provide random access.
ArrayList takes less memory overhead as it LinkedList takes more memory overhead, as it stores the object as
5)
stores only object well as the address of that object.
2) The Iterator can be used in List, Set, and Queue. ListIterator can be used in List only.
The Iterator can only perform remove operation ListIterator can perform ?add,? ?remove,? and ?set? operation
3)
while traversing the collection. while traversing the collection.
7) What is the difference between Iterator and Enumeration?
No. Iterator Enumeration
1) The Iterator can traverse legacy and non-legacy elements. Enumeration can traverse only legacy elements.
The Iterator can perform remove operation while The Enumeration can perform only traverse operation
4)
traversing the collection. on the collection.
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.
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.
11) What is the difference between HashSet and HashMap?
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.
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.
HashMap is not ?thread-safe,? so it is useful for non- Hashtable is thread-safe, and it can be shared between
3)
threaded applications. various threads.
4) 4) HashMap inherits the AbstractMap class Hashtable inherits the Dictionary class.
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.
15) What is the difference between Comparable and Comparator?
No. Comparable Comparator
The Comparator provides multiple sorts of
1) Comparable provides only one sort of sequence.
sequences.
2) It provides one method named compareTo(). It provides one method named compare().
Syntax:
The hashCode() method returns the same integer number if two keys (by calling equals() method) are identical.
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.
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.
19) What is the advantage of the generic collection?
There are three main advantages of using the generic collection.
Generic confirms the stability of the code by making it bug detectable at compile time.
Separate Chaining
Open Addressing
Array ArrayList
SN
The Array is of fixed size, means we cannot resize the ArrayList is not of the fixed size we can change the size
1
array as per need. dynamically.