JavaTutorial : Collection in Java


Collection Framework has been added in JDK 1.2 and has been expanded in 1.4 -1.6

Interface & Classes of Collection Framework

Collection API provides a group of interface to choose from, but it also gives you some concrete classes to directly play with.

Core Interfaces

  • Collection
  • List
  • Set
    • SortedSet  (Extends Set Interface)
    • NavigableSet (Extends SortedSet)
  • Map
    • SortedMap (Extends Map Interface)
    • Navigable Map (Extends SortedSet)
  • Queue

Implemented Classes

Map

  • HashMap
  • HashTable
  • TreeMap
  • LinkedHashMap

Sets

  • HashSet
  • LinkedHashSet
  • TreeSet

Lists

  • ArrayList
  • Vector
  • LinkedList

Queue

  • AbstractQueue
    1. ArrayBlockingQueue
    2. ConcurrentLinkedQueue
    3. DelayQueue
    4. LinkedBlockingDeque
    5. LinkedBlockingQueue
    6. LinkedTransferQueue
    7. PriorityBlockingQueue
    8. PriorityQueue
    9. SynchronousQueue

Deque

  • ArrayDeque
  • ConcurrentLinkedDeque

Arrays

Note: Not all the Collection Framework implement the Collection Interface. None of the Map related classes & Interface extend from Collection.
Collections can also be divided in 4 part based on below options
  1. Sorted
  2. Unsorted
  3. Ordered
  4. Unordered
Â

Â

Ordered Collection

An ordered collection can be iterated in a specific order(not-random). E.G.: Array, HashTable
  1. LinkedHashSet (Iteration Order is predictable)
  2. Array
  3. HashTable
  4. ArrayList
Â
Un-Ordered Collection
  1. HashSet

Â

Sorted Collection

In sorted collections order of the collections is determined based to some rules. E.G: List

Â
Â

List Interface

Property:
Cares about the index
Has methods related to index(indexOf..)
Ordered by Index postion
ArrayList
Syntax


Property:
  • Growable array
  • Fast Iteration
  • Fast Random Access
  • Ordered collection by index
  • Unsorted
Â

Â

Vector

Same as an ArrayList but methods in Vector are synchronized.

Â

LinkedList

Property
  • Elements are doubly-linked to each other.
  • Ordered by Index position
  • Good for implementing Stack and Queue
  • Fast Insertion and Deletion
Â

Â

Set Interface

Set is used when uniqueness of the object is to be consider. It doesn’t allow duplicate object. To determine if two objects are equal or not Set uses equals() and hashcode() methods.

Â

HashSet

Property
  • Unsorted
  • Unordered
  • Uses hashcode of the objects
  • No Duplicate object
  • LinkedHashMap
  • Ordered(Insertion)
  • Uses hashcode of the objects
  • No Duplicate object
Â

Â

TreeSet

  • Sorted(Uses Tree Structure)
  • Uses hashcode of the objects
  • No Duplicate object
Map Interface
Â
â€"â€"â€"â€"â€"â€"â€"â€"â€"â€"â€"â€"â€"â€"â€"â€"â€"â€"â€"â€"â€"â€"â€"â€"â€"â€"â€"â€"â€"â€"

Concurrent Collection Interface

  • BlockingQueue extends Queue
  • TransferQueue extends BlockingQueue
  • BlockingDeque extends BlockingQueue
  • ConcurrentMap extends Map

Concurrent Collection Implementation

  • LinkedBlockingQueue extends AbstractQueue implements BlockingQueue
  • ArrayBlockingQueue extends AbstractQueue implements BlockingQueue
  • PriorityBlockingQueue extends AbstractQueue implements BlockingQueue
  • DelayQueue extends AbstractQueue implements BlockingQueue
  • SynchronousQueue extends AbstractQueue implements BlockingQueue
  • LinkedBlockingDeque extends AbstractQueue implements BlockingDeque
  • LinkedTransferQueue extends AbstractQueue implements TransferQueue
  • CopyOnWriteArrayList implements List, RandomAccess
  • CopyOnWriteArraySet extends AbstractSet
  • ConcurrentSkipListSet extends AbstractSet implements NavigableSet
  • ConcurrentHashMap extends AbstractMap implements ConcurrentMap
  • ConcurrentSkipListMap extends AbstractMap implements ConcurrentNavigableMap
Â

Factors that could help on deciding Collection Class

There are various factors that can be considered when selecting an appropriate collection for a particular problem. These factors are:
  1. Ordering â€" Some sort of ordering in the elements. For example, sorted order, insertion order or no specific ordering.
  2. Duplicates â€" May or may not want to allow duplicate elements in a collection.
  3. Thread Safe â€" Ensure the safety of the elements in a collections in case there are multiple threads accessing it.
  4. Key-Value pair â€" Store in key-value pairs.
  5. Blocking operations â€" Wait for the collection to become non-empty when retrieving an element.
  6. Random Access â€" Instant retrieval of an element.
  7. Upper Bounds â€" To limit the maximum number of elements a collection can hold.
There are also other factors like priority, delay etc..
Â

Cheat-sheet

  1. Collection is a data structure in which Objects are stored.
  2. Objects can be Added, Deleted and can traversed in Collection.
  3. There are 4 type of basic Collection
  4. List : Ordered, Duplicates are allowed, Indexed
  5. Sets : May or may not Ordered. Duplicates are not allowed.
  6. Maps : Duplicate keys are not allowed.
  7. Queue : Ordered by FIFO or priority.
  8. ArrayList : Fast Iteration & Fast Random Access.
  9. Vector: Synchronized Method.
  10. LinkedList : Good for implementing Stack and Queue.
  11. HashSet : Fast Access, No Duplicates, No Ordering.
  12. LinkedHashSet : No Duplicates, Iterates by insertion order.
  13. TreeSet : No Duplicates, Iterates in sorted order.

Note: if any help is needed just comment us below we definately help you out

Previous
Next Post »