Site icon Narayana Tutorial

Java Collection Framework Interview Questions and Answers


Here I am going to sharing list of Java Collection Framework Interview Questions and Answers. Which are asking in the interview frequently.

Java Collection Framework Interview Questions and Answers

What is a collection framework?

A collection framework is a class, library to handle group of objects. Collection framework is implemented in java.util package.

Does a collection object store copies of other objects or references?

A collection object stores references of other objects.

Can you store a primitive data type into a collection?

No. Collections store only objects.

What is difference between Iterator and ListIterator?

Both are useful to retrieve elements from a collection.

Iterator

  1. Iterator can retrieve the only in forward direction.
  2. Iterator can be used in List, Set and Queue.

ListIterator

  1. ListIterator can retrieve the elements in forward and backward direction also. So ListIterator is preferred to Iterator.
  2. ListIterator can be used in List only.
What is difference between Iterator and Enumeration?

Both are useful to retrieve elements from a collection.

Iterator
1. Iterator has methods whose names are easy to follow.
2. Iteration has an option to remove element from the collection
Enumeration
1. Enumeration has method whose names are difficult to follow and to remember
2. Enumeration has an no option to remove element from the collection

So Iterator is preferred to Enumeration .

What is autoboxing?

Converting a primitive data type into a object form automatically is called Auto Boxing.

Auto Boxing is done in generic types.

What is difference between ArrayList and Vector?

ArrayList
1. ArrayList object is not synchronized by default.
2. In case of a single thread, using ArrayList is faster than Vector.
3. ArrayList increases its size every time by 50 percent (Half)

Vector

1. Vector object is synchronized by default.
2. In case of a multiple threads, using Vector advisable. With a single thread, Vector becomes slow
3. Vector increases its size every time by doubling it.

Can you synchornize the ArrayList object?

Yes. We can use synchronizedList() method to synchronize the ArrayList like as follows.
Collections.synchronizedList(new ArrayList());

What is difference bewtween HashMap and Hashtable?

HashMap

  1. HashMap object is not synchronized by default.
  2. In case of single thread, using HashMap is faster than Hashtable
  3. HashMap allows null keys and null values to be stored. ( one null key and more null values allowed)
  4. Iterator in the HashMap is full-fast. This means Iterator will produce exception if concurrent updates are made to the HashMap.

Hashtable

  1. Hashtable object is synchronized by default.
  2. In case of multiple threads, using Hashtable is advisable. With a single thread, Hashtable becomes slow.
  3. Hashtable does not allow null keys and null values to be stored.
  4. Enumeration for the Hashtable is not full-fast. This means if concurrent updates are done to the Hashtable. There will not be any incorrect results produced by the Enumeration.
What is load factor for a HashMap or Hashtable?

Load Factor is 0.75

The default initial capacity of the HashMap will be taken as 16 and the load factor as 0.75. Load factor represents at what level HashMap capacity should be doubled.

Example

The product of capacity and load factor = 16 * 0.75 = 12

This represents that after storing the 12th  key-value pair into HashMap, its capacity will become 32.

Can you make HashMap synchronized?

Yes.

We can make HashMap object synchronized using synchronizedMap() method as shown here.

Collections.synchronizedMap(new HashMap)

What is difference between Set and List?

Set

  1. A Set represents a collection of elements. Order of the elements may change in the set.
  2. Set will not allow duplicate values to be stored.
  3. Accessing elements by their index (position number) is not possible in case of sets.
  4. Sets will not allow null elements.

List

  1. A List represents ordered collection of elements. List preserves the order of the elements in which they are entered.
  2. List will allow duplicate values to be stored.
  3. Accessing elements by their index (position number) is possible in case of List.
  4. List will allow null elements to be stored.
What is difference between Set and Map?

Set
Set contains values only.
Map
Map contains key and values both.

What is the difference between HashMap and TreeMap?

HashMap

It does not follow order to store elements

TreeMap

It follow ascending order by default to store elements.

What is the difference between Collection and Collections?

Collection

  1. Collection is an interface whereas
  2. Collection interface provides normal functionality of data structure to List, Set and Queue

Collections

  1. Collections is a class. .
  2. Collections class is to sort and synchronize collection elements.
What is hash-collision in Hashtable?

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.

How to synchronize List, Set and Map elements?

Collections class provides methods to make List, Set or Map elements as synchronized.

  1. public static List synchronizedList(List l){}
  2. public static Set synchronizedSet(Set s){}
  3. public static SortedSet synchronizedSortedSet(SortedSet s){}
  4. public static Map synchronizedMap(Map m){}
  5. public static SortedMap synchronizedSortedMap(SortedMap m){}
Difference between Comparator and Comparable in Java

Comparable

  1. Sorting logic must be in same class whose objects are being sorted. Hence this is called natural ordering of objects
  2. Class whose objects to be sorted must implement this interface.
  3. It provides one method named compareTo().
  4. It is found in java.lang package. i.e java.lang.Comparable
  5. Collections.sort(List). Here objects will be sorted on the basis of CompareTo method

Comparator

  1. Sorting logic is in separate class. Hence we can write different sorting based on different attributes of objects to be sorted. E.g. Sorting using id,name etc.
  2. Class whose objects to be sorted do not need to implement this interface. Some other class can implement this interface.
  3. It provides one method named compare().
  4. It is found in java.util package. i.e java.util.Comparator 
  5. Collections.sort(List, Comparator). Here objects will be sorted on the basis of Compare method in Comparator