Generics and Collections
1. If you do not override a class’s equals() method, you won’t be able to
use those objects as a key in HashTable and you probably won’t get
accurate Sets.
2. The equals() method in class Object uses only the == operator for
comparisons, so unless you override equals(), two objects are
considered equal only if the two references refer to the same object.
3. toString(), equals() and hashCode() methods must be declared as
public.
4. HashCodes are typically used to increase the performance of large
collections of data.
5. Collections such as HashMap and HashSet use the hashcode value of
an object to determine how the object should be stored in the
collection, and to help locate the object in collection.
6. If two objects are equal, their hashcodes must be equal, but the
reverse is not true.
7. Transient variables can really mess with your equals() and hashCode()
implementations.(After de-serialization transient variable value will
become as default value.). Keep variables non-transient or if ther must
be marked transient. Don’t use them to determine hashcodes or
equality.
8. Core interfaces need to be known in collections are
I. Collection
II. List
III. Queue
IV. Set
V. Map
VI. SortedSet
VII. NavigableSet
VIII. Sorted Map
IX. NavigableMap
9. concrete implementation of classes need to know is :
Maps Sets Lists Queues Utilities
HashMap HashSet ArrayList PriorityQueue Collections
HashTable LinkedHashSe Vector Arrays
t
TreeMap TreeSet LinkedList
LinkedHashMa
p
10. There are three overloaded uses of word “collection” as follows
a) “collection” (lowercase c), which represents any of the
data structures in which objects are stored and iterated
over.
b) Collection (capital C), which is actually the
[Link] interface from which Set, List, and
Queue extend.
c) Collections (capital C and ends with s) is the
[Link] class that holds pile of ‘static’ utility
methods for use with collections.
11. None of ‘Map’ related classes and interfaces extend from Collection.
These are SortedMap, HashTable, HashMap, TreeMap, and
LinkedHashMap, and uses ‘Map’ interface.
12. The interface and class hierarchy is as shown below:
Collection (interface)
extends extends
extends
Set List Queue
(Interface) (Interface) (Interface)
HashSet SortedSet Vector
(interface) LinkedList PriorityQueue
ArrayList
LinkedHa
shSet NavigableS
et(interface
)
TreeSet
Map
Object
HashTable HashMap SortedMap
(Interface)
Arrays Collections LinkedHas
hMap Navigable
Map(Interf
extends ace)
implements
TreeMap
13. Collections come in four basic flavors.
A. List: List of things (implement List and duplicates
are allowed).
B. Sets: Unique things (implements Set and
duplicates are not allowed).
C. Maps: Things with a unique ID (Classes that
implement Map).
D. Queues: Things arranged by the order in which
they are to be processed.
14. An implementation class can be unsorted and unordered, ordered but
unsorted, or both ordered and sorted, but can never be sorted and
unordered (because sorting is a specific type of ordering).
List
15. List interface in ordered but not sorted.
16. ArrayList implements List interface, it cares about index. ArrayList is
fatser for iteration, but not for frequent insertion and deletion.
17. ArrayList implements new RandomAccess interface and it is not thread
safe (i.e methods can not be accessed asynchronously).
18. Vector is another only interface which implements RandomAccess
interface.
19. Vector is slow for iteration compared to ArrayList, but this is thread
safe.
20. LinkedList is ordered by position, like ArrayList, except that the
elements are doubly-linked to one another.
21. LinkedList may iterate more slowly than an ArrayList, but it’s good for
fast insertion and deletion.
22. After Java5, the lLinkedList class has been enhanced to implement the
[Link] interface. As such, it now supports the common queue
methods: peek(),poll(), and offer().
SET
23. Set does not allow duplicate values (equals () method checks for
equality of objects).
24. HashSet is unsorted and unordered interface, this does not allow
duplicates.
[Link] is based on hashCode(), and useful when you don’t care about
order when iterate through it.
[Link] is an ordered HashSet, Use this when you care about
order when iterate through it.
[Link] in LinkedHashSet will be the order in which they inserted.
28. When using HashSet or LinkedHashSet, the objects you add to them
must override hashCode().
[Link] hashCode() is not overridden the default [Link]() method
will allow multiple objects that you might consider “meaningfully
equal” to be added to “no duplicates allowed” set.
30. The TreeSet is one of sorted collections (the other being TreeMap) and
elements will be ordered in ascending order.
[Link] you can define the ordering criterion by using Comparable
or Comparator in TreeSet constructor.
[Link] implements NavigableSet.
MAP
33. Map cares about unique key identifier.
34. Maps rely on ‘equals()’ method to check equality of keys.
35. HashMap is an unordered, unsorted map.
36. Keys in Map will be stored based on key’s hashCode().
37. HashMap allows one null key and multiple null values in a collections.
38. Hashtable is the synchronized counterpart to the HashMap.
39. Hashtable does not allow using ‘null’ key and ‘null’ values.
40. LinkedHashMap maintains order as insertion order or last access
order. Although it will be somewhat slower than HashMap for adding
and removing elements, faster for iteration.
[Link] is sorted map and sorted by natural (ascending) order of the
elements.
42. TreeMap allows to define custom sort order (via a Comparable or
Comparator) when you construct a TreeMap.
[Link] implements NavigableMap.
Queue
44. LinkedList class has been enhanced to implement Queue interface.
45. LinkedList can be used to handle basic queues.
46. Priority Queue is to create a “priority-in, priority-out” as opposed to a
typical FIFO queue.
47. PriorityQueue’s elements are ordered and sorted.
48. PriorityQueue’s elements are ordered either by natural ordering or
according to a Comparator.
49. In general collections can hold objects but not primitives.
ArrayList
50. Array list
Can grow dynamically
Provides more powerful insertion and search than arrays
51. It’s important to remember that when you override ‘equals()’ you
MUST take an argument of type ‘Object’, but that when you override
compareTo() you should take an argument of the type you’re sorting.
52. To implement custom sorting in array list use the following procedure:
Write the class on which sorting has to be
done (For Ex: Employee class).
This class should implement ‘Comparable’
interface and ‘compareTo()’ method.
Override ‘toString()’ method also.
Single sort is allowed using Comparable
interface.
Using Comparator also you can customize
the sort.
Comparator class allows multiple sorts.
‘compare()’ method will be used to sort the
elements.
53.