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
1)
Java array is fixed in size i.e the size of the
array can't be increased and decreased once when it is declared.
2)
We can store both primitive types and objects in
an array.
3)
Array consumes more memory.
4)
Performance point of view array is faster than a
collection.
Collection
1)
Java collection is dynamic in nature i.e it can
grow their size as needed.
2)
Collection store only objects.
3)
It consumes less memory.
4)
Performance point of view collection is not
faster than an array.
(3) Difference between
HashSet and TreeSet?
HashSet
1)
HashSet doesn't maintain any order of elements.
2)
The performance of HashSet is better for
operations like add, remove, retrieve, etc.
TreeSet
1)
TreeSet maintains the ascending order of an
element.
2)
The performance is not better than HashSet for
the operations like add, remove, retrieve, etc.
(4) Difference
between Iterator and ListIterator?
Iterator
1)
Iterator is the super interface of ListIterator
interface.
2)
By the help of iterator interface, we can
traverse the elements only in forward direction.
3)
Iterator can be used in List, Set, and Queue.
ListIterator
1)
ListIterator is the sub-interface of Iterator
interface.
2)
By the help of list iterator interface, we can
traverse the elements in both direction i.e forward and backward directions.
3)
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
1)
Iterator interface
2)
ListIterator interface
3)
Enumeration interface
(6) Difference
between Iterator and Enumeration?
Iterator
1)
Iterator can traverse both legacy and non-legacy
elements.
2)
Iterator is a fail-fast.
3)
Iterator is slower than enumeration.
Enumeration
1)
Enumeration interface mainly used for legacy
classes.
2)
Enumeration is not fail-fast.
3)
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?
1)
Java comparable
interface belongs to the java.lang package whereas java comparator interface belongs to the java.util package.
2)
Java comparable
provides only one sort of sequence whereas Java comparator interface provides multiple sorts of sequences.
3)
In java comparable,
there is one method compareTo() and in Java comparable, there is compare() method.
(9) Difference
between ArrayList and Vector in java?
ArrayList
1)
ArrayList is a legacy class in java collection.
2)
ArrayList class is not synchronized.
3)
ArrayList class increases its size by 50% of the
array size.
Vector
1)
Vector class is a legacy class in java
collection.
2)
Vector class is synchronized class.
3)
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
1)
HashMap class implements Map interface.
2)
HashMap class contains elements in the key-value
combination.
HashSet
1)
HashSet class implements Set interface.
2)
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
1)
ArrayList uses a dynamic array to store the
elements.
2)
ArrayList is good for the store and fetch data.
LinkedList
1)
LinkedList uses a doubly linked list to store
data.
2)
LinkedList is good for manipulation of the data.
(12) Difference
between HashMap and Hashtable?
HashMap
1)
HashMap class is not synchronized.
2)
HashMap class allows one null key and multiple
null values.
Hashtable
1)
Hashtable class is synchronized.
2)
Hashtable class doesn't allow any null key and
null values.
(13) Difference
between List and Set?
List
1)
List interface contains dupicate elements.
2)
ArrayList, LinkedList, and Vector class
implement List interface.
Set
1)
Set interface does not contains duplicate
elements.
2)
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?
1)
For the array of objects, we will use
Arrays.sort() method.
2)
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.