Collection in Java

Categories: Java // Tags: , , .
Email this to someonePrint this pageShare on Google+3Share on Facebook1Tweet about this on TwitterShare on LinkedIn1Share on Reddit0Pin on Pinterest0Share on StumbleUpon1

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  in java is a  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
      1. SortedSet(Extends Set Interface)
      2. NavigableSet(Extends SortedSet)
    • Queue
    • Map
      1. SortedMap(Extends Map Interface)
      2. Navigable Map (Extends SortedSet)

Implemented Classes

Map

  • HashMap
  • HashTable
  • TreeMap
  • LinkedHashMap

Sets

  • HashSet
  • LinkedHashSet
  • TreeSet

Lists

  • ArrayList
  • Vector
  • LinkedList

Queue

  • AbstractQueue
    • ArrayBlockingQueue
    • ConcurrentLinkedQueue
    • DelayQueue
    • LinkedBlockingDeque
    • LinkedBlockingQueue
    • LinkedTransferQueue
    • PriorityBlockingQueue
    • PriorityQueue
    • 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

  • LinkedHashSet (Iteration Order is predictable)
  • Array
  • HashTable
  • ArrayList

Un-Ordered Collection

  • 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:

  • Ordering – Some sort of ordering in the elements. For example, sorted order, insertion order or no specific ordering.
  • Duplicates – May or may not want to allow duplicate elements in a collection.
  • Thread Safe – Ensure the safety of the elements in a collections in case there are multiple threads accessing it.
  • Key-Value pair – Store in key-value pairs.
  • Blocking operations – Wait for the collection to become non-empty when retrieving an element.
  • Random Access – Instant retrieval of an element.
  • Upper Bounds – To limit the maximum number of elements a collection can hold.

Email this to someonePrint this pageShare on Google+3Share on Facebook1Tweet about this on TwitterShare on LinkedIn1Share on Reddit0Pin on Pinterest0Share on StumbleUpon1

About Team

Browse Archived Articles by Team