Monday, 9 November 2015

Quick review of Collections

The Most Commonly used Core interfaces are:-

1.Collection 
2. Set 
3. SortedSet 
4. List 
5. Map 
6. SortedMap 
7. Queue

and the most frequently used core concrete implementation classes are

1. HashMap 
2. HashSet 
3. ArrayList 
4. PriorityQueue 
5. Collections 
6. Hashtable 
7. LinkedHashSet 
8. Vector 
9. Arrays 
10. TreeMap 
11. TreeSet 
12. LinkedList 
13. LinkedHashMap 


Remember none of the Map-related classes and interfaces extend from Collection.

So while SortedMap, Hashtable, HashMap, TreeMap, and LinkedHashMap are all thought of as collections, none are actually extended from Collection(interface).

collection (lowercase c), which represents any of the data structures in which objects are stored and iterated over.

Collection (capital C), which is actually the java.util.Collection interface from which Set, List, and Queue extend. (That's right, extend, not implement. There are no direct implementations of Collection.)

Collections (capital C and ends with s) is the java.util.Collections class that holds a pile of static utility methods for use with collections.

Collections come in four basic flavors:

1. Lists of things Ordered, duplicates allowed, with an index.

2. Sets of things May or may not be ordered and/or sorted; duplicates not allowed.

3. Maps of things with keys May or may not be ordered and/or sorted; duplicate keys are not allowed.

4. Queues of things to process Ordered by FIFO or by priority.


But there are sub-flavors within those four flavors of collections:

Sorted 
Unsorted
Ordered
Unordered

An implementation class can be unsorted and unordered, ordered but unsorted, or both ordered and sorted. 

But an implementation can never be sorted but unordered, because sorting is a specific type of ordering. 
For example, a HashSet is an unordered, unsorted set, while a LinkedHashSet is an ordered (but not sorted) set that maintains the order in which objects were inserted.

Ordered: When a collection is ordered, it means you can iterate through the collection in a specific (not-random) order. 

Sorted: Order in the collection is determined according to some rule or rules, known as the sort order. A sort order has nothing to do with when an object was added to the collection, or when was the last time it was accessed, or what "position" it was added at. Sorting is done based on properties of the objects themselves. You put objects into the collection, and the collection will figure out what order to put them in, based on the sort order. 

A collection that keeps an order (such as any List, which uses insertion order) is not really considered sorted unless it sorts using some kind of sort order. 

Most commonly, the sort order used is something called the natural order.

List Interface:

A List cares about the index. (Ordered but Not Sorted)

The one thing that List has that non-lists don't have is a set of methods related to the index. 

All three List implementations are ordered by index position—a position that you determine either by setting an object at a specific index or by adding it without specifying position, in which case the object is added to the end. 

ArrayList: Fast iteration and fast random access.

ArrayList implements RandomAccess interface—a marker interface (meaning it has no methods) that says, "this list supports fast (generally constant time) random access." 

Vector: It's like a slower ArrayList, but it has synchronized methods.

LinkedList: Elements are doubly-linked to one another. Good for adding/remove elements to the ends, i.e., stacks and queues.


Note : 
--> Choose ArrayList over a LinkedList when you need fast iteration i.e random access.
--> Go for Linked List when you need fast insertion and deletion.


Set Interface : A Set cares about uniqueness—it doesn't allow duplicates. 

HashSet: Fast access, assures no duplicates, provides no ordering.

LinkedHashSet: No duplicates; iterates by insertion order.

TreeSet: No duplicates; iterates in sorted order. Implements SortedSet.

Note : When using HashSet or LinkedHashSet, the objects you add to them must override hashCode(). If they don't override hashcode(), the default object. hashcode() method will allow multiple objects that you might consider "meaningfully equal" to be added to your "no duplicates allowed" set.

Map Interface : Cares about unique identifiers. Supports use of key/value or name/value pair. The Map implementations let you do things like search for a value based on the key, ask for a collection of just the values, or ask for a collection of just the keys. 

Like Sets, Maps rely on the equals() method to determine whether two keys are the same or different.

HashMap: unsorted, unordered Map. Fastest updates (key/value pairs); allows one null key, many null values.

Hashtable : Like a slower HashMap (as with Vector, due to its synchronized methods). No null values or null keys allowed.

LinkedHashMap : Faster iterations; iterates by insertion order or last accessed; allows one null key, many null values.

TreeMap : A sorted map. Implements SortedMap.

Note: 
LinkedList ---> faster insertions and deletions and slower iterations (compared to arraylist)
LinkedHashMap ---> faster iterations and slower insertions and deletions (compared to HashMap)

Queue Interface : Although other orders are possible, queues are typically thought of as FIFO (first-in, first-out). Queues support all of the standard Collection methods and they also add methods to add and subtract elements and review queue elements.

PriorityQueue: A to-do list ordered by the element's priority.

No comments:

Post a Comment