16 Questions Related To Collection
16 Questions Related To Collection
1. What is Collection and what is the difference between Collection and Collections?
Ans:
Collection is a group of similar types of objects.it is a predefined interface present
in java.util package.
Ans:
The following are the collections available in java
1. ArrayList.
2. LinkedList.
3. Stack.
4. Queue.
5. HashMap
6. HashSet.
7. Vector.
8. TreeMap.
Ans:
The similarities between ArrayList and LinkedList is both are “Dynamic “in nature.
Ans:
Dissimilarities between ArrayList and LinkedList is ArrayList is “Continuous”
where LinkedList is “Discontinuous”.
Ans:
The similarities between Array and ArrayList is both are continuous /sequential in
nature.
Ans:
Because a lot of shifting needs to be occurred.
Ans:
Among Array And ArrayList Array is faster because ArrayList is dynamic in nature
means its size can be expanded sequentially.
Ans:
ArrayList always “slower” as comparision to Array and LinkedList.
Ans:
Because no shifting needs to be occurred.
Ans:
There are 2 ways to iterate the
i) by Iterator interface.
ii) by for-each loop.
Ans:
Boolean add() used to insert an element in the collection Where Boolean addAll()
used to insert the specified collection element in the invoking collection.
Ans:
Boolean remove() used to delete an element from the collection. Where Boolean
removeAll() used to delete all the elements of specified collection from the
invoking collection.
Ans:
It is use to delete all the elements of invoking collection except the specified
collection.
Ans:
It returns the total no of elements in the collections.
Ans:
int size() retuens the total no of elements in the collection. Where void clear()
removes the total no. of element from the collection.
Ans:
The difference between ArrayList and Vector is
i) ArrayList is not synchronized so it’s not suitable for use in multi-thread
environment. WHERE Vector is synchronized so suitable for use in multi-
thread environment.
Ans:
1. ArrayList and Vector are index based and backed up by an array internally.
2. ArrayList andVector maintains the insertion order of element .
3. ArrayList and Vector allows null and duplicate.
Ans:
1. Major difference between TreeSet and TreeMap is that TreeSet implements
set interface while TreeMap implements Mapinterface in Java.
2. Second difference between TreeMap and TreeSet is the way they store
objects. TreeSet stores only one object while TreeMap uses two objects called
key and Value. Objects in TreeSet are sorted while keys in TreeMap remain in
sorted Order.
3. Third difference between TreeSet and TreeMap is that, former
implements NavigableSet while later implementsNavigableMap in Java.
4. Fourth difference is that duplicate objects are not allowed in TreeSet but
duplicates values are allowed in TreeMap.
Ans:
Ans:
Queue is a linear list of elements. With a queue, deletions can take place only one
end called front, and insertion can take place only the other hand called
rear. deQueue is a linear list of elements.
With a dequeue, added and removed at either end but not in the middle.
Ans:
Hash map is not synchronized. it contain one null key and multiple null values.
Where Hash table is synchronized .it cannot contain any null key or null value.
Ans:
1. We need not to learn multiple ad hoc collection APIs.
2. It provides a standard interface for collections that fosters software reuse and
also provides algorithms to manipulate them.
3. Reduces the effort required to design and implement APIs by eliminating the
need to produce ad hoc collections APIs.
4. It provides useful data structures and algorithms that reduces programming
effort due to which we need not to write them ourselves.
5. It provides high-performance implementations of useful data structures and
algorithms that increases the performance.
6. Helps in establishing a common language to pass collections back and forth that
provides interoperability between unrelated APIs.
7. Collection is resizable and can grow.
Ans:
There are three ways to traverse collections:
1. using aggregate operations
2. with the for-each construct and
3. by using Iterator.
Ans:
Iterator Interface provides the facility of iterating the element in forward direction.
Ans:
Deletion in linked list is fast because it involves only updating the next pointer in the
node before the deleted node and updating the previous pointer in the node after the
deleted node.
24. Which two method you need to implement for key Object in HashMap?
Ans:
The two method are 1.Map interface 2.AbstractMap class.
26. What will happen if we put a key object in a HashMap which is already there?
Ans:
HashMap never contains any duplicate key so it will replace with the old key.
Ans:
It is use to traverse the element in background and forward direction.
Ans:
List interface is the sub interface of collection, which contains methods to insert and
delete elements in index basis.
Ans:
Creating a vector is nothing but creating an object of java.util.Vectorclass.Vectorecah
time double its size,whileArrayList grows 50% of its size each time.
Ans:
The priorityQueue class provides the facility of using queue.it does not orders the
elements in FIFO order.
Ans:
Deque is a linear collection that supports element insertion and removal at both
ends. The java.util.Deque interface is a subtype of the java.util.Queue interface
.Deque is short for “Double ended Queue”.
Ans:
ArrayDeque implements Deque interface.it available from jdk1.6.It is not thread
safe.It allows unlimited insertion of elements.
Ans:
1. HashMap implements the Map interface WHERE TreeMap implements
NavigableMap interface
2. HashMap may have one null key WHERE TreeMap cannot have null key.
3. HashMap maintains no order WHERE TreeMap maintains ascending order.
Ans:
Read only List means a List where you cannot perform modification operations like add, remove or set.
You can only read from the List by using get method or by using Iterator of List.
Ans:
List can contain duplicate elements whereas set contains unique elements only.
Ans:
Because a lot of shifting has to be done internally.
Ans:
Using a parameterized type such as LinkedList<String> , instead of LinkedList ,
enables the compiler to perform more type checks and requires fewer dynamic casts.
This way errors are detected earlier, in the sense that they are reported at compile-
time by means of a compiler error message rather than at runtime by means of an
exception.
Ans:
Iterator is a interface which iterating the element in forward direction.