Java Collection Framework Questions & Answers
What is Java Collection Framework?
A java collection framework is a collection of interfaces and classes which are used to store and
process data efficiently, as a Collection is something which is used to store Data objects.
This framework was introduced in Java version 1.2.
-> Reduced the development effort.
-> Code quality is enhanced.
This framework was introduced in Java version 1.2.
-> Reduced the development effort.
-> Code quality is enhanced.
What are the basic interfaces of collection framework?
1. Collection
2. List
3. Set
4. Queue
5. Map
2. List
3. Set
4. Queue
5. Map
Why Map Interface does not extend Collection interface?
The Map is way different than collection. In Map there are no elements, it has key-value pairs.
👉🏻 It does not fit into the Group of elements Paradigm.
However there are many methods to retrieve keys and values as collection.
👉🏻 It does not fit into the Group of elements Paradigm.
However there are many methods to retrieve keys and values as collection.
What is the difference between Collection and Collections?
'There is no need to do it!'
Collection is not supposed to do what Cloneable and Serializable interfaces do.
What they do?
They are just the marker interfaces which are actually empty interfaces.
👉🏻 If Collection Interface implements these interfaces then it will mandate cloning and serialization in all implementation, which is less flexible and more restrictive.
Collection is not supposed to do what Cloneable and Serializable interfaces do.
What they do?
They are just the marker interfaces which are actually empty interfaces.
👉🏻 If Collection Interface implements these interfaces then it will mandate cloning and serialization in all implementation, which is less flexible and more restrictive.
When we had Array, Why do we need collection?
We know Array is a group of primitives.. which holds homogeneous data.
It has also some limitations like, arrays are always fixed in size.
So to overcome these problems, collection were introduced.
Collection, which can also be called a container, is a group of individual objects.
How can it solve the problems that we had in array?
Collections are grow-able in Nature,
that means we aren't bound to declare size of any collection at declaration as we are with arrays.
Size of collections can be increased or decreased on the base of our programming requirement.
You can add or remove any number of elements in collection.
hence collections solve the first limitation.
And next,
we only can have homogeneous type of data elements in array,
But Collections can have any kind of data element. They can be homogeneous or heterogeneous.
It has also some limitations like, arrays are always fixed in size.
So to overcome these problems, collection were introduced.
Collection, which can also be called a container, is a group of individual objects.
How can it solve the problems that we had in array?
Collections are grow-able in Nature,
that means we aren't bound to declare size of any collection at declaration as we are with arrays.
Size of collections can be increased or decreased on the base of our programming requirement.
You can add or remove any number of elements in collection.
hence collections solve the first limitation.
And next,
we only can have homogeneous type of data elements in array,
But Collections can have any kind of data element. They can be homogeneous or heterogeneous.
when we should use arrays and when collection?
Array are recommended to use if we know the size in advance..
because Performance-wise Arrays are better to use than Collections.
From Memory point of view, we should not use 'Arrays'..
Suppose we take an array of 100 size. And if we only have 10 elements in it, so,
here the rest of the memory blocks get wasted. Thats why arrays are not recommended to use in memory point of view.
On the other hand, Collections are grow-able or resizable in nature, and it uses memory as per required only.
because Performance-wise Arrays are better to use than Collections.
From Memory point of view, we should not use 'Arrays'..
Suppose we take an array of 100 size. And if we only have 10 elements in it, so,
here the rest of the memory blocks get wasted. Thats why arrays are not recommended to use in memory point of view.
On the other hand, Collections are grow-able or resizable in nature, and it uses memory as per required only.
What is the difference between Array and ArrayList?
The very first difference between Array and ArrayList is,
Array can contain primitive or Objects whereas ArrayList can only contain Objects.
next is
Arrays are fixed in size whereas ArrayList size is dynamic.
And, ArrayList accepts duplicate objects unlike arrays..
Array does not provide a lot of features like ArrayList, such as add(), addAll(), removeAll() etc..
So ArrayList is the obvious choice when we work on list.. But if the size of list is fixed and if we are using the list of primitives then we should use Arrays.. Although collection use auto boxing to reduce the coding effort but still it makes them slow.. so would use array with primitives.
And if are working on multidimensional situation, then also using array [][] is way more easy than List
Array can contain primitive or Objects whereas ArrayList can only contain Objects.
next is
Arrays are fixed in size whereas ArrayList size is dynamic.
And, ArrayList accepts duplicate objects unlike arrays..
Array does not provide a lot of features like ArrayList, such as add(), addAll(), removeAll() etc..
So ArrayList is the obvious choice when we work on list.. But if the size of list is fixed and if we are using the list of primitives then we should use Arrays.. Although collection use auto boxing to reduce the coding effort but still it makes them slow.. so would use array with primitives.
And if are working on multidimensional situation, then also using array [][] is way more easy than List
- >..
How LinkedList is different from ArrayList?
ArrayList and LinkedList both implement the List interface but there are some differences between them..
ArrayList is an indexed based data structure.. The underlying data structure for ArrayList is a resizable or growable Array.
And LinkedList is a data structure which stores data and its address as list of nodes.. here, every node is linked to its previous and next node using pointers.. That is, the address part of the element points to the next element of the linked list.so see the address that the first node is having.. is actually the address of the next node.
This is how we create ArrayList, and linkedLists
ArrayList al = new ArrayList();
//and to add elements.. these are the methods.. (explain the suggestions)
al.add("John");
al.add("Rohn");
LinkedList lList = new LinkedList();
lList.add("");
Since ArrayList is index based data structure, it provides random access to its elements with the performance of O(1).. that is, we can access any element by the index.. Linked list also allows to get any element by index, but internally it traverse the list from start to read at the index node, then return the element. So the performance is O(n) which is slower than of arrayList. Therefore the ArrayList is recommended to use retrieving data..
Next thing is, Insertion and removal of any element is faster in LinkedList compared to ArrayList. Because one insertion or deletion in middle requires n number of shifts.. So there is no concept of resizing array when element is added in middle..
and in linkedList it requires only one change in the address pointer of the particular node to add or remove any element.
Next is,
LinkedList consumes more memory than ArrayList because every node in LinkedList stores reference of previous and next elements..'
ArrayList is an indexed based data structure.. The underlying data structure for ArrayList is a resizable or growable Array.
And LinkedList is a data structure which stores data and its address as list of nodes.. here, every node is linked to its previous and next node using pointers.. That is, the address part of the element points to the next element of the linked list.so see the address that the first node is having.. is actually the address of the next node.
This is how we create ArrayList, and linkedLists
ArrayList
//and to add elements.. these are the methods.. (explain the suggestions)
al.add("John");
al.add("Rohn");
LinkedList
lList.add("");
Since ArrayList is index based data structure, it provides random access to its elements with the performance of O(1).. that is, we can access any element by the index.. Linked list also allows to get any element by index, but internally it traverse the list from start to read at the index node, then return the element. So the performance is O(n) which is slower than of arrayList. Therefore the ArrayList is recommended to use retrieving data..
Next thing is, Insertion and removal of any element is faster in LinkedList compared to ArrayList. Because one insertion or deletion in middle requires n number of shifts.. So there is no concept of resizing array when element is added in middle..
and in linkedList it requires only one change in the address pointer of the particular node to add or remove any element.
Next is,
LinkedList consumes more memory than ArrayList because every node in LinkedList stores reference of previous and next elements..'
What are the different ways to iterate over a list?
To access or to get elements from collection there are many ways present in java.
1. Loops (classic for loop)
2. Cursors
In java these are the cursors available for collections:
1. Iterator
2. ListIterator
3. Enumeration
1. Loops (classic for loop)
2. Cursors
In java these are the cursors available for collections:
1. Iterator
2. ListIterator
3. Enumeration
What is the difference between Iterator and ListIterator and Enumeration?
(Answer would go here)
What are the legacy implementations?
Vector is a legacy class.
Legacy class: The classes that were already there before introducing collection framework.
Sub Class of Vector -> Stack, is also a legacy class.
Vector is implemented on a growable or a resizable array.
-> It is an ordered collection
-> allows duplicates.
Vector v = new Vector();
Stack implements the stack data structure. It is based on the principle of Last In First Out.
👉🏻 element which is inserted in last, will be the first one to come out.
Stack s = new Stack();
-> to insert any object : push()
-> to remove the top element : pop()
Legacy class: The classes that were already there before introducing collection framework.
Sub Class of Vector -> Stack, is also a legacy class.
Vector is implemented on a growable or a resizable array.
-> It is an ordered collection
-> allows duplicates.
Vector v = new Vector();
Stack implements the stack data structure. It is based on the principle of Last In First Out.
👉🏻 element which is inserted in last, will be the first one to come out.
Stack s = new Stack();
-> to insert any object : push()
-> to remove the top element : pop()