Java collections interview questions Set 7
Categories: Java 8(JDK1.8)
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.
a) Separate Chaining
b) Open Addressing
What is the Dictionary class?
The Dictionary class provides the capability to store key-value pairs.
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.
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.
What is the difference between Array and ArrayList?
The main differences between the Array and ArrayList are given below.
SNArrayArrayList
1The 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.
2Arrays are of the static type.ArrayList is of dynamic size.
3Arrays can store primitive data types as well as objects.ArrayList cannot store the primitive data types it can only store the objects.
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());
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()])
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.
How to synchronize ArrayList?
We can synchronize ArrayList in two ways.
a) Using Collections.synchronizedList() method
b) Using CopyOnWriteArrayList<T>
When to use ArrayList and LinkedList?
LinkedLists are better to use for the update operations whereas ArrayLists are better to use for the search operations.