Friday, October 14, 2011

All About Java Collection : Useful Point wise information



What Do You Do with a Collection?
  • Add objects to the collection.
  • Remove objects from the collection.
  • Find out if an object (or group of objects) is in the collection.
  • Retrieve an object from the collection (without removing it).
  • Iterate through the collection, looking at each element (object) one after another.
Key Interfaces and Classes of the Collections Framework

  • Collection
  • Set
  • SortedSet
  • List
  • Map
  • SortedMap
  • Queue 

Maps
·         HashMap
·         Hashtable
·         TreeMap
·         LinkedHashMap

Sets
·         HashSet
·         LinkedHashSet
·         TreeSet

Lists
·         ArrayList
·         Vector
·         LinkedList

Queues
·         PriorityQueue

Utilities
·         Collections
·         Arrays


Note : Basically You will find three kinds of collection words in this framework but each has its own importance given as below

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

ii)       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.)

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


List Interface
  • A List cares about the index.
ArrayList
  • This as a growable array
  • It gives you fast iteration and fast random access
  • It is an ordered collection (by index), but not sorted

Vector
  • Vector is a holdover from the earliest days of Java
  • Vector and Hashtable were the two original collections
  • Vector is basically the same as an ArrayList
  • Vector methods are synchronized for thread safety
  • You'll normally want to use ArrayList instead of Vector because
  • the synchronized methods add a performance hit you might not need
  • Vector is the only class other than ArrayList to implement RandomAccess

LinkedList
  • A LinkedList is ordered by index position, like ArrayList, except that the elements are doubly-linked to one another
  • LinkedList  is best for adding and removing from the beginning or end, which makes it an easy choice for implementing a stack
  • or queue.
  • LinkedList may iterate more slowly than an ArrayList

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

HashSet

  • A HashSet is an unsorted, unordered Set
  • It uses the hashcode of the object being inserted
  • Use this class when you want a collection with no duplicates and you don't care about order when you iterate through it

LinkedHashSet

  • A LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements.
  • Use this class instead of HashSet when you care about the iteration order.

  • When you iterate through a HashSet the order is unpredictable, while a LinkedHashSet lets you iterate through the elements in the order in which they were inserted.

TreeSet

  • The TreeSet is sorted collections

  • It uses a Red-Black tree structure, and guarantees that the elements will be in ascending order, according to natural order

  • TreeSet, lets you define a custom sort (your own rule to sort) order via a Comparable or Comparator)

Map Interface
  • A Map cares about unique identifiers

HashMap

  • The HashMap gives you an unsorted, unordered Map
  • A Map cares about unique identifiers.
  • You map a unique key (the ID) to a specific value
  • Map implementations let you do things like search for a value based on the key

Hashtable

  • Like Vector, Hashtable has existed from prehistoric Java times

  • Looks HashMap vs. Hashtable. Where's the capitalization of t?
  • Hashtable is the synchronized counterpart to HashMap
  • Hashtable are synchronized,  means that the key methods of the class are synchronized
  • HashMap lets you have null values as well as one null key, a Hashtable doesn't let you have anything that's null.

LinkedHashMap

  • LinkedHash-Map collection maintains insertion order

  • It will be somewhat slower than HashMap for adding and removing elements,

  • Iteration with a LinkedHashMap will be faster

TreeMap

  • TreeMap is a sorted Map by natural order

  • Like TreeSet, TreeMap lets you define a custom sort order via a Comparable or Comparator

Queue Interface
  • A Queue is designed to hold a list of "to-dos," or things to be processed in some way.

PriorityQueue

  • We PriorityQueue is to create a "priority-in, priority out" queue as opposed to a typical FIFO queue

  • A PriorityQueue's elements are ordered either by natural ordering (in which case the elements that are sorted first will be accessed first) or according to a Comparator

  • In either case, the elements' ordering represents their relative priority




No comments:

Post a Comment