Showing posts with label collections. Show all posts
Showing posts with label collections. Show all posts

Saturday, 31 May 2025

Beginners Level(0-1 yrs ) Interview Question based on Java Collections

What is the root interface in collection hierarchy ?
Root interface in collection hierarchy is Collection interface .

What is the difference between Collection and Collections ?
Collection is  an interface while Collections is a java class , both are present in java.util package and  part of java collections framework.

Which collection classes are synchronized or thread-safe ?
Stack, Properties , Vector and Hashtable can be used in multi threaded environment because they are synchronized classes (or thread-safe).

Name the core Collection  interfaces ?
The list of core collection interfaces are : just mention the important ones
Important : Collection , Set , Queue , List , Map
Maps do not extend this interface, yet they are part of java collection framework
Other interface also in the list :  SortedSet, SortedMap , Deque, ListIterator etc.

What is the difference between List and Set ?
Set contain only unique elements while List can contain duplicate elements.
Set is unordered while List is ordered . List maintains the order in which the objects are added .

What is the difference between Map and Set ?
Map object has unique keys each containing some value, while Set contain only unique values.

What are the classes implementing List and Set interface ?
Class implementing List interface :  ArrayList , Vector , LinkedList ,
Class implementing Set interface :  HashSet , TreeSet

What is an iterator ?
Iterator is an interface . It is found in java.util package. It provides methods to iterate over any Collection.

What is the difference between Iterator and Enumeration ?

The main difference between Iterator and Enumeration is that Iterator has remove() method while Enumeration doesn't.
Hence , using Iterator we can manipulate objects by adding and removing the objects from the collections. Enumeration behaves like a read only interface as it can only traverse the objects and fetch it .

Which design pattern followed by Iterator ?
It follows iterator design pattern. Iterator design pattern provides us to navigate through the collection of objects by using a common interface without letting us know about the underlying implementation.
Enumeration is an example of Iterator design pattern.

Which methods you need to override to use any object as key in HashMap ?
To use any object as key in HashMap , it needs to implement equals() and hashCode() method .

What is the difference between Queue and Stack ?
Queue is a data structure which is based on FIFO ( first in first out ) property . An example of Queue in real world is buying movie tickets in the multiplex or cinema theaters.
Stack is a data structure which is based on LIFO (last in first out) property . An example of Stack in real world is  insertion or removal of CD  from the CD case.

How to reverse the List in Collections ?
There is a built in reverse method in Collections class . reverse(List list) accepts list as parameter.
Collections.reverse(listobject);

How to convert the array of strings into the list ?
Arrays class of java.util package contains the method asList() which accepts the array as parameter.
So,

String[]  wordArray =  {"Hello" , "World" , "from Java"};
List wordList =  Arrays.asList(wordArray);

Saturday, 3 December 2016

Intermediate Level(1-3 yrs ) Interview Question based on Java Collections

The collection framework of Java is asked in almost every Java interview. In the last post I shared some of the collection questions which are asked the fresher level or below 1 year of experience. If you haven't gone through that here is the link for that.Java Collection Interview Question(0-1 Years). Below are some of questions that are asked at some experience level.

 

What is the difference between ArrayList and Vector ?


Vector is synchronized while ArrayList is not . Vector is slow while ArrayList is fast . Every time when needed, Vector increases the capacity twice of its initial size while ArrayList increases its ArraySize by 50%.

 

What is the difference between HashMap and Hashtable ?


a. HashMap allows one null key and any number of null values while Hashtable does not allow null keys and null values.
b. HashMap is not synchronized or thread-safe while Hashtable is synchronized or thread-safe .

 

What is the difference between peek(),poll() and remove() method of the Queue interface ?


Both poll() and remove() method is used to remove head object of the Queue. The main difference lies when the Queue is empty().
If Queue is empty then poll() method will return null . While in similar case , remove() method will throw NoSuchElementException .
peek() method retrieves but does not remove the head of the Queue. If queue is empty then peek() method also returns null.

 

What is the difference between Iterator and ListIterator.


Using Iterator we can traverse the list of objects in forward direction . But ListIterator can traverse the collection in both directions that is forward as well as backward.

 

What is the difference between Array and ArrayList in Java ?


a. Array is static in size while ArrayList is dynamic in size.
b. Array can contain primitive data types while ArrayList can not contain primitive data types.When you add any primitive data type to ArrayList, it will automatically convert it to its corresponding Wrapper class(auto Boxing).

 

What is the difference between HashSet and TreeSet ?


a.  HashSet maintains the inserted elements in random order while TreeSet maintains elements in the sorted order
b. HashSet can store null object while TreeSet can not store null object.

 

What is the difference between HashMap and ConcurrentHashMap ?


Main differences between HashMap and ConcurrentHashMap are :
a. HashMap is not synchronized while ConcurrentHashMap is synchronized.
b. HashMap can have one null key and any number of null values while ConcurrentHashMap does not allow null keys and null values .

 

What is the difference between LinkedList and ArrayList in Java ?


Main differences between LinkedList and ArrayList are :
a. LinkedList is the doubly linked list implementation of list interface , while , ArrayList is the resizable array implementation of list interface.
b. LinkedList can be traversed in the reverse direction using descendingIterator() method  provided by the Java Api developers , while , we need to implement our own method to traverse ArrayList in the reverse direction.

 

Why Map interface does not extend the Collection interface in Java Collections Framework ?


Map interface is not compatible with the Collection interface.
Explanation : Since Map requires key as well as value , for example , if we want to add key-value pair then we will use put(Object key , Object value) . So there are two parameters required to add element to the HashMap object  . In Collection interface add(Object o) has only one parameter.
The other reasons are Map supports valueSet , keySet as well as other appropriate methods which have just different views from the Collection interface.

 

When to use ArrayList and when to use LinkedList in application?


ArrayList has constant time get operation O(1).Hence, ArrayList is preferred when the list is collection required more querying than modifying.
Insertion , Deletion operations take constant time O(1) for LinkedList. Hence, LinkedList is preferred when there are more insertions or deletions involved in the application.

Suggested Posts:

Interview Questions on List Interface 

 

Tuesday, 11 October 2016

List Interface - An Interview based approach.

List is all about indexes and index based access of elements. The one thing that List has that non-lists don't have is a set of methods related to the index. Those key methods include things like get(int index) , indexOf(Object o) , add(int index, Object obj) , and so on. The three List implementations are ordered by index position—a position that you determine either by setting an object at a specific index or by adding it without specifying position, in which case the object is added to the end. The three List implementations are described in the following sections.

ArrayList : ArrayList can be considered as dynamically resizing array. It gives you fast iteration and fast random access. To state the obvious: it is an ordered collection (by index), but not sorted. It implements RandomAccess interface—a marker interface that says, "this list supports fast (generally constant time) random access.

Vector : Vector is a holdover from the earliest days of Java; Vector and Hashtable were the two original collections, the rest were added with Java 2 versions 1.2 and 1.4. A Vector is basically the same as an ArrayList, but Vector methods are synchronized for thread safety. You'll normally want to use ArrayList instead of Vector because the synchronized methods add a performance hit you might not need. And if you do need thread safety, there are utility methods in class Collections that can help. Vector is the only class other than ArrayList to implement RandomAccess.

LinkedList A LinkedList is ordered by index position, like ArrayList, except that the elements are doubly-linked to one another. This linkage gives you new methods (beyond what you get from the List interface) for adding and removing from the beginning or end, which makes it an easy choice for implementing a  stack or queue. Remenber that a LinkedList may iterate more slowly than an ArrayList, but it's a good choice when you need fast insertion and deletion. As of Java 5, the LinkedList class has been enhanced to implement the java.util.Queue interface. As such, it now supports the common queue methods: peek() , poll() , and offer() .

ArrayList Vs LinkedList
  1.  Both are concrete implementations of List interface. 
  2.  ArrayList provided random access where as LinkedList provide sequential access.
  3.  Both LinkedList and ArrayList maintains the insertion order.
  4. Add operation of ArrayList is slower than that of LinkedList because it may involve resizing of array which requires creating new array and copying elements to new array.
  5. Get Opration of ArrayList is much faster than LinkedList. Time complexity of get operation of ArrayList is O(1) ie. constant time wheres as that of LinkedList is O(n)
  6. Both provide fail-fast iterator and throw concurrent modification exception.
Imporant : ArrayList is preferred when the list manipulation is done less frequently and the get operation is performed more frequently whereas LinkedList is preferred when the frequency of list manipulation is more than the frequency of get operation on the list.