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)
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.
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
- Convert List to Set
- Convert Map to List
- Convert List to String
- Iterate List example
- Iterate List in reverse order
- Convert List to array
ArrayList Examples
- Get elements from ArrayList
- Print ArrayList example
- Get first and last elements from ArrayList
- Add elements to ArrayList
- Java ArrayList capacity
- Iterate ArrayList using Iterator
- Iterate ArrayList using ListIterator
- Find the minimum or maximum value in ArrayList
- Convert array to ArrayList
- Convert ArrayList to array
- Compare two ArrayList
- Convert ArrayList to comma separated String
- Get unique values from ArrayList
- Java ArrayList of arrays
- Add array to ArrayList
- ArrayList reverse example
- Iterate Java ArrayList
- Get sublist from ArrayList
- Get random elements from ArrayList
- Shuffle elements of ArrayList
- Convert ArrayList to String[] array
- Find element in ArrayList using indexOf and lastIndexOf methods
- Search ArrayList element using binary search
- Java ArrayList replace element
- ArrayList clone example
- ArrayList insert element at the beginning
- Copy ArrayList elements to other ArrayList
- Remove the last element from ArrayList
- Check if ArrayList contains element
- Iterate ArrayList using for loop or for each loop
- Remove element from ArrayList
- Check if ArrayList is empty
- Clear or empty ArrayList
- Get ArrayList length
- Initialize ArrayList
- Remove duplicate elements from ArrayList
- Convert comma separated String to ArrayList
- Convert String to ArrayList
- Sort ArrayList using Comparator
- Convert String array to ArrayList
LinkedList Examples
- Convert HashMap to LinkedList
- Convert LinkedList to String
- Iterate LinkedList example
- Iterate LinkedList in reverse order
- Convert Java LinkedList to Array
- Convert array to LinkedList in Java
- Java LinkedList add array example
- Java LinkedList remove duplicate elements
- Get elements from Java LinkedList
- Get first and last elements of LinkedList in Java
- Add elements to Java LinkedList example
- How to iterate LinkedList in reverse direction (backward) in Java
- Java LinkedList replace elements
- Java LinkedList remove elements
- Find index of LinkedList elements using indexOf and lastIndexOf methods
- Check if element exists in Java LinkedList (LinkedList contains element)
- How to sort LinkedList in Java example
- Java LinkedList remove the last element
- How to clear Java LinkedList (empty LinkedList)
- Check if the LinkedList is empty in Java
- Java LinkedList get size example (LinkedList length)
- Java LinkedList add an element at the beginning (at the front)
- Java LinkedList print elements example
Comparable Examples
- Search ArrayList elements using Comparable
- Sort LinkedList using Comparable
- Sort LinkedHashMap by keys using Comparable interface
- Sort LinkedHashMap by values using Comparable interface
- How to create TreeMap objects using Comparable interface
- How to sort TreeSet elements using Comparable interface
- How to sort HashSet elements using Comparable
- How to sort LinkedHashSet elements using Comparable
- Sort Java Vector using Comparable
- Find the minimum or maximum element of Vector using Comparable
Comparator Examples
- Sort String array using Comparator
- Binary search ArrayList elements using Comparator
- Convert HashMap to TreeMap using Comparator
- Sort HashMap by key using Comparator
- Sort HashMap by values using Comparator
- Sort ArrayList elements using Comparator
- Sort String array of numbers using Comparator
- Sorting custom objects using Comparator
- Sort LinkedList using Comparator
- Sort LinkedHashMap by keys using Comparator interface
- Sort LinkedHashMap by values using Comparator interface
- How to create a new TreeMap object using the Comparator interface
- How to sort TreeSet elements using Comparator interface
- How to sort HashSet elements using Comparator
- How to sort LinkedHashSet elements using Comparator
- Find the minimum or maximum element of Vector using Comparator
- Sort Java Vector using Comparator
HashMap Examples
- How to create HashMap in Java (HashMap constructors)
- Iterate HashMap
- Sort HashMap by keys
- Sort HashMap by values
- Convert HashMap to TreeMap
- Convert Map to List
- How to maintain insertion order in HashMap
- How to get HashMap size example
- How to check If HashMap is empty example
- Java HashMap print example
- Delete all mappings or clear HashMap example
- How to replace a value for key in Java HashMap example
- Check if key exists in HashMap Java example
- Check if value exists in HashMap Java example
- Java HashMap get key using value example
- Java HashMap get method example
- Java HashMap put method example
- Get all keys of Java HashMap example
- Get first key or first value of Java HashMap example
- Iterate Java HashMap using forEach or for loop example
- Read text file to Java HashMap example
- Convert ArrayList to HashMap Java example
- Convert String or String array to HashMap
- Write HashMap to a text file in Java
- Compare two HashMap objects
LinkedHashMap Examples
- Print Java LinkedHashMap
- How to add key-value pairs to LinkedHashMap in Java
- How to check LinkedHashMap size
- How to check if LinkedHashMap is empty
- How to clear or remove all mappings from LinkedHashMap
- How to convert LinkedHashMap to List in Java
- Sort LinkedHashMap by keys
- Sort LinkedHashMap by values
- Convert ArrayList to LinkedHashMap in Java
- Convert LinkedHashMap to two arrays
- How to iterate LinkedHashMap
- How to iterate LinkedHashMap in reverse order (backward direction)
- Java LinkedHashMap forEach example
- How to get all keys of LinkedHashMap in Java (keySet method)
- How to get all values of LinkedHashMap in Java (values method)
- How to get first or last entry from Java LinkedHashMap
- How to check if LinkedHashMap contains a key (indexOf)
- How to check if LinkedHashMap contains a value
- How to get LinkedHashMap value by index
- How to get LinkedHashMap key by index
- How to maintain insertion order of elements of HashMap using LinkedHashMap
HashSet Examples
- How to create new HashSet objects using HashSet constructor
- How to add elements to Java HashSet (add, addAll method)
- How to print Java HashSet elements
- How to sort Java HashSet elements
- How to check HashSet size (size method)
- How to check if HashSet is emtpy (isEmpty method)
- How to iterate HashSet in Java
- How to find the minimum or maximum value from Java HashSet
- How to get first or last elements from Java HashSet
- How to check if element exists in Java HashSet
- How to get random elements from Java HashSet
- How to remove elements from HashSet
- How to clear or remove all elements from HashSet
- How to get elements by index from HashSet in Java
- How to compare two HashSet objects using the equals method
- How to copy HashSet or append HashSet to another HashSet
- How to preserve insertion order of Java HashSet elements
- How to convert Java HashSet to comma separated string
- How to convert comma separated string to Java HashSet
- How to convert HashSet to ArrayList
- How to convert ArrayList to HashSet
- Convert HashSet to array
- Convert array to HashSet
- Convert List to HashSet
LinkedHashSet Examples
- How to create new objects of LinkedHashSet class (LinkedHashSet constructors)
- How to add elements to LinkedHashSet object
- How to get the size of the LinkedHashSet in Java
- How to iterate LinkedHashSet elements
- How to remove an element from LinkedHashSet in Java
- How to clear or remove all elements from LinkedHashSet
- How to get the first or last element from LinkedHashSet
- How to print LinkedHashSet elements (display LinkedHashSet elements)
- How to check if LinkedHashSet is empty in Java
- How to check if element exists in LinkedHashSet
- How to get random elements from LinkedHashSet
- How to sort LinkedHashSet in Java
- How to get LinkedHashSet elements by index in Java
- How to find element index in LinkedHashSet
- How to find the minimum or maximum element in LinkedHashSet
- How to copy merge or clone LinkedHashSet in Java
- How to convert LinkedHashSet to array in Java
- How to convert an array to LinkedHashSet in Java
- How to convert ArrayList to LinkedHashSet in Java
- How to convert LinkedHashSet to ArrayList in Java
Iterator Examples
- Iterate through List using Iterator
- Iterate through HashMap using Iterator
- Iterate through ArrayList example
- Iterate through List in reverse order using Iterator
- Java ArrayList Iterator example
- Iterate through LinkedHashMap using an Iterator
- Iterate through Java TreeMap using an iterator
- Iterate Java TreeMap in reverse direction using an iterator
- Iterate through elements of TreeSet in Java using an iterator
- Iterate through elements of HashSet in Java using an iterator
- Iterate through elements of LinkedHashSet in Java
- Iterate over Vector elements
- Iterate over Vector elements in reverse or backward direction
ListIterator Examples
- Iterate through ArrayList using ListIterator
- Iterate List in reverse order using ListIterator
- Iterate ArrayList elements using ListIterator
- Iterate List using ListIterator
- Iterate LinkedList in reverse direction using ListIterator
- Iterate through LinkedHashMap in reverse order using ListIterator
- Iterate over Vector elements using ListIterator
- Iterate Vector in reverse direction using ListIterator
TreeMap Examples
- How to convert HashMap to TreeMap in Java
- How to create a new TreeMap object in Java (TreeMap constructors)
- How to print TreeMap in Java
- How to get the size of the TreeMap
- How to check if the TreeMap is empty
- How to fix java.lang.ClassCastException – cannot be cast to java.lang.Comparable while adding entries to the TreeMap
- How to get all keys from the TreeMap
- How to get all values from the TreeMap
- How to check if the key exists in the TreeMap (TreeMap contains a key)
- How to check if the value exists in the TreeMap (TreeMap contains a value)
- How to remove all entries or clear TreeMap in Java
- How to get value from TreeMap using the key (TreeMap get method)
- How to add key-value mapping to the TreeMap (TreeMap put method)
- How to replace a value for the given key in the TreeMap (TreeMap replace method)
- Java TreeMap forEach example
- How to iterate TreeMap in Java
- How to iterate Java TreeMap in reverse order
- How to get TreeMap key or TreeMap value using index
- How to get TreeMap key using the value in Java
- How to compare two TreeMap objects in Java
- How to convert Java TreeMap to an ArrayList (List)
- How to get last key, last value, or last entry from the Java TreeMap
- How to get submap, headmap, and tailmap from Java TreeMap
- How to remove the first entry or last entry from the Java TreeMap
- How to get TreeMap key, value or entry greater than or less than the specified key
TreeSet Examples
- How to create new objects of TreeSet using the TreeSet constructors
- How to add elements to TreeSet
- How to fix Java TreeSet java.lang.ClassCastExcpetion exception
- How to print TreeSet elements in Java
- How to get size of the TreeSet object (TreeSet length)
- How to iterate over elements of TreeSet in Java
- How to get the first and last elements of the TreeSet
- How to remove elements from Java TreeSet (remove first, last, all elements)
- How to clear TreeSet (remove all elements)
- How to check if element exists in TreeSet (TreeSet contains element)
- Find elemnet index in Java TreeSet
- Get elements by index from Java TreeSet
- How to check if TreeSet is empty (isEmpty method)
- How to compare two TreeSet objects using the equals method
- How to get headset, tailset, and subset from the Java TreeSet
- How to copy, merge, or clone the TreeSet in Java
- How to get floor or ceiling values from Java TreeSet
- How to reverse order of the TreeSet elements (iterate TreeSet in reverse order)
- How to get a union or intersection of two TreeSet objects
- How to convert an array to TreeSet
- How to convert TreeSet to an array
- How to convert HashSet to TreeSet in Java
- How to convert TreeSet to List (ArrayList or LinkedList)
- How to convert ArrayList to TreeSet
Arrays Examples
- Find array element index using Arrays class
- Add all elements of an array to ArrayList using Arrays
- Initialize ArrayList with elements using Arrays
- Print ArrayList elements using Arrays
- Remove duplicate elements from String array using Arrays class
- Convert comma separated String to ArrayList using Arrays
- Convert array to Set using Arrays
- Print array elements using Arrays class
- Convert String to ArrayList using Arrays
- Convert String to String array using Arrays
- Convert String array to String using Arrays
- Check if array contains value using Arrays class
- Sort String array containing numbers using Arrays
- Sort String array using Arrays class
- Reverse array using Arrays class
- Convert String array to ArrayList using Arrays
- Convert Java LinkedList to Array
- Convert Java TreeSet to array
- Get random elements from Java HashSet using Arrays class
- Convert array to LinkedHashSet using Arrays class
- Convert Java array to Vector using Arrays class
Vector Examples
- How to create new Vector objects in Java
- How to add elements to Vector in Java
- How to replace an element at a specific index of the Vector
- How to insert an element at the beginning of the Java Vector
- How to get elements from Vector (Vector get method)
- How to get first and last elements from the Vector
- How to get random elements from the Vector
- How to get the size of the Vector (Vector length)
- How to remove elements from the Vector
- How to clear or remove all elements from the Vector
- How to remove duplicate elements from the Vector
- How to remove the first element or last element from the Vector
- How to check if Vector is empty in Java (isEmpty method)
- How to check if element exists in Vector in Java
- How to find an element in the Vector using indexOf and lastIndexOf methods
- How to find the minimum or maximum element from the Vector
- How to iterate through the Vector elements
- How to iterate the Vector elements in the reverse order (backward direction)
- How to sort Vector in Java
- How to shuffle Vector elements in Java
- How to copy, clone, and append (merge) one Vector to another Vector in Java
- How to convert an array to Vector
- How to convert Vector to an array
- How to convert Vector to ArrayList and ArrayList to Vector in Java
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.
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&
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.
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!!
Hello Steve,
Glad it helped you. There will be more quizzes on various topics for sure in near future.
Thank you.
I need whole notes of java
Hi Pavan,
Sorry, I did not understand the question. Do you want suggestions for a good Java book?
Thanks.
Hello, I have a problem with the Quizes, they are not checking my results.
Hello Lora,
Thanks for pointing it out. There was some problem with the page that has been fixed now. It should work now.
Thanks.
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.
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.