Java Collections Interview Questions
Now, We are going to discuss the most important topic of core java interview which is java collection framework. Here we will discuss collection framework in java interview questions and answers in detail.
(1) What is a collection in java?
Collection in java is a framework and collection allows a group of objects to be treated as a single unit. These objects can be stored, retrieved and manipulated as elements of the collection.
(2) Difference between Array and Collection?
Array
- Java array is fixed in size i.e the size of the array can't be increased and decreased once when it is declared.
- We can store both primitive types and objects in an array.
- Array consumes more memory.
- Performance point of view array is faster than a collection.
collection
- Java collection is dynamic in nature i.e it can grow their size as needed.
- Collection store only objects.
- It consumes less memory.
- Performance point of view collection is not faster than an array.
(3) Difference between HashSet and TreeSet?
HashSet
- HashSet doesn't maintain any order of elements.
- The performance of HashSet is better for operations like add, remove, retrieve, etc.
TreeSet
- TreeSet maintains the ascending order of an element.
- The performance is not better than HashSet for the operations like add, remove, retrieve, etc.
(4) Difference between Iterator and ListIterator?
Iterator
- Iterator is the super interface of ListIterator interface.
- By the help of iterator interface, we can traverse the elements only in forward direction.
- Iterator can be used in List, Set, and Queue.
ListIterator
- ListIterator is the sub-interface of Iterator interface.
- By the help of list iterator interface, we can traverse the elements in both direction i.e forward and backward directions.
- ListIterator can be used with the List interface.
(5) How many ways, you can retrieve elements from a collection?
There are many ways we can retrieve the elements from the collection.
- foreach loop
- Iterator interface
- ListIterator interface
- Enumeration interface
(6) Difference between Iterator and Enumeration?
Iterator
- Iterator can traverse both legacy and non-legacy elements.
- Iterator is a fail-fast.
- Iterator is slower than enumeration.
Enumeration
- Enumeration interface mainly used for legacy classes.
- Enumeration is not fail-fast.
- It is faster than Iterator interface
(7) Difference between Collection and Collections?
Collection is an interface whereas collections is a class in java.
(8) Difference between Comparable and Comparator?
Java comparable interface belongs to the java.lang package whereas java comparator interface belongs to the java.util package.
Java comparable provides only one sort of sequence whereas Java comparator interface provides multiple sorts of sequences.
In java comparable, there is one method compareTo() and in Java comparator, there is compare() method.
(9) Difference between ArrayList and Vector in java?
ArrayList
- ArrayList is a legacy class in java collection.
- ArrayList class is not synchronized.
- ArrayList class increases its size by 50% of the array size.
Vector
- Vector class is a legacy class in java collection.
- Vector class is synchronized class.
- Vector class increases its size by doubling the array size.
(10) Difference between HashMap and HashSet?
There is some difference between java HashMap and HashSet class.
HashMap
- HashMap class implements Map interface.
- HashMap class contains elements in the key-value combination.
HashSet
- HashSet class implements Set interface.
- HashSet class contains only single value, there is no key-value combination.
(11) Difference between ArrayList and LinkedList?
There are some differences between ArrayList and LinkedList class.
ArrayList
- ArrayList uses a dynamic array to store the elements.
- ArrayList is good for the store and fetch data.
LinkedList
- LinkedList uses a doubly linked list to store data.
- LinkedList is good for manipulation of the data.
(12) Difference between HashMap and Hashtable?
HashMap
- HashMap class is not synchronized.
- HashMap class allows one null key and multiple null values.
Hashtable
- Hashtable class is synchronized.
- Hashtable class doesn't allow any null key and null values.
(13) Difference between List and Set?
List
- List interface contains duplicate elements.
- ArrayList, LinkedList, and Vector class implement List interface.
Set
- Set interface does not contains duplicate elements.
- HashSet, LinkedHashSet, and TreeSet implements Set interface.
(14) Difference between HashMap and TreeMap?
Java HashMap does not maintain insertion order of an element whereas Java TreeMap maintains the elements in ascending order.
(15) What is properties file?
Java properties file maintain the data in the form of key-value pair. We can create properties file using notepad.
(16) Difference between fail-fast and fail-safe?
Java fail-fast iterator throws ConcurrentModificationException when there is a structural modification in underlying collection whereas fail-safe does not throw any exception when they detect any structural modification as fail-safe iterators work with clones of underlying collection.
(17) How we can sort a list of an object?
- For the array of objects, we will use Arrays.sort() method.
- For the collection of objects, we will use collections.sort().
This java collection framework interview questions and answers will help you in any java collection interview related questions.
Great java blog. Thanks
ReplyDeletechange the 3rd sentence of difference between comparator & comparable
ReplyDeleteThank you for this correction chandra.
Delete