Below are some of the Java Collections Interview Questions and Answers.
Java Collections Interview Questions and Answers
1. What is a collections ?
Collection is an object which manages a group of objects called elements which are of same kind, that is they all descents from common parent type.
2. What are the core collection interfaces?
Collection,Set, SortedSet, List, Queue,Deque and Map. SortedSet and SortedMap interfaces are sorted versions of Set and Map
3. What is an Iterator?
Iterator is an interface that is used to loop through the elements from the collection. It allows you to go through each element in the collection and lets you organize and manage all the elements. This is different for different methods and it is used differently in different conditions. Iterator is not as same as enumeration, but it takes the place of enumeration in the Java Framework.
4. What is Set Interface ?
Set is an unordered collection. Set don’t allow duplicate elements.
5. What are the features of Set Interface?
- The Set interface provides methods to access the elements of a finite mathematical set
- Duplicate elements are not allowed by Sets.
- It contains only the methods inherited from Collection
- If two Set Objects contain same elements, they are said to be equal.
6. What are the types of Sets ?
Java consists of three implementation of it, those are as follows:
- HashSet : stores its element in hash table
- TreeSet : store its element in the tree form
- LinkedHashSet : implemented as a hash table with a linked list running through it
7. What are the features of List Interface ?
List is an ordered collection. List allows duplicates.
8. What are the features of Map Interface ?
Maps are called associative arrays. A Map object describes mappings from Key to value. Map don’t allow duplicates and key can be mapped to one value only.
9. What is a Map Interface?
Map is an interface that provides three collection views, which allows the map’s content to be viewed in different forms like set of keys, collection of values, or set of key-value mappings. It is an object interface that allows the associations between keys and values.
10. What are the implementations of Map?
The implementations of Map are as follows:
11. Explain the concepts of Map and SortedMap interface?
- Keys will be mapped to their values using Map object. Map allows no duplicate values. The keys in a map objects must be unique.
- Java collection framework allows implementing Map interface in three classes namely, HashMap, TreeMap and LinkedHashMap.
- SortedMap is a special interface for maintaining all the elements in a sorted order. This interface extends Map interface.
- It maintains all the elements in ascending order. The sorting process is performed on the map keys.
- It has two additional methods than Map interface. They are firstKey() and lastKey(). Method firstKey() returns the first value available currently in the map, where as the lastKey() returns the last value available currently in the map.
12. What are the interfaces used for ordering objects in collections ?
Comparator and Comparable Interface.
13. What is Comparable Interface ?
Comparable Interface is used to order the objects of a class. We can sort collections that contain objects of class that implement Comparable Interface. Comparable Interface are implemented by java classes like Byte,Long,String,Date and Float.
14. What is Comparator Interface ?
Comparator Interface is used to enhance sorting of objects based on multiple attributes . It can be used to sort objects that do not implement Comparable Interface.
15. How to sort a list ?
To sort a list use Collections.Sort