What is Collection in java


What are collections
                  Collection is a Framework which will have set of interfaces and classes which is used to store the data. Collection in general refers to group of data.

Before discussing about Collections, we will discuss about why we are going for collections. In java, collection of elements are stored  using Arrays.  Click here to read about Arrays in JAVA.

Advantages of  Arrays:
                              compile time checking is done for the element types
                              It can hold primitive data types and object references

Disadvantages of Arrays:
                             The size of arrays are fixed
                              No built in algorithms  to sort,search etc...
In order to overcome the disadvantages, we are going to collections.

   Collection [ Interface]
                   1 . Queue [ Interface]
                   2.  List [Interface]
                   3.  Set [ Interface ]
                              a] SortedSet[ Interface]
                              b] NavigableSet [ Interface]


Map is a special type of collection, which is used frequently

Map [ Interface]
                1. SortedMap [ Interface]
                2.  ConcurrentMap [ Interface]

Let us see the classes implementing the above collection interfaces.
           
Classes implementing List Interface
                   1. LinkedList
                   2. ArrayList
            Old collection classes            
                   3. Vector
                   4. Stack

Classes implementing set Interface
                 1. LinkedHashSet   - Stores data in insertion order
                 2. HashSet               - Stores data in random order
                 3. TreeSet                - Stores data in sorted order

Classes implementing Map Interface
                 1. LinkedHashMap       -Stores data in insertion order
                 2. HashMap                  -Stores data in random order
                 3. TreeMap                   -Stores data in sorted order
                         
             




Previous
Next Post »