Collection Notes
Collection Notes
5. What was the java‛s Collections framework equivalent in C++ called as?
Java‛s Collections framework equivalent in C++ is called as Standard Template
Library.
6. What was the java‛s Collections framework equivalent in smalltalk called as?
Java‛s collection framework equivalent in Smalltalk is called as Small Talk Collection
hierarchy.
Arrays ArrayList
Can store only primitive data. Can store only Objects
Arrays are fixed in size. They cannot ArrayList can grow or shrink in size
grow or shrink during program execution. during program execution
Can store only homogeneous data. Can store heterogeneous data.
Creation takes less time Creation would take more time because
auto boxing has to take place using
wrapper classes
Auto boxing is not required. Auto boxing is required.
ArrayList LinkedList
Internally makes use of dynamic array Internally makes uses of linked list
Suitable for performing rear end insertion Suitable for performing insertion at all
given position
Cannot utilize dispersed memory location Can utilize dispersed memory location
Iterator ListIterator
Can be used to access objects present in Can be used to access objects present in
all the seven Collections classes only list based classes
Iterator can iterate upon Collections ListIterator can iterate upon Collections
only in the forward direction both in forward and reverse direction
Iterator Enumeration
Iterator can traverse legacy and non- Enumeration can traverse only legacy
legacy elements elements
Iterator is fail-fast Enumeration is not fail-safe
Iterator is slower than Enumeration Enumeration is faster than Iterator
HashSet TreeSet
Internally makes use of Hashing Internally makes use of balanced binary
algorithm search tree using red-black algorithm
Objects would not get displayed in the It would display the objects in sorted
sorted order order because it would make use of
in-order traversal.
Search time complexity is O(1) Search time complexity is O(log2n)
26. Differentiate between Set and Map?
Set Map
Set stores only values. Map stores <key, value> pair.
Set is a member of Collections Map is not a member of Collection
hierarchy. hierarchy.
As the complexity of an object Efficiency would not be reduced even
increases, efficiency reduces. though complexity increases because
hashing would be applied on “key” and
not on value.
HashSet HaspMap
It is an implementation of Set interface It is an implementation of Map
Interface
Contains only values (objects) Contains object as <key,value> pair
Duplicates are not permitted Duplicate keys are not permitted.
However, duplicate values are permitted
add() method would be used to insert an put() method would be used for insertion
object operation
One parameter has to be passed while Two parameters have to be passed for
inserting an object. put() method.
Eg. add(Object arg0); Eg. put(Object arg0, Object arg1)
put(key , value)
Slow in operation Fast in operation
HashMap HashTable
It is not a synchronized class. It is a synchronized class.
It is not a legacy class. It is a legacy class.
It can contain an empty Key or Value It cannot contain an empty Key or value
pair. pair.
Suitable for multithreaded programming Not suitable for multithreaded
programming
Comparable Comparator
It expects implementation for It expects implementation for
compareTo() method compare() method
The first object is referred to as “this” The first object is not referred to as
“this”
It is present in java.lang package It is present in java.util package
Target class has to be modified Target class need not be modified
It is useful in such scenarios where the It is useful in all the scenarios
target class is modifiable
32. Are the List, Set and Map elements automatically synchronized in java‛s
Collection framework?
No. They are not automatically synchronized.
Fail-fast Fail-safe
It would result in termination of the Does not terminate the program by
program by generating an Exception if generating an Exception if structural
structural modification is attempted modification is attempted
No clone is used Clone is used
All the Collections based classes All the Collections based classes present
present in java.util package exhibit in java.util.concurrent package exhibit
fail-fast behavior fail-safe behavior
39. How do we avoid ConcurrentModificationException while iterating Collection?
To avoid ConcurrentModificationException we can make use of the classes present
in java.util.concurrent package.
40. What are the different Collection views provided by Map interface?
The different Collection views are:
1) Keyset
2) EntrySet
3) values.
47. What are the best practices related to java Collections framework?
i) Choose the best type of Collection depending upon objects to be stored.
ii) If the number of objects to be stored is known in advance then preferably we
can use Collections classes that allow us to specify the initial capacity.
iii) We can always use generics for type safety and to avoid ClassCastException at
run time.
iv) We can also use Collections utility class methods since it enhances code
re-usability, greater stability and maintainability.
48. Which are the classes that have implemented the List interface?
ArrayList and LinkedList classes.
49. Which are the classes that have implemented the Set interface?
HashSet and LinkedHashSet classes.
50. Which are the classes that have implemented the Queue interface?
ArrayDeque and PriorityQueue classes.
51. Which are the classes that have implemented the Map interface?
HashMap, TreeMap and LinkedHashMap classes.
52. Which methods do we have to override in order to use an object as a key in
HashMap?
We have to override hashCode() and equals() methods.
56. What is the difference between peek(), poll() and remove() methods of Queue
interface?
peek() and poll() would return null if the queue is empty whereas remove() would
throw NoSuchElementException.
70. By what factor does the capacity increase in case of a Vector if the existing
capacity is filled?
If the existing capacity is filled then the capacity doubles in existing size.
87. Which are the Collection class in which duplicates are permitted?
Duplicates are permitted in ArrayList, LinkedList, ArayDeque and PriorityQueue
classes.
88. Which are the Collection class in which duplicates are not permitted?
Duplicates are not permitted in TreeSet, HashSet and LinkedHashSet classes.
89. Which are the Collection class in which null are permitted?
Null is permitted in ArrayList, LinkedList, HashSet and LinkedHashSet classes.
90. Which are the Collection class in which null is not permitted?
Null is not permitted in ArrayDeque, PriorityQueue and TreeSet.
91. What are the two ways in which a LinkedList can be accessed in the reverse
order?
i) Using ListIterator
ii) Using DescendingIterator
121. How do you sort the data present in non-indexing data structure?
(Refer class notes).