Scroll to top

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 the 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

1- Map

  • HashMap
  • HashTable
  • TreeMap
  • LinkedHashMap

2- Sets

  • HashSet
  • LinkedHashSet
  • TreeSet

3- Lists

  • ArrayList
  • Vector
  • LinkedList

4- Queue

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

5- Deque

  • ArrayDeque
  • ConcurrentLinkedDeque

6- Arrays

Not all the classes in Collection Framework implement the Collection Interface.

None of the Map related classes & Interface extend from Collection.

Collections can also be divided based on sorting and ordering.


1- 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

2– Un-Ordered Collection

  1. HashSet

3- Sorted Collection

In sorted collections order of the collections is determined based on some rules. e.g. List


List Interface

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

Syntax

List l = new ArrayList<E>();

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 the uniqueness of the object is to be considered. 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

——————————————————————————————

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 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 the priority, delay etc..

Cheat-sheet

  1. A collection is a data structure in which Objects are stored.
  2. Objects can be Added, Deleted and can be traversed in Collection.
  3. There are 4 type of basic Collection
  4. List: Ordered, Duplicates are allowed, Indexed
  5. Sets: May or may not Be 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.
Author avatar
JBT
https://javabeginnerstutorial.com/

23 comments

  1. SAMIM

    I THINK THIS IS BEST SITE FOR JAVA STUDY

  2. This is really nice tutorial about collection
    thanks

Post a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.