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.
The Collection interface is the root interface and provides common methods like add, remove, clear, contains, equals, hashcode, and iterator.
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.
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.
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.
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.
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
- Convert List to Set
- Convert Map to List
- Convert List to String
- Iterate List example
- Iterate List in reverse order
- Convert List to array
- 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
- Convert HashMap to LinkedList
- Convert LinkedList to String
- Iterate LinkedList example
- Iterate LinkedList in reverse order
- 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
- 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
- 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 ArrayList using ListIterator
- Iterate List in reverse order using ListIterator
- Iterate ArrayList elements using ListIterator
- Iterate List using ListIterator
- 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
Please let me know if you liked the Java Collection framework tutorial with examples in the comments section below.