Skip to content

Java Collection Framework Tutorial with Examples

Java Collection framework tutorial with examples will help you understand how to use the collection classes in an easier way. A collection in Java is an object that stores a group of objects as a single unit. Java collection framework provides several interfaces and classes to help group the objects as a unit.

The Java collection framework has two main interfaces, the Collection interface, and the Map interface. Several other classes and interfaces implement and extend these two main interfaces to make the collection framework.

Here is the pictorial representation of the Java Collection Framework hierarchy.

(click image to open it in a new window)

Java Collection Framework Cheat Sheet

As you can see from the above collection framework hierarchy, the Collection interface and Map interface are at the top of the hierarchy.

Collection Interface

The Collection interface is the root interface and provides common methods like add, remove, clear, contains, equals, hashcode, and iterator.

List interface

The List interface extends the Collections interface. The List represents index based ordered collection of the objects. The elements contained in the List are ordered and can be inserted, accessed or searched based on their index. The list may contain duplicate elements. The main classes implementing the List interface are ArrayList and LinkedList.

Set interface

The Set interface extends the Collection interface and represents a collection that does not contain any duplicate elements (it can only have one null element as well). The main classes implementing the Set interface are TreeSet, HashSet and LinkedHashSet.

Queue Interface

The Queue interface extends Collection interface and represents a collection that is usually ordered by FIFO (first in first out) order. The main classes implementing the Set interface are LinkedList, ArrayDeque and PriorityQueue.

Map Interface

The map interface is a root interface and allows storing the key value pairs. The map does not allow duplicate keys. Map interface does not guarantee the order of the elements, however, some implementations like TreeMap does. The main classes implementing the Set interface are Hashtable, HashMap, TreeMap and LinkedHashMap.

Utility Classes

In addition to the above mentioned main interfaces and classes, there are two utility classes that are part of the Java collection framework.

Java Collections and Arrays Utility classes

1. Collections class

The Collections class contains static utility methods that either accepts or returns the collection. The collection class provides many useful methods for shuffling, reversing, sorting and searching collection objects.

2. Arrays Class

Similar to the Collections class, the Arrays class contains static utility methods for manipulating arrays. The Arrays class provides many useful methods for sorting, searching, copying and filling the arrays.

Java Collection Examples

List Examples

ArrayList Examples

LinkedList Examples

Comparable Examples

Comparator Examples

HashMap Examples

LinkedHashMap Examples

HashSet Examples

LinkedHashSet Examples

Iterator Examples

ListIterator Examples

TreeMap Examples

TreeSet Examples

Arrays Examples

Vector Examples

References:
Java Collection interface Javadoc
Java Map interface Javadoc

Please let me know if you liked the Java Collection framework tutorial with examples in the comments section below.

About the author

10 comments

  1. Hello!
    In test Java Object Oriented Programming Quiz 4 I find mistake:
    In question
    What will happen when you compile and run the following code?

    abstract class One{
    public One(){
    System.out.println(“One”);
    }

    abstract void nothing();
    }

    public class Test{
    public static void main(String[] args) {
    One one = new One();
    }
    }
    the given correct answer is
    None of the above,
    since in explanation is written
    The code will give compilation error “Cannot instantiate the type One”.

    So, maybe, correct answer is Compilation error&

    1. Hello Paul,

      You are absolutely right. It was my mistake. The given explanation is right that the code will give compilation error, but the option number was wrong. As you said, the correct option is 3.
      I’ve now fixed it.

      Thanks a lot for your time in correcting my mistake.

      Thanks.

  2. You’re awesome, my friend. Your site is a big help for my technical assessment preparation. A few more quizzes on Java like, for Collections would be great.
    How about putting up tests fro agular and Spring as well?
    Thanks again!
    Cheers!!

    1. Hello Lora,

      Thanks for pointing it out. There was some problem with the page that has been fixed now. It should work now.

      Thanks.

  3. You are absolutely doing a great job. If possible few more tabs will be highly appreciated
    1.Java features from 8 to latest
    2.Sppring boot
    3.Microservices
    4.Interview preperation.

    I know it is too much to ask. Thanks for the wonderful website.

    1. Hello Saphal,

      I am glad you liked my work and thanks for the suggestions. I will surely try to add at least some of them.

      Thank you.

Leave a Reply

Your email address will not be published.