Java Collections
Java Collections
Java Collections
-------
Collections
-----------------------------------------------------------------------------------
-------
interface Iterable
interface Collection extends Iterable
interfaces List, Queue, Set extend Collection
interface SortedSet extends Set
interface Map
interface SortedMap extends Map
classes ArrayList, Vector, Stack, LinkedList implement List
classes HashSet, LinkedHashSet implement Set
class TreeSet implements SortedSet
classes HashMap, WeakHashMap, LinkedHashMap implement Map
class TreeMap implements SortedMap
utility classes Collections and Arrays
5. What is an Iterator?
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.
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 buckets defined by their hashcodes. Each
bucket is a singly linked list. Provides constant time performance for basic
operations. LinkedHashSet
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.
8. TreeSet vs LinkedHashSet
It's totally possible. Actually a totally valid hashCode() function could look
like this
15. Can we put two elements with equal hash code to one hash map?
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
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.
17. What is the significance of ListIterator? What is the difference b/w Iterator
and ListIterator?
See above
22. Which implementation of the List interface provides for the fastest insertion
of a new element into the middle of the list?
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.
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.
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