20 Java Collections Interview Questions: 1) What Is The Difference Between Arraylist and Vector?
20 Java Collections Interview Questions: 1) What Is The Difference Between Arraylist and Vector?
20 Java Collections Interview Questions: 1) What Is The Difference Between Arraylist and Vector?
In java, collection interview questions are mostly asked by the interviewers. Here is the
list of mostly asked collections interview questions with answers.
3) ArrayList increases its size by 50% of the array size. Vector increases its size by doubl
2 ArrayList is not efficient for manipulation because a lot of shifting is LinkedList is effi
) required. manipulation.
Iterator traverses the elements in forward direction only whereas ListIterator traverses
the elements in forward and backward direction.
1 Iterator traverses the elements in forward ListIterator traverses the elements in back
) direction only. directions both.
2 Iterator can be used in List, Set and Queue. ListIterator can be used in List only.
)
1) Iterator can traverse legacy and non-legacy elements. Enumeration can traverse only
List can contain duplicate elements whereas Set contains only unique elements.
Set contains values only whereas Map contains key and values both.
HashSet contains only values whereas HashMap contains entry(key,value). HashSet can
be iterated but HashMap need to convert into Set to be iterated.
2 HashMap can contain one null key and multiple null values. Hashtable cannot contain any
)
If you change the value in properties file, you don't need to recompile the java class. So,
it makes the application easy to manage.
14) 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 same.
But, it is possible that two hash code numbers can have different or same keys.
The equals method is used to check whether two objects are same or not. It needs to be
overridden if we want to check the objects based on property.
For example, Employee is a class that has 3 data members: id, name and salary. But, we
want to check the equality of employee object on the basis of salary. Then, we need to
override the equals() method.
Yes, Collections class provides methods to make List, Set or Map elements as
synchronized:
If we use generic class, we don't need typecasting. It is typesafe and checked at compile
time.
18) What is hash-collision in Hashtable and how it is handled in
Java?
Two different keys with the same hash value is known as hash-collision. Two different
entries will be kept in a single hash bucket to avoid the collision.
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.