Java Collections Interview Questions: In Java Collections are the group of objects represents a single entity. A collection is a java object that is used to group both homogeneous and heterogeneous elements. A collection can hold only object types and not primitives. Collection interface extends Iterable in Java. In Java collection declare like below:
public interface Collection<E> extends Iterable<E>
Java Collections Interview Questions and Answers
1) What is the difference between Arrays and Collections?
|1)Arrays are fixed in size.||1)Collections are growable in nature.|
|2)Arrays can hold only homogeneous data.||2)Collections can hold both homogeneous and heterogeneous elements.|
|3)With respect to memory, arrays concept is not recommended to use.||3)With respect to memory, collections are recommended to use.|
|4)With respect to performance, arrays are recommended to use.||4)With respect to performance, collections are not recommended to use.|
|5)Arrays concept is not implemented based on some data structure, hence readymade method support is not available.||5)Every collection class is implemented based on some data structure, hence for every requirement readymade method support is available.|
|6)Arrays can hold both primitives and object type.||6)The collection can hold only object types and not primitives.|
2) What is the Collections API?
A) The Collections API is a set of classes and interfaces that support operations on a collection of objects.
3) Is Collection is an Interface or Class?
A) A collection is an Interface.
4) What is the difference between collection and collections?
A) The difference between Collection and Collections is shown below:
Collection: Collection is an interface in java.util package used to represent a group of as a single entity.
Collections: Collections is a class in java.util package. This class consists exclusively of static methods that operate on or return Collections.
5) What is Cursor in Java?
A) If we want to receive objects one by one from the Collection then we should use Cursor in Java.
6) How many Cursors are there in Java?
A) There are three cursors available in Java. They are:
3) List Iterator.
7) What is an Iterator in Java?
A) The Iterator interface is used to step through the elements of a Collection. Iterators let you process each element of a collection. Iterators are a generic way to go through all the elements of a collection no matter how it is organized. An iterator is an interface implemented a different way for every Collection.
8) How do you traverse through a collection using its Iterators?
A) To use an Iterator to traverse through the contents of a collection, follow these step:
a) Obtain an Iterator to the start of the collection by calling the collection iterator() method.
b) Set up a loop that makes a call to hasNext(). Have the loop iterate as long as hasNext() returns true.
c) Within the loop, obtain each element by calling next().
9) What are the methods present in Iterator in Java?
A) There are four methods present in Iterator. They are:
4) forEachRemaining( Consumer<? superE> action).
10) How do you remove elements during Iterator?
A) Iterator also has a method remove() when remove is called, the current element in the iteration is deleted.
11) What is the List Iterator in Java?
A) List Iterator is just like Iterator, except that it allows us to access the collection in either the forward or backward direction and lets us modify an element.
12) What are the methods present in List Iterator?
A) There are nine methods present in List Iterator. They are:
1) add( E e).
9) set( E e).
13) What is an Enumeration in Java?
A) The Enumeration interface defines the methods by which you can enumerate ( obtain one at a time ) the elements from legacy collection objects. We can create Enumeration object by using elements method.
Example: Enumeration e = v.elements();
14) What are the methods present in Enumeration Interface?
A) There are three methods in Enumeration Interface. They are:
15) How Can Enumeration be converted into an Iterator?
A) An Enumeration can be converted into an Iterator by using the Enumeration.asIterator() method.
16) What is the List Interface in Java?
A) The List interface provides support for ordered collections of objects. Lists may contain duplicate elements.
17) What is the Comparable Interface?
A) Comparable Interface is used to order the Objects of user-defined Class. It provides only single sorting sequence.
18) What is the Comparator in Java?
A) The comparator is an interface present in java.util package. The comparator object reference can be used to define our own customized sorting order.
19) What is Sorted Map in Java?
A) Sorted Map is a subinterface of Map. If we want to represent a group of key-value pairs according to sorting order of keys. In Sorted Map we can perform sorting only based on the keys but not values.
20) Which interface provides the capability to store objects using a key-value pair?
21) What is the use of descending keyset()?
A) The descending keyset() method returns a NavigableSet in which the order of the elements is reversed compared to the original key set.
22) What is the use of descendingMap()?
A) The descendingMap() method returns a NavigableMap which is a view of the original Map. The order of the elements in this view map is reverse of the order of the original map.
23) What is the use of headMap()?
A) The headMap() method returns a view of the original NavigableMap which only contains elements that are less than the given element.
24) What is the main implementation of the List interface?
A) The main implementation of the List interface are as follows:
- Array List: Resizable-array implementation of the List interface. The best all-around implementation of the List interface.
- Vector: Synchronized resizable-array implementation of the List interface with additional “Legacy methods”.
- Linked List: Doubly-linked list implementation of the List interface. May provide better performance than the Array list implementation if elements are frequently inserted or deleted within the list. Useful for queues and double-ended queues (deques ).
25) What is the advantage of Array List over arrays?
A) Some of the advantages Array List has over arrays are:
- It can grow dynamically.
- It provides more powerful insertion and search mechanisms than arrays.
26) Difference between Array List and Vector?
|1) Array List is Not synchronized by default.||1) Vector List is synchronized by default.|
|2) Array List can use the only Iterator to access the elements.||2) Vector list can use Iterator and Enumeration Interface to access the elements.|
|3) The Array List increases its array size by 50 percent if it runs out of the room.||3) A Vector defaults to doubling the size of its array if it runs out of the room.|
|4) Array List has no default size.||4) While vector has a default size of 10.|
27) How to obtain Array from an Array List?
A) An array can be obtained from an Array List using toArray() method on Array List.
List a = new ArrayList();
Object a = a.toArray();
Java Collections Interview Questions and FAQ’s
28) What is the difference between the List and Set?
A) A list can contain duplicate elements whereas Set contains unique elements only.
29) How do you decide, when to use Array List and when to use Linked List?
A) If you need to support random access, without inserting or removing elements from any place other than the end, then Array List offers the optimal collection. If however, you need to frequently add and remove elements from the middle of the list and only access the list elements sequentially, then linked list offers the better implementation.
30) What is the Set Interface in Java?
A) The Set Interface provides methods for accessing the elements of a finite mathematical set. Sets do not allow duplicate elements. Contains no methods other than those inherited from the collection. It adds the restriction that duplicate elements are prohibited. Two Set objects are equal if they contain the same elements.
31) What are the main implementations of the Set Interface?
A) The main implementation of the Set interface is as follows:
- Linked HashSet
32) What is a HashSet in Java?
A) A HashSet is an unsorted, unordered Set. It uses the hash code of the object being inserted (so the more efficient your hashcode() implementation, the better access performance you’ll get). Use this class when you want a collection with no duplicates and you don’t care about the order when you iterate through it.
33) What is a TreeSet in Java?
A) TreeSet is a Set implementation that keeps the elements in sorted order. The elements are sorted according to the natural order of elements or by the comparator provided at creation time.
34) What is an EnumSet in Java?
A) An EnumSet is a specialized set for use with enum types, all of the elements in the EnumSet type that is specified, explicitly or implicitly, when the set is created.
35) What is the difference between HashSet and TreeSet?
1) HashSet is under Set interface i.e. it does not guarantee for either sorted order or sequence order.
|1) TreeSet is under Set i.e. it provides elements in a sorted order(acceding order).|
36) What is a Map in Java?
A) A map is an object that stores associations between keys and values (key/value pairs). Given a key, you can find its value. Both keys and values are objects. The key must be unique, but the values may be duplicated. Some maps can accept a null key and null values, others cannot.
37) What is the main Implementation of the Map Interface?
A) The main implementation of the List Interface is as follows:
38) What is the difference between HashMap and when to use TreeMap?
|1) HashMap lets you have null values as well as one null key.||1) Hashtable does not allow null values as key and value.|
|2) The iterator in the HashMap is fall-safe (If you change the map while iterating, you’ll know).||2)The enumerator for the HashTable is not fall-safe.|
|3) HashMap is unsynchronized.||3) HashTable is synchronized.|
39) What is the difference between HashMap and WeakHashMap?
A) HashMap: Object is not eligible for garbage collection if it is associated with Hashmap. Even though it does not have any external references.
WeakHashMap: If an Object is not having any external references then it is always eligible for garbage collector even though it is associated with WeakHashMap.
Frequently Asked Java Collections Interview Questions
40) What is Stack in Java?
A) The stack is a child class of vector. If we want to represent object according to last in first out order then we should go far, Stack.
41) What is ceiling key?
A) The ceilingKey() method returns the least(smallest) key in the Map that is greater than or equal to the element passed as a parameter to the ceilingKey() method.
42) What is the PollFirstEntry()?
A) The PollFirstEntry() method returns and removes the first entry (key + values) in the NavigableMap or null if the map is empty.
43) What is the use of PollLastEntry?
A) The PollLastEntry returns and removes the last element in the map or null if the map is empty.
44) How does a Hashtable internally maintain the key-value pairs?
A) TreeMap actually implements the SortedMap interface which extends the Map interface. In a Treemap, the data will be sorted in acceding order of keys according to the natural order for the key’s class, or by the comparator provided at creation time. Treemap is based on the Red-Black tree data structure.
45) What are the different collection views that Maps provide?
A) A Map provides three Collection Views.
- Key Set – allow a map’s contents to be viewed as a set of keys.
- Values collection – allow a map’s contents to be viewed as a set of values.
- Entry Set – allow a map’s contents to be viewed as a set of key-value mappings.
46) What is a Keyset view?
A) The keyset is a set returned by the KeySet() method of the Map Interface, it is a set that contains all the keys present in the Map.
47) What is a values Collection Views?
A) Values Collection Views is a collection returned by the Values() method of the Map Interface, it contains all the objects present as values in the map.
48) What is an Entry Set View?
A) Entry Set View is a set that is returned by the entryset() method in the map and contains objects of type map. Entry, each of which has both Key and Value.
49) What is the difference between HashSet and LinkedHashSet?
A) HashSet does not maintain Insertion order of Objects. LinkedHashSet maintains Insertion order of objects.
50) How do you sort an ArrayList (or any list) of user-defined objects?
A) Create an implementation of the java.lang.Comparable interface that knows how to order your objects and pass it to java.util.Collections.sort(List, comparator).