Collection Framework Interview In Java?
Q1) What is Iterable Interface In Java?
Q2) What is the difference between Collections and Collection?
Q3) Which is faster to access ArrayList or LinkedList?
Q4) Which is faster ArrayList or Vector?
Q5) How to create Synchronized ArrayList?
Q6) What is hashCode() in Java, how to get the hashcode of Object?
Q7) Difference between Iterator and ListIterator?
Q8) What is Comparable and Comparator Interface in Java?
Q9) What is the difference between Hashtable and HashMap?
Q10) Difference between LIST and Set?
Q11) What is the difference between Set and MAP?
Q12) What is Queue Interface In java?
Q13) What is Priority Queue and Abstract Queue in Java?
Q1) What is Iterable Interface In Java?
ردحذفAns:- It is the base interface of all Collection Interfaces of Java.
Q2) What is the difference between Collections and Collection?
ردحذفAns:-
1) The Collection is an interface whereas Collections is a class.
2) The Collection interface provides the standard functionality of data structure to
List, Set, and Queue. However, Collections class is to sort and synchronize the
collection elements.
3) The Collection interface provides the methods that can be used for data structure w
Whereas Collections class provides the static methods which can be used for various
operation on a collection.
Q3) Which is faster to access ArrayList or LinkedList?
ردحذفAns:-
ArrayList is Faster than LinkedList if
Q4) Which is faster ArrayList or Vector?
ردحذفAns:-
ArrayList is Faster Since it is non-Synchronized While Vector Operation Give Slower Performance Since They are Synchronized (Thread-safe)..
Q5) How to create Synchronized ArrayList?
ردحذفAns:_
We can Use Collection.SynchronizedList(List) Method to Synchronize collection in java
The SynchronizedList(List) method is Used to return a Synchronizes (Thread-safe) List backed By the Specified List.
import java.util.*;
public class SyncronizeArrayList {
public static void main(String args[]) {
List fruitList = new ArrayList();
fruitList.add("Mango");
fruitList.add("Banana");
fruitList.add("Apple");
fruitList.add("Strawberry");
fruitList.add("Pineapple");
furitList = Collections.synchronizedList(fruitList);
synchronized (fruitList) {
Iterator itr = fruitList.iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
}
}
}
Q6) Difference between Iterator and ListIterator?
ردحذفAns:-
Iterator:-
1) The Iterator traverses the elements in the forward direction only.
2)The Iterator can be used in List, Set, and Queue.
3)The Iterator can only perform remove operation while traversing the collection.
ListIterator:-
!)ListIterator traverses the elements in backward and forward directions
both.
2) ListIterator can be used in List only.
3) ListIterator can perform ?add,? ?remove,? and ?set? operation while
traversing the collection.
Q:6) What is Comparable and Comparator Interface in Java?
ردحذفAns;-
Comparator:-
this interface is used to compare two different objects .by default TreeSet
using Comparator interface compare() to compare internal data.
The comparator provides flexibility to collection objects because we can
compare the data using different attributes to separate class for comparison.
Comparable interface:-
it provides a compareTo method which should be implemented into the actual class, we
can pass only one parameter for comparison.
Q9) What is the difference between Hashtable and HashMap?
ردحذفAns;-
HashMap:-
1)HashMap is not synchronized.
2)HashMap can contain one null key and multiple null values
3)HashMap is not ?thread-safe,? so it is useful for non-threaded
applications.
4) HashMap inherits the AbstractMap class
Hashtable:-
1)Hashtable is synchronized.
2)Hashtable cannot contain any null key or null value.
3)Hashtable is thread-safe, and it can be shared between various
threads.
4) Hashtable inherits the Dictionary class.
Q10) Difference between LIST and Set?
ردحذفAns:-
List,Set:-
1)The List can contain duplicate elements whereas Set includes unique
items.
2)The List is an ordered collection which maintains the insertion order
whereas Set is an unordered collection which does not preserve the insertion
order.
3)The List interface contains a single legacy class which is Vector class
whereas Set interface does not have any legacy class.
4)The List interface can allow n number of null values whereas Set interface
only allows a single null value.
Q11) What is the difference between Set and MAP?
ردحذفAns;-
Set and MAP:-
1)Set contains values only whereas Map contains key and values both.
2}Set contains unique values whereas Map can contain unique Keys with
duplicate values.
3)Set holds a single number of null value whereas Map can include a single
null key with n number of null values.
Q12) What is Queue Interface In java?
ردحذفAns:-
Queue interface: Queue interface defines queue data structure,
which stores the elements in the form FIFO (first in first out).
Q13) What is Priority Queue and Abstract Queue in Java?
ردحذفPriorityQueue class:-
The PriorityQueue class provides the facility of using
queue. But it does not orders the elements in FIFO manner.
It inherits AbstractQueue class.
Abstract Queue :-
The AbstractQueue class in Java is a part of the Java Collection
Framework and implements the Collection interface and the
AbstractCollection class. It provides skeletal implementations of some
Queue operations. The implementations in this class are appropriate
when the base implementation does not allow null elements.