Saravanan's Corner: Blackberry Dev

Monday, 15 October 2018

Collection Framework

There are 9 key Interfaces are available in collection framework



1. Collection -

1. If you want to represent a group of an individual object as an entity, you should go for Collection

2 In general, the collection interface is considered a root interface of any collection interface

3. Define the most common methods which are applicable for any collection objects

4. There is no concrete class that implements the collection interface directly.

2. List Interface -

1. if you want to represent a group of an individual object as an entity where duplicates allowed and insert order is preserved

2. ArrayList, Linked List, Vector and Stacks are Implementation classes

Collections --> List --> ArrayList,
                                      Linked List,
                                      Vector-->Stacks
                                                                 
3. Collections came in 1.2

4. Vector came in 1.0 version which are legacy classes

3. SET

1. if you want to represent a group of individual objects as an entity where duplicates are not allowed and insert no order is preserved

2. Hash set, LinkedHashSet

Collections (1.2v)--> Set (1.2v)--> HashSet(1.2v) --> LinkedHashset (1.4)

Difference between SET and LIST

List - Duplicates allowed, Insertion order is preserved

SET - Duplicated not allowed, Insertion order is not preserved


4. SortedSet Interface

1. if you want to represent a group of individual objects as an entity where duplicates are not allowed and all objects inserted in sorting order

Collections (I)--> Set (I)--> SortedSet (I)(1.2v)

5. NavigableSet Interface

NavigableSet is child interface of SortedSet defines several methods for navigation purpose

Collections (I)--> Set (I)--> SortedSet (I)(1.2v) --> NavigableSet (I) (1.2v) --> TreeSet (Imp class) (1.2)

6. Queue Interface

If you want to represent a group of an individual object prior to processing then go for Queue

Queue Implementation classes: (V1.5)
 Priority Queue, Blocking Queue, Linked Blocking Queue, PriorityBlockingQueue

7. Map

If you want to represent a group of an individual object as a key-value pair then go for Map interface

- Map is not child interface of collection

- Duplicate keys are not allowed but values can be duplicate

Implementation classes:

Hashmap, LinkedHashmap, WeakHshMap, IdentityHashMap, HashTable, Properties, Dictionary

Map -> Hashmap -> LinkedHashmap (1.4v)

Map -> WeakHshMap (1.4v)

Map -> IdentityHashMap (1.4v)

Map -> HashTable
                                --> Properties (1.0v)
                                ---> Dictionary (1.0v)

8.SortedMap

If you want to represent a group of an individual object as a key-value pair with some sorted order of keys then go for SortedMap interface

Map --> SortedMap

9. NavigableMap

It defines several utility methods for navigation purpose

Map --> SortedMap --> Navigable Map (1.6v) --> TreeMap(Impl)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
-----------------------------------------------------------------------------------------
               |  Implements   | Order | Random | Key-Value | Duplicate | Null | Thread |
-----------------------------------------------------------------------------------------
ArrayList      |      List     |    X  |    X   |           |     X     |   X  |        |
-----------------------------------------------------------------------------------------
Vector         |      List     |    X  |    X   |           |     X     |   X  |    X   |
-----------------------------------------------------------------------------------------
LinkedList     | List, Deque   |    X  |        |           |     X     |   X  |    X   |
-----------------------------------------------------------------------------------------
HashSet        |      Set      |    X  |    X   |      X    |     X     |   X  |        |
-----------------------------------------------------------------------------------------
TreeSet        |   SortedSet   |    X  |    X   |           |           |      |        |
-----------------------------------------------------------------------------------------
Stack          | Vector, List  |    X  |        |           |     X     |   X  |    X   |
-----------------------------------------------------------------------------------------
Properties     | Hashtable,Map |       |    X   |      X    |           |      |    X   |
-----------------------------------------------------------------------------------------
HashMap        |      Map      |       |    X   |      X    |           |   X  |        |
-----------------------------------------------------------------------------------------
TreeMap        |   SortedMap   |    X  |    X   |      X    |           |      |        |
-----------------------------------------------------------------------------------------
Hashtable      |      Map      |       |    X   |      X    |           |      |    X   |
-----------------------------------------------------------------------------------------

Where to use ArrayList in java

This is obvious that if ArrayList is the part of Collection Framework then it will give some sort of operation (store, retrieve, searching, sorting, insertion, deletion, and advance manipulation) on the Objects stored inside it.
  • ArrayList provides faster iteration so where faster iteration or index-based retrieval is required and there are not many additions or deletion operation takes place then it is recommended to use ArrayList in java.
  • ArrayList is slower in addition or removal data, so where more operation of addition and deletion is required then the use of ArrayList is not recommended.
        
        System.out.println("Following is the output ArrayList with for loop");
        for(String name : listName){  // for loop to iterate arraylist
             System.out.println(name); 
        
         
        System.out.println("Following is the output ArrayList with for loop");
         
        Iterator itr = listName.iterator();//getting Iterator from arraylist to iterate elements 
        while(itr.hasNext()){
            String name = itr.next();
            System.out.println(name); 
        }

ArrayList in java collections framework is defined as below -
  • ArrayList in java is used to store the java element or object.
  • ArrayList class extends AbstractList class and implements List interface.
  • ArrayList stores and iterates data in order to which they are inserted.
  • ArrayList class uses arrays to store the element or object internally.
  • ArrayList size grows automatically when elements or objects are added to it.
  • Java ArrayList allows random access of elements and java objects.
  • ArrayList class can store duplicate elements or java objects.
  • ArrayList class is not thread-safe. if thread safety is required we need to synchronize this.

No comments:

Post a Comment