Java Collections Framework

Nimasha Madhushani
LinkIT
Published in
3 min readJan 29, 2024
image by author

Hi techies…😎, This is the first blog to have an understanding of the Java collection framework. After reading this mini blog, you will realize why we should learn about collections.

Ok…, Let’s have a start🐣

📌Introduction

In Java, the collection framework has provided a certain no: of classes and interfaces. So we have,

  • ArrayList
  • List
  • HashMap
  • HashTable
  • Tree
  • Stack

📌What is Collection

  • A collection is a Group of Objects(A single entity which represents multiple objects)

If there are multiple students in a class we can say it is a collection of students, If there are many employees in a company we can say it is a collection of employees.

image by author

To represent a group of elements we need a collection.

📌What is the Collection Framework

Defines the different interfaces and classes representing a group of objects to a collection.

For example collection framework,

  • ArrayLists in Java can be used to store elements in a collection

📌Problems with Array Concept

The way of storing a value in a variable,

  • int a = 10; (int — data type, a — variable, 10 — value)

If we need to store 100 values, we need to initiate 100 variables. It is not a best programming practice. So, we can use the array concept to store multiple values.

  • int a[] = new int[100];(This is a single variable, that we can store multiple values). This will create 100 memory locations. So, arrays can be used to store multiple elements/objects.
  • We can consider the indexes in an array to access the memory locations.
  • However, we have some limitations in the array concept. Therefore, we can use a collection framework to overcome these limitations.
  • int a[] = new int[10]; Here we can only store 10 elements, as we have only 10 memory locations under this array variable.

In an array, we can store only homogeneous data. If we create an integer-type array in the above example, we can store only integer-type elements.

If we create an array with a Student Class object, in every memory location we can store a student detail object. As this is a Student type we can only store student detail objects.

📌Object Array

Object a[] = new Object[5]; In this array, we can store heterogeneous data. Example,

  • a[0] = 10;(Integer type)
  • a[1] = 10.5;(Float type)
  • a[2] = "Nimal";(String type)

Object array is also a type of collection.

📌Problem with Array

  1. Fixed memory size when defining (Having limited memory space)

Arrays are not growable in nature.(Cannot increase or decrease the size of the array after defining)

  • If Object a[] = new Object[5]; , we can only store 5 objects. If we add the 6th data object, it will return an exception(ArrayIndexOutOfBoundException).
  • If we store only 3 objects in the above-defined array, the rest of the memory allocations will be wasted.

2. Only homogeneous data can be stored

3. While implementing, underlying data structure concepts haven’t been used. Therefore, any ready-made method is not available.

📌Why Collection?

To overcome the problems/limitations related to the array concept, we can use collections.

📌Collection Vs. Array

image by author

Hope now you know, why we need Java collection framework and the importance of using them. I think you like to sharpen your knowledge about the collection framework further. So, stay tuned guys…😋

--

--

LinkIT
LinkIT

Published in LinkIT

Publication for the Students at IT Faculty, University of Moratuwa

Responses (2)