Java Collections Framework
This tutorial will introduce you to the Java Collections Framework. Java Collections Framework is one of the most important topics. Because you can do a lot of data. Java Collections Framework provides interfaces and classes to represent and manipulate the data. Java Collections Framework reduces the efforts you put in programming, there is a small learning curve once understood you are good to go.
You can use the Java Collections Framework when you have a huge data set to process. For example, you have a list of cities that you first need to sort in ascending or descending order then the sorted list will have to be displayed on the GUI. In this case, collections are very useful. You can use the different type of collection classes depending on the use case. Follow the tutorial to understand the use cases of each type of collection with the example.
Before proceeding with this tutorial let’s acknowledge the confusion that people have around this topic.
- Newbie to Java Collections Framework generally gets confused in calling it “Java Collection Framework” or “Java Collections Framework”. It is written as Java Collections Framework. It has Collection Interface and Collections Class. Collection Interface is the root interface of the Collections framework. Which means every single class in Collections Framework implements Collection interface directly or indirectly. And, Collections Class is a utility class contains static methods for doing operations on objects of classes which implements the Collection interface.
- The second confusion is regarding Map, “whether it is a part of Collections Framework or not?” Map is an interface which does not extents the Collection interface so it is not a true collection. However, these interfaces contain collection-view operations, which enable them to be manipulated as collections. Hence, studying under Java Collections Framework.
|Concrete Class/Map||Interface||Duplicate Elements||Ordered||Sorted||Allow Null?|
|LinkedList||List, Queue||Yes||By Index||No||Yes|
|LinkedHashSet||Set||No||By Insertion Order||No||Yes|
|HashMap||Map||unique keys||No Order||No||Yes|
|HashTable||Map||unique keys||No Order||No||No|
|LinkedHashMap||Map||unique keys||By Insertion Order or Last Accessed||No||Yes|
|TreeMap||SortedMap||unique keys||Key Order||Natural/Custom||No|
|PriorityQueue||Queue||Yes||Natural Ordering||Natural Ordering||No|
Let’s get started with the understanding of Java Collections Framework with the quick visual.
The diagram below is the extended version of Java Collections Framework but doesn’t show all the interfaces (like RandomAccess, Cloneable, Serializable) so that it is easy to understand at the same time give a clear picture of what the hierarchy looks like.
Interfaces, Abstract classes, and Classes are marked clearly. The classes are in blue and provide the necessary methods for programming.
Java Collections Framework: As its name suggests, it is a collection, collection of objects called elements. A collection can be ordered or unordered some collections allow duplicate and some are not. We will see
Collection Interface: You can not use the Collection Interface directly in the code. Instead, it has been extended by List, Set, and Queue.
List Interface: List is ordered means preserve the order, but not sorted. It allows duplicate elements and null as well.Elements can be accessed by using the index number, indexing starts from zero.
Set Interface: Set doesn’t allow duplicates elements. But other features can vary depending on the implementing class like HashSet doesn’t preserve order but LinkedHashSet and TreeSet does.
Queue Interface: A Java Queue is a collection that holds elements before performing any operation. Here Objects are added to the tail and removed from the head and the object added first will be removed first.
Map Interface: Map, as said does not extend Collection Interface so represent separately. Boxes in blue are the classes that are used for programming. It contains Key-Value pair. Key will be unique. No class extending Map preserves the order accept TreeMap class