1) What is the Collection framework in Java?
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.
2) What are the main differences between array and collection?
Array and Collection are somewhat similar regarding storing the references of objects and manipulating the data, but they differ in many ways.
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 run-time,
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 ready made methods to use.
4) What is the difference between ArrayList and Vector?
ArrayList
|
Vector
|
ArrayList
is not synchronized.
|
Vector
is synchronized.
|
ArrayList
is not a legacy class.
|
Vector
is a legacy class.
|
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 synchronized.
|
Vector
list is thread-safe as it’s every method is synchronized.
|
5) What is the difference between ArrayList and LinkedList?
LinkedLists are better to use for the update operations whereas ArrayLists are better to use for the search operations.
ArrayList
|
LinkedList
|
ArrayList uses a dynamic array.
|
LinkedList uses a doubly linked
list.
|
ArrayList is not efficient for
manipulation because too much is required.
|
LinkedList is efficient for
manipulation.
|
ArrayList is better to store and
fetch data.
|
LinkedList is better to manipulate
data.
|
ArrayList provides random access.
|
LinkedList does not provide random
access.
|
ArrayList takes less memory
overhead as it stores only object
|
LinkedList takes more memory
overhead, as it stores the object as well as the address of that object.
|
6) What is the difference between Iterator and ListIterator?
Iterator traverses the elements in the forward direction only whereas ListIterator traverses the elements into forward and backward direction.
Iterator
|
ListIterator
|
The Iterator traverses the
elements in the forward direction only.
|
ListIterator traverses the
elements in backward and forward directions both.
|
The Iterator can be used in List,
Set, and Queue.
|
ListIterator can be used in List
only.
|
The Iterator can only perform
remove operation while traversing the collection.
|
ListIterator can perform
add,remove, and set operation while traversing the collection.
|
7) What is the difference between Iterator and Enumeration?
Iterator
|
Enumeration
|
||
The
Iterator can traverse legacy and non-legacy elements.
|
Enumeration
can traverse only legacy elements.
|
||
The
Iterator is fail-fast.
|
Enumeration
is not fail-fast.
|
||
The
Iterator is slower than Enumeration.
|
Enumeration
is faster than Iterator.
|
||
The
Iterator can perform remove operation while traversing the collection.
|
The
Enumeration can perform only traverse operation on the collection.
|
8) What is the difference between List and Set?
The List and Set both extend the collection interface.
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.
9) What is the difference between HashSet and TreeSet?
The HashSet and TreeSet, both classes, implement Set interface.
HashSet maintains no order whereas TreeSet maintains ascending order.
HashSet impended by hash table whereas TreeSet implemented by a Tree structure.
HashSet performs faster than TreeSet.
HashSet is backed by HashMap whereas TreeSet is backed by TreeMap.
10) What is the difference between Set and Map?
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?
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.
12) What is the difference between HashMap and TreeMap?
HashMap maintains no order, but TreeMap maintains ascending order.
HashMap is implemented by hash table whereas TreeMap is implemented by a Tree structure.
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.
13) What is the difference between HashMap and Hashtable?
HashMap
|
Hashtable
|
HashMap
is not synchronized.
|
Hashtable
is synchronized.
|
HashMap
can contain one null key and multiple null values.
|
Hashtable
cannot contain any null key or null value.
|
HashMap
is not ?thread-safe,? so it is useful for non-threaded applications.
|
Hashtable
is thread-safe, and it can be shared between various threads.
|
4)
HashMap inherits the AbstractMap class
|
Hashtable
inherits the Dictionary class.
|
14) What is the difference between Collection and Collections?
The Collection is an interface whereas Collections is a 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?
Comparable
|
Comparator
|
Comparable
provides only one sort of sequence.
|
The
Comparator provides multiple sorts of sequences.
|
It
provides one method named compareTo().
|
It
provides one method named compare().
|
It is
found in java.lang package.
|
It is
located in java.util package.
|
If we
implement the Comparable interface, The actual class is modified.
|
The
actual class is not changed.
|
16) What do you understand by BlockingQueue?
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>
17) What is the advantage of Properties file?
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
18) What does the hashCode() method?
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.
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.
19) Why we override equals() method?
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.
20) How to synchronize List, Set and Map elements?
Yes, Collections class provides methods to make List, Set or Map elements as synchronized:
public static List synchronizedList(List l){}
public static Set synchronizedSet(Set s){}
public static SortedSet synchronizedSortedSet(SortedSet s){}
public static Map synchronizedMap(Map m){}
public static SortedMap synchronizedSortedMap(SortedMap m){}
21) What is the advantage of the generic collection?
If we use the generic class, we don't need typecasting.
It is type-safe and checked at compile time.
Generic confirms the stability of the code by making it bug detectable at compile time.
22) What is hash-collision in Hashtable and how it is handled in Java?
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
23) What is the Dictionary class?
The Dictionary class provides the capability to store key-value pairs.
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.
25) What do you understand by fail-fast?
The Iterator in java which immediately throws ConcurrentmodificationException,
if any structural modification occurs in, is called as a Fail-fast iterator. Fail-fats iterator does not require
any extra space in memory.
26) What is the difference between Array and ArrayList?
Array
|
ArrayList
|
The
Array is of fixed size, means we cannot resize the array as per need.
|
ArrayList
is not of the fixed size we can change the size dynamically.
|
Arrays
are of the static type.
|
ArrayList
is of dynamic size.
|
Arrays
can store primitive data types as well as objects.
|
ArrayList
cannot store the primitive data 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.
Finding the length of the array
Int [] array = new int[4];
System.out.println("The size of the array is " + array.length);
Finding the size of the ArrayList
ArrayList<String> list=new ArrayList<String>();
list.add("ankit");
list.add("nippun");
System.out.println(list.size());
28) How to convert ArrayList to Array and Array to ArrayList?
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()])
29) How to make Java ArrayList Read-Only?
We can obtain java ArrayList Read-only by calling the Collections.unmodifiableCollection() method. When we define an ArrayList as Read-only then we cannot perform any modification in the collection through add(), remove() or set() method.
30) How to remove duplicates from ArrayList?
There are two ways to remove duplicates from the ArrayList.
31) How to synchronize ArrayList?
We can synchronize ArrayList in two ways.
Using Collections.synchronizedList() method
Using CopyOnWriteArrayList<T>