|
| 1 | +------------------------------------------------------------------------------------------ |
| 2 | +Collections |
| 3 | +------------------------------------------------------------------------------------------ |
| 4 | + |
| 5 | +1. Draw Collections Framework Class Diagram |
| 6 | + |
| 7 | + interface Iterable |
| 8 | + interface Collection extends Iterable |
| 9 | + interfaces List, Queue, Set extend Collection |
| 10 | + interface SortedSet extends Set |
| 11 | + interface Map |
| 12 | + interface SortedMap extends Map |
| 13 | + classes ArrayList, Vector, Stack, LinkedList implement List |
| 14 | + classes HashSet, LinkedHashSet implement Set |
| 15 | + class TreeSet implements SortedSet |
| 16 | + classes HashMap, WeakHashMap, LinkedHashMap implement Map |
| 17 | + class TreeMap implements SortedMap |
| 18 | + utility classes Collections and Arrays |
| 19 | + |
| 20 | +2. What is HashMap and Map? |
| 21 | + |
| 22 | + Map is an interface. Contains methods to manipulate Key-Value based collections. The main methods of Map interface are put(K,V), get(K), Collection<V> values(), Set<K> keySet(), containsKey(), containsValue() |
| 23 | + HashMap is one of implementations of the Map interface based on hashcodes of objects used as keys. |
| 24 | + |
| 25 | +3. Difference between HashMap and HashTable? Can we make hashmap synchronized? |
| 26 | + |
| 27 | + Both implement Map interface. HashTable is synchronized. It is recommended to use HashMap wherever possible. HashTable doesn't allow null keys and values. HashMap allows one null key and any number of null values. |
| 28 | + We can make it synchronized |
| 29 | + Map m = Collections.synchronizedMap(new HashMap()); |
| 30 | + |
| 31 | +4. Difference between Vector and ArrayList? |
| 32 | + |
| 33 | + Both implement List interface. ArrayList is not synchronized. |
| 34 | + |
| 35 | +5. What is an Iterator? |
| 36 | + |
| 37 | + It is an interface that contains three methods: next(), boolean hasNext(), void remove() |
| 38 | + It allows to iterate over the collection |
| 39 | + If the class implements iterator then it can be used in foreach loop |
| 40 | + |
| 41 | +6. List vs Set vs Map. Purposes and definitions. |
| 42 | + |
| 43 | + All three are interfaces. |
| 44 | + |
| 45 | + List -- storing values in specified order. Provides methods to get the element by its position get(i), finding element, ListIterator. Known implementations: ArrayList, Vector, LinkedList. List should be used when the order in which the elements are stored matters. |
| 46 | + |
| 47 | + Set -- storing only different objects and at most one null element. Known implementations: TreeSet (iterate over the elements in order defined by Comparator, or if the elements implement comparable; provides log(n) performance for basic operations), HashSet -- stores values in buckest defined by their hashcodes. Each bucket is a singly linked list. Provides constant time performance for basic operations. LinkedHashSet |
| 48 | + |
| 49 | + Map -- for storing key-value pairs. Map cannot contain duplicate keys. Provides three collection views: set of keys, collection of values, set of key-value mappings. Know implementations HashMap, EnumMap, TreeMap, LinkedHashMap, WeakHashMap. |
| 50 | + |
| 51 | +7. Pros and cons of ArrayList and LinkedList |
| 52 | + |
| 53 | + ArrayList -- fast random access. |
| 54 | + LinkedList -- slow random access. Implements Queue interface. Fast deletion of the element. |
| 55 | + If lots of random reads is anticipated use ArrayList. |
| 56 | + If lots of iterations over the whole list and lots of add/delete -- use LinkedList. |
| 57 | + |
| 58 | +8. TreeSet vs LinkedHashSet |
| 59 | + |
| 60 | + LinkedHashSet is backed by LinkedHashMap. LinkedHashMap is backed by doubly linked list to enforce ordering on the elements contained in the Map. |
| 61 | + If the ordering of the elements in the Set matters to you but you don't want to use a comparator you may use LinkedHashSet since it will enforce ordering in which the elements were added to the set. Otherwise use TreeSet |
| 62 | + |
| 63 | +9. What are relationships between equals and hash codes? |
| 64 | + |
| 65 | + See above. |
| 66 | + |
| 67 | +10. What are the advantages of ArrayList over arrays ? |
| 68 | + |
| 69 | + 1. ArrayList comes with a number of utility methods (e.g. contains, remove, addAll) |
| 70 | + 2. Type safety |
| 71 | + 3. Dynamic sizing |
| 72 | + On the other hand arrays are a little bit faster and take less memory (packing) |
| 73 | + |
| 74 | +11. Principle of storing data in a hashtable |
| 75 | + |
| 76 | + HashSet. add(element) -> element.hashCode() -> mod bucketsNumber -> store |
| 77 | + HashMap. add(key, value) -> key.hashCode() -> mod bucketsNumber -> store(value) |
| 78 | + |
| 79 | +12. Differences between Hashtable, ConcurrentHashMap and Collections.synchronizedMap() |
| 80 | + |
| 81 | + ConcurrentHashMap allows concurrent modification of the Map from several threads without the need to block them. Collections.synchronizedMap(map) creates a blocking Map which will degrade performance, albeit ensure consistency (if used properly). |
| 82 | + Use the second option if you need to ensure data consistency, and each thread needs to have an up-to-date view of the map. Use the first if performance is critical, and each thread only inserts data to the map, with reads happening less frequently. |
| 83 | + |
| 84 | +13. How are hash codes computed? |
| 85 | + |
| 86 | + if hashCode() method is defined then it is called to calculate the hashcode |
| 87 | + if its not defined the default implementation in Object class does the following: |
| 88 | + |
| 89 | + public int hashCode() { |
| 90 | + return VMMemoryManager.getIdentityHashCode(this); |
| 91 | + } |
| 92 | + |
| 93 | +14. Is it possible that hash code is not unique? |
| 94 | + |
| 95 | + It's totally possible. Actually a totally valid hashCode() function could look like this |
| 96 | + |
| 97 | + int hashCode(){ return 57; } |
| 98 | + |
| 99 | +15. Can we put two elements with equal hash code to one hash map? |
| 100 | + |
| 101 | + Yes we can. The hashcode of objects doesn't matter. Only the hashcode of keys. But even if you want to put keys with the same hashcode it will be ok since it just means that key-value pairs will be put into the same bucket |
| 102 | + |
| 103 | +16. Iterator and modification of a List. ConcurentModificationException. |
| 104 | + |
| 105 | + The iterators returned by this class's iterator method are fail-fast: if the set is modified at any time after the iterator is created, in any way except through the iterator's own remove method, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future. |
| 106 | + |
| 107 | + Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs. |
| 108 | + |
| 109 | +17. What is the significance of ListIterator? What is the difference b/w Iterator and ListIterator? |
| 110 | + |
| 111 | + ListIterator allows to perform iteration both ways (first-->last and last-->first) |
| 112 | + From JavaDoc: ListIterator is an iterator for lists that allows the programmer to traverse the list in either direction, modify the list during iteration, and obtain the iterator's current position in the list |
| 113 | + |
| 114 | +18. What is the Collections API? |
| 115 | + |
| 116 | + See above |
| 117 | + |
| 118 | +19. How can we access elements of a collection? |
| 119 | + |
| 120 | + Depends on the collection type. In general: using an iterator, getting by index, getting by key, using iterator on a view |
| 121 | + |
| 122 | +20. What’s the difference between a queue and a stack? |
| 123 | + |
| 124 | + In Java Queue is an interface and has a number of implementations. Stack is a class derieved from Vector class. |
| 125 | + The Queue interface contains methods to enqueue and dequeue elements: offer(), poll(). Also contains methods to see the first element in the queue: peek(). The queue ordering is not neccessairly FIFO. It can be based on the comparator (PriorityQueue) |
| 126 | + The Stack contains methods to add and remove elements to/from a stack. If used correctly enforces LIFO policy. Contains methods push(), pop(), peek(). The problem with Java stack implementation is that it contains all the methods from Vector class which can lead to violation of LIFO policy. |
| 127 | + |
| 128 | +21. What is the Properties class? |
| 129 | + |
| 130 | + Property class is designed to hold properties (i.e. pairs <String, String> -- property name (key) and property value). The two main methods of Properties class is getProperty(String name) and setProperty(String name, String value). Properties class extends HashTable |
| 131 | + |
| 132 | +22. Which implementation of the List interface provides for the fastest insertion of a new element into the middle of the list? |
| 133 | + |
| 134 | + LinkedList. It only requires 4 assignments. |
| 135 | + Before: ... <--> A <--> B <--> ... |
| 136 | + After: ... <--> A <--> C <--> B <--> ... . A changes one reference, B changes one reference, C assigns two references. |
| 137 | + |
| 138 | +23. How can we use hashset in collection interface? |
| 139 | + |
| 140 | + I'm not sure I understand this question fully. HashSet implements Set interface which in turn extends the Collection interface. Thus HashSet is a valid class to be used everywhere where Collection interface is required. |
| 141 | + |
| 142 | +25. Can you limit the initial capacity of vector in java? |
| 143 | + |
| 144 | + I'm not sure if limit is correct word to be used in this question. One can just set the initial capacity of the Vector. Well you could say that you are putting lower limit on the size of the Vector. |
| 145 | + |
| 146 | +26. What method should the key class of Hashmap override? |
| 147 | + |
| 148 | + equals() and hashCode(). |
| 149 | + |
| 150 | +27. What is the difference between Enumeration and Iterator? |
| 151 | + |
| 152 | + Enumeration is an interface similar to Iterator. But it doesn't have remove() method. Enumeration acts as Read-only interface, because it has the methods only to traverse and fetch the objects, where as using Iterator we can manipulate the objects also like adding and removing the objects |
| 153 | + |
| 154 | +28. Collections class and Arrays class |
| 155 | + |
| 156 | + The Collections class contains a number of factory methods for creating unmodifiable views or synchronized wrappers of collections, as well as utility methods such as sort, shuffle, reverse |
| 157 | + |
| 158 | + The Arrays class contains a factory method that take an array as input and return a List based on this array (Arrays.asList()). Also contains methods to sort, search, fill and print arrays |
0 commit comments