Automation Using Selenium Webdriver

Friday 23 December 2016

Top 20 collection framework interview questions and answers in java

Java collections interview questions:


1.Why Map interface doesn’t extend Collection interface?

Set is unordered collection and does not allows duplicate elements.
List is ordered collection allows duplicate elements.
Where as Map is key-value pair.
It is viewed as set of keys and collection of values.
Map is a collection of key value pairs so by design they separated from collection interface.

2.What is difference between HashMap and Hashtable?

  • Synchronization or Thread Safe 
  • Null keys and null values 
  • Iterating the values 
  •  Default Capacity 
Hashmap%2Bvs%2Bhashtable

3.Differences between comparable and comparator?

Comparable Interface is actually from java.lang package.
It will have a method compareTo(Object obj)to sort objects
Comparator Interface is actually from java.util package.
It will have a method compare(Object obj1, Object obj2)to sort objects

4.How can we sort a list of Objects?

 To sort the array of objects we will use  Arrays.sort() method.
If we need to sort collection of object we will use Collections.sort().

5.What is difference between fail-fast and fail-safe?

Fail fast is nothing but immediately report any failure. whenever a problem occurs fail fast system fails.
in java Fail fast iterator while iterating through collection of objects sometimes concurrent
modification exception will come there are two reasons for this.
If one thread is iterating a collection and another thread trying to modify the collection.
And after remove() method call if we try to modify collection object


6. What is difference between Iterator ,ListIterator and Enumeration?

Enumeration interface implemented in java 1.2 version.So Enumeration is legacy interface.
Enumeration uses elements() method.
Iterator is implemented on all Java collection classes.
Iterator uses iterator() method.
Iterator can traverse in forward direction only.
ListIterator is implemented only for List type classes
ListIterator uses listIterator() method.
Read more :
What is difference between Iterator ,ListIterator and Enumeration?

7.What is difference between Set and List in Java?

A set is a collection that allows unique elements.
Set does not allow duplicate elements
Set allows only one null value.
Set having classes like :
HashSet
LinkedHashSet
TreeSet
List having index. and ordered  collection
List allows n number of null values.
List will display Insertion order with index.
List having classes like :
Vector
ArrayList
LinkedList
8.Differences between arraylist and vector?

Vector was introduced in  first version of java . that's the reason only vector is legacy class.
ArrayList was introduced in java version1.2, as part of java collections framework.
Vector is  synchronized.
ArrayList is not synchronized.
Read more: Differences between arraylist and vector
9.What are the classes implementing List interface?

ArrayList  
LinkedList  
Vector
10. Which all classes implement Set interface ?

HashSet
LinkedHashSet
TreeSet
11.How to make a collection thread safe?

Vector, Hashtable, Properties and Stack are synchronized classes, so they are thread-safe and can be
used in multi-threaded environment.
By using Collections.synchronizedList(list)) we can make list classes thread safe.
By using
java.util.Collections.synchronizedSet()  we can make set classes thread safe.
12.Can a null element added to a TreeSet or HashSet?

One null element can be added to hashset.
TreeSet does not allow null values
13. Explain Collection’s interface hierarchy?



14.Which design pattern Iterator follows?

Iterator design pattern

15.Which data structure HashSet implements

Hashset implements hashmap internally.

16.Why doesn't Collection extend Cloneable and Serializable?

List and Set and queue extends Collection interface.
SortedMap extends Map interface.

17.What is the importance of hashCode() and equals() methods? How they are used in Java?

equals() and hashcode() methods defined in "object" class.
If equals() method return true on comparing two objects then hashcode() of those two objects must be same.

18.What is difference between array & arraylist?

Array is collection of similar type of objects and fixed in size.
Arraylist is collection of homogeneous and heterogeneous elements.

19.What is the Properties class?

Properties is a subclass of Hashtable. It is used to maintain lists of values in which the key and the value is String.

20.How to convert a string array to arraylist?

ArrayList al=new ArrayList( Arrays.asList( new String[]{"java", "collection"} ) );
 arrayList.toArray(); from list to array

2 comments: