Java Collection framework - Introduction and general overview

Asfaq LeeonAsfaq Leeon
6 min read

Hello everyone. Today I'm going to talk about the Java Collection framework. In this article I'm not going to deep. This is an introductory. Let's get started. So, what is the Java Collection?

What is Collection

Collection is a single entity which represents a group of individual objects.

What Collection offers

  • We can increase/decrease the size of Collections unlike arrays.

  • Every Collection class is based on some standard data structures. For example, Linked List, Set, Hash table, Queue etc. So, we can get many readymade methods. We can also get some data structure variation. We don't need to implement by our own.

What is Collection framework

Collection framework provides several interfaces and classes which provides some special features of Java collection.

Limitations of Array

We already have array in Java, right? So, why we need another concept like Collection.

  • Arrays are fixed in size. We can't increase or decrease the size of array once created. So, we have to assume the initial size of array while declaring it.

  • Java is not meant for programming. It is created for development purpose initially. So, developers should not spent much time to implement the common data structures and algorithm. That's why Collection comes to a picture. The collection framework has many readymade data structures and algorithm implementations.

Difference between Array and Collection

Before move on, we have to know the key differences between the array and the collection. So, we can understand why Java creators make this another concept.

ArrayCollection
Fixed size. It creates a slot in the memory.Growable in nature. automatically it resize its size.
Memory wise not recommended. It is possible to waste a large number of memory in arrayMemory wise recommended. There is less possibility in Collection. The ArrayList is using array internally. But it can resize automatically. So, wasting a large number of memory is not possible.
Performance wise recommended if we know the capacity initially.Performance wise not recommended. For example, there are 1million data in an ArrayList. Now we need to add a single data. Maybe the ArrayList need to resize it. Now, the ArrayList will create another new big array. Copy those old 1 million data then insert the new one. So, copying 1million data is a huge performance drawback.
Support primitive and object data types.Support only object data types. For primitives we need wrapper types like Integer.
There are no readymade data structures or methods.Readymade data structures and methods available.
Can hold only homogeneous data type objects.Can hold both homogeneous using generics(this is an advanced concept, we can leave now) and heterogeneous data type objects. Heterogenous means, in a list we can add multiple types objects. For example, in a single list type we can add integer, string, student type objects at a time.

Overview of the Collection interface

All collection classes and interfaces

Here are some legacy classes. Those are Vector and Stack.

Overview of the Map interface

The legacy classes are Dictionary, Hashtable and Properties.

Properties of the Collection interface

There are some basic properties of the collection interface. Those helps us to think why Java creators create this interface. Those are

  • The Collection interface is the root interface of the Collection framework. Although the Map interface is not a part of the Collection interface, this interface play the vital role.

  • The Collection interface contains the most common methods which are applicable for any collection objects.

  • There is no concreate class that implements the Collection interface directly.

Child interfaces in the Collection interface

Let's talk about the child interfaces I've showed in the previous diagram one by one.

List

The first one is the List interface. It has some special properties. Those are

  • This one is also a child interface of Collection.

  • In set, duplicate values are not allowed. If we do, we will get exceptions.

  • Set collections do not preserve the insertion order. I'll explain later why not they don't preserve the insertion order.

SortedSet

  • This one is the child of Set

  • The key difference from the Set is that the insertion order is sorted order, not a random order like Set. What sorting order it is used I'll explain later. Right now just get familiar with the interfaces.

NavigableSet

  • This one is the child interface of SortedSet.

  • This interface contains several methods for navigation purposes. This makes different from SortedSet.

Difference between List and Set

So, based on our requirement we can decide which is good for our requirement. Let's differentiate the List and Set.

ListSet
Duplicates are allowedNot allowed
Insertion order is preservedNot preserved

Where we need a intact list, we should go for the List. When we have to work with only unique data and the insertion order is not important then we should go for Set.

Queue

Queue is famous for the FIFO. First in first out.

  • Queue is a child interface of Collection

  • When we need some priority order we should go for this.

  • The usual priority order is the FIFO(First in first out).

  • We can also implement our own priority order for processing.

Map

Once I thought Map is a part of Collection interface. Then I learned this is different from the Collection interface. Despite it is separate from the Collection interface, it is a part of the Collection framework.

  • The crucial property of the Map interface is the key-value pair concept. We can represent a group of individual objects as key-value pairs.

  • In map, duplicate keys are not allowed but the values can be duplicate.

Here is an example of key-value pair.

ID (Key)Name (Value)
1seema
2siam
3asfia

Child interfaces in Map

Let's talk about child interfaces in Map.

SortedMap

  • This is the child interface of Map

  • The key objects are maintained some sorting order while insertion.

  • On the other hand value objects have no restriction. They aren't maintaining any order.

NavigableMap

  • This is the child interface of SortedMap

  • Like NavigableSet, this contains some methods for navigation purposes.

Utilities

There are several utility classes and interfaces to enhance Java collections.

Sorting

To define sorting order there are 2 interfaces

  • Comparable (Interface)

  • Comparator (Interface)

Cursors

For traversing a collection, we need those cursor interfaces. After Java 8, we can also use the stream in a functional coding style. But still we need to learn those.

  • Enumeration (Interface)

  • Iterator (Interface)

  • ListIterator (Interface)

Here the Enumeration is a legacy one.

Utility classes

There are some utility classes for sorting searching, conversion etc purposes.

  • Collections

  • Arrays

Conclusion

There are many more. There are lot more to learn. The collection is a vast topic. It is difficult to cover in a single blog. Next I'll talk about the List and Set interfaces and their implementation classes. If you have any query, please leave a comment below. Till then stay safe. Happy learning.

1
Subscribe to my newsletter

Read articles from Asfaq Leeon directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Asfaq Leeon
Asfaq Leeon