Java Interview Questions - Collection
1. What is Collection and what is the difference between Collection and Collections?
Collection is a group of similar types of objects.it is a predefined interface present in java.util package.
2. What are the different Collection in JAVA?
The following are the collections available in java
- ArrayList.
- LinkedList.
- Stack.
- Queue.
- HashMap
- HashSet.
- Vector.
- TreeMap.
3. What is the similarity between ArrayList and LinkedList?
The similarities between ArrayList and LinkedList is both are “Dynamic “in nature.
4. What is the difference between ArrayList and LinkedList?
Dissimilarities between ArrayList and LinkedList is ArrayList is “Continuous” where LinkedList is “Discontinuous”.
5. What is the dissimilarities between Array and ArrayList?
Dissimilarities between Array and ArrayList is Array is “Static” in nature where ArrayList is “Dynamic” in nature.
6. What is the similarities between Array and ArrayList?
The similarities between Array and ArrayList is both are continuous /sequential in nature.
7. Why manipulation slows in case of ArrayList class?
Because a lot of shifting needs to be occurred.
8. Who is faster between Array and ArrayList& Why?
Among Array And ArrayList Array is faster because ArrayList is dynamic in nature means its size can be expanded sequentially.
9. Who is slower among Array, ArrayList&LinkedList?
ArrayList always “slower” as comparision to Array and LinkedList.
10. Why manipulation fast in case of LinkedList class?
Because no shifting needs to be occurred.
11. What are the ways to iterate the elements of collection?
There are 2 ways to iterate the
- by Iterator interface.
- by for-each loop.
12. State difference between Boolean add() & Boolean addAll()?
Boolean add() used to insert an element in the collection Where Boolean addAll() used to insert the specified collection element in the invoking collection.
13. State difference between Boolean remove() & Boolean removeAll()?
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.
14. State the use of Boolean retainAll()?
It is use to delete all the elements of invoking collection except the specified collection.
15. State the use of intsize()?
It returns the total no of elements in the collections.
16. State the difference between intsize() & void clear()?
int size() retuens the total no of elements in the collection. Where void clear() removes the total no. of element from the collection.
14. What is the difference between ArrayList and Vector?
The difference between ArrayList and Vector is:
- 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.
- ArrayList is not synchronized but faster thenvector WHERE Vector is synchronized and thread-safe it pays price of synchronization which make it little slow.
15. What is the similarity between ArrayList and Vector?
- ArrayListandVectorareindexbasedandbackedupbyanarrayinternally.
- ArrayList andVector maintains the insertion order of element.
- ArrayListandVectorallowsnullandduplicate.
16. What is the difference between TreeSet and TreeMap?
- Major difference between TreeSet and TreeMap is that TreeSet implements set interface while TreeMap implements Mapinterface in Java.
- 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.
- Third difference between TreeSet and TreeMap is that, former implements NavigableSet while later implementsNavigableMap in Java.
- Fourth difference is that duplicate objects are not allowed in TreeSet but duplicates values are allowed in TreeMap.
17. What is the difference between Queue and Deque?
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.
18. What is the difference between HashMap and HashTable?
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.
19. What are advantages of Collection?
- We need not to learn multiple adhoc collection APIs.
- It provides a standard interface for collections that fosters software reuse and also provides algorithms to manipulate them.
- Reduces the effort required to design and implement APIs by eliminating the need to produce ad hoc collections APIs.
- It provides useful data structures and algorithms that reduces programming effort due to which we need not to write them ourselves.
- It provides high-performance implementations of useful data structures and algorithms that increases the performance.
- Helps in establishing a common language to pass collections back and forth that provides interoperability between unrelated APIs.
- Collection is resizable and can grow.
20.What are the ways traverse Collection objects?
There are three ways to traverse collections:
- using aggregate operations
- with the for-each construct and
- byusingIterator.
21. What is Iterator Interface?
Iterator Interface provides the facility of iterating the element in forward direction.
22. Why deletion in LinkedList is fast than ArrayList?
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.
23. Which two method you need to implement for key Object in HashMap?
The two method are
- Map interface
- AbstractMap class.
24. What will happen if we put a key object in a HashMap which is already there?
HashMap never contains any duplicate key so it will replace with the old key.
25. What is use of ListIterator interface?
It is use to traverse the element in background and forward direction.
26. What is List interface?
List interface is the sub interface of collection, which contains methods to insert and delete elements in index basis.
27. What is vector class?
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.
28. What is priorityQueue class?
The priorityQueue class provides the facility of using queue.it does not orders the elements in FIFO order.
29. What is Deque class?
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”.
30. What is ArrayDeque?
ArrayDeque implements Deque interface.it available from jdk1.6.It is not thread safe.It allows unlimited insertion of elements.
31. Difference between HashMap and TreeMap?
- HashMap implements the Map interface WHERE TreeMap implements NavigableMap interface
- HashMap may have one null key WHERE TreeMap cannot have null key.
- HashMap maintains no order WHERE TreeMap maintains ascending order.
32. Common algorithms list implemented Collections Framework?
Java Collections Framework also provided polymorphic algorithms to operate on the collections. These are all reusable piece of functionality provided to make job easier when working with collections. All operation as usual defied static to call directly from Collections class. Below are algorithms list:
- Sorting
- Shuffling
- Routine Data Manipulation
- Searching
- Composition
- Finding Extreme Values
33. How you create read only collection?
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.
34. What is the difference between List and set?
List can contain duplicate elements whereas set contains unique elements only.
35. Why insertion and deletion in ArrayList slow compared to LinkedList?
Because a lot of shifting has to be done internally.
36. Why Generics are used in Java?
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.
37. Is Iterator a Class or Interface? What is its use?
Iterator is a interface which iterating the element in forward direction.