Thursday, June 11, 2015

A walk through of Collections (Java)

Collections Notes:-

1. Difference b/w array and Collection :-
a. Array's no of element is limited, where as u can add any no of elements in a collection.
b. Array can store primitive and non-primitive variables, but Collection can store only non-primitive variables(object reference variables).


2. Attributes:- Performance, Ordered/Sorted, Uniqueness of the items, Synchronized.

3. Examples of:
a. List
a.1. ArrayList(Duplicate Allow) Ordered by index Not Sorted
a.2. LinkedList(Duplicate Allow) Ordered by index Not Sorted
a.3. Vector(Duplicate Allow) Ordered by index Not Sorted SYNCHRONIZED.vector
b. Set
b.1. HashSet Not Ordered Not Sorted
b.2. TreeSet Sorted Either by natural rules or by comparison
b.3. LinkedHashSet Ordered by insertion Not Sorted
c. Map
c.1. HashTable Not Ordered Not Sorted SYNCHRONIZED.hashtable
c.2. HashMap Not Ordered Not Sorted
c.3. LinkedHashMap Ordered
c.4. TreeMap Sorted Either by natural rules or by comparison

4. Ordering (Difference b/w ordering and sorting: Ordering means in a specific order by index, and sorting means in particular order by value.) Sorting refers to only values.
a. Natural Ordering:-     Collections.sort(list);

5.  There are several differences between HashMap and Hashtable in Java:
a. Hashtable is synchronized, whereas HashMap is not. This makes HashMap better
for non-threaded applications,
as unsynchronized Objects typically perform better than synchronized ones.
b. Hashtable does not allow null keys or values. HashMap allows one null key and any number of null values.
c. One of HashMap's subclasses is LinkedHashMap, so in the event that you'd want predictable iteration order
(which is insertion order by default), you could easily swap out the HashMap for a LinkedHashMap.
This wouldn't be as easy if you were using Hashtable.

Since synchronization is not an issue for you, I'd recommend HashMap. If synchronization becomes an issue, you may also look at ConcurrentHashMap.

6. All three classes implement the Map interface and offer mostly the same functionality. The most important difference is the order in which iteration through the entries will happen:
    a. HashMap makes absolutely no guarantees about the iteration order. It can (and will) even change completely when new elements are added.
    b. TreeMap will iterate according to the "natural ordering" of the keys according to their compareTo() method (or an externally supplied Comparator).
Additionally, it implements the SortedMap interface, which contains methods that depend on this sort order.
    c. LinkedHashMap will iterate in the order in which the entries were put into the map.
"Hashtable" is the generic name for hash-based maps. In the context of the Java API, Hashtable is an obsolete class from the days of Java 1.1 before the
collections framework existed. It should not be used anymore, because its API is cluttered with obsolete methods that duplicate functionality,
and its methods are synchronized (which can decrease performance and is generally useless).

7. TreeMap is an example of a SortedMap, which means that the order of the keys can be sorted, and when iterating over the keys,
you can expect that they will be in order.
a. HashMap on the other hand, makes no such guarantee. Therefore, when iterating over the keys of a HashMap, you can't be sure what order they will be in.
b. HashMap will be more efficient in general, so use it whenever you don't care about the order of the keys.


Happy Collections :)