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.

23 Comments Collection in Java

  1. baby jhansi

    mam can please explain about setters and getters in java..? how and where should we use the setters&getters..?? if we doesn’t use them then what happens..??

    Reply
    1. J Singh

      Hi,

      As name suggest setter and getters are used to set and get value of particular property in a class. These are methods inside a class.

      Setter (Set): Set some value in property of class
      Getter (Get): Get value of a property in class.

      It’s upto you to use these method as per requirement. If youdon’t declare these methods you won’t find any way to set or get value of a property in class.
      Hope it help but i will write a separate article on Java Bean which will cover all these aspects.

      Regards

      Reply
  2. divya

    your tutorials are very clear when compared to other tutorials available over internet.. Thank u very much for this effort.

    Reply
  3. Ruchika

    Please give some information about Map Interface. I have one problem also… i want know about “null interface” is really there is concept like null interface.

    Reply
    1. Vivekanand Gautam

      Hi Ruchita,

      This is first time i am hearing about Null Interface. I will check if there is anything like Null interface in Java or not. But as for as my knowledge is concerned there is nothing like it in Java.

      I will try to add Map Interface related details soon if i get time.

      Thanks

      Reply
      1. Ruchika

        Hi Sir,

        I got some information about null interface in Java, it is as follows:-

        1.Null interface does not contain any abstract method and it has a special meaning for jvm.

        2.Null interfaces act as markers..they just tell the compiler that the objects of this class need to be treated differently.

        3.It gives a message to the java compiler that it can add some special behaviour towards the class which implementing it.

        Eg’s of Marker Interfaces are:-
        – java,lang.Cloneable
        – java,io.Serializable
        – java.util.EventListener

        For Eg:- java.io.Serializable is a marker interface, it doesn’t contain any method declarations.

        When a java class is to be serialized , i.e when we want to store the state of an object or pass through the network, we should intimate the java compiler in some way that there is a possibility of serializing this java class. For this, marker interfaces are used.

        The java class which needs to be serialized has to implement the java.io.Serializable marker interface and by doing this we are informing the java compiler to add that capability.

        Reply
        1. Vivekanand Gautam

          Hi Ruchika,

          I must admit. I almost forgot about this. Yes i do know about Marker Interface in Java. But i forgot that Marker Interface is also known as Null Interface. Thank you for reminding me. It will be useful for others too.

          Also details provide by you for Marker Interface is quite god. We would love to have you as Writer on our website. 🙂 Do let me know in case you are interested. Feel free to share Articles on different subject with us.

          Thanks

          Reply
    1. sandeepna

      Unfortunatly author is replying to girls message only.
      I felt bad about it so repling to your message.

      Check other sites please

      Reply
      1. Vivekanand Gautam

        Hi Sandeepna,

        I don’t know why u have this feeling that i only reply to girl. 😛
        May be coz on given article i have replied to a girls msg. Normally i reply on mail and don’t put those messages on blog. But this time i will definitely add this reply on blog. So that no one have this kind of feeling. If i can i always reply to all mails.

        Regards

        Reply

Leave 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.