Collection in Java

This entry is part 22 of 34 in the series Core Java Course

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

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.
Series Navigation<< Java Exceptions TutorialJava Hashmap tutorial >>

22 Comments Collection in Java

    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. :P
        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
      2. rk

        hee…u pepole has 35 % reservation .that y he giving proirty for u …u have to more happy for that :p

        Reply
  1. 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
  2. divya

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

    Reply
  3. 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

Leave A Comment

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