top of page
sathyaguruswamyp

Java Collections Framework for Beginners | Realtime examples



Collections framework is one of the most valuable and exciting topics in the Java language.

So why do you think we need Java collections? The Java collection framework provides the developers with access to prepackaged data structures as well as algorithms to manipulate data.




This Java collections framework blog will cover the following topics in depth.

  • What is Collections Framework in Java?

  • Realtime examples of Collection

  • Difference between Array and Collection in Java

  • Advantages Of Java Collections Framework

  • Java collection framework Hierarchy

  • Java Collections: Interfaces


What is Collections Framework in Java?


Java collections can be defined as a group of objects. A collection is usually a single unit consisting of more than one object. You can perform all operations on data such as searching, sorting, insertion, manipulation, deletion, etc. by Java collections.


A framework is software that has a ready-made functional interface or architecture and also contains a set of classes and interfaces to be used with the provided interface.


Let’s understand it with some real-time examples.

Realtime examples of Collection




1. In childhood, you had a kiddy bank. In the kiddy bank, you had collected a lot of coins. This kiddy bank is called collection and the coins are nothing but objects.

2. During school time, you put all the important books in the school bag before going to school. Here Schoolbag is a collection and books are objects.

3. A classroom is a collection of students. So, the classroom is nothing but a collection and students are objects.

4. We know that the entire world is a collection of humans, animals, and different things. The world is a collection and humans, animals and different things are different objects.

Technically, a collection is an object or container which stores a group of other objects as a single unit or single entity. Therefore, it is also known as a container object or collection object in java.

A container object means it contains other objects. In simple words, a collection is a container that stores multiple elements together.

JVM (Java Virtual Machine) stores the reference of other objects into a collection object. It never stores physical copies of other objects because other objects are already available in the memory and storing another copy of objects into a collection object would be a wasting of memory.

A group of objects stored in a collection object is shown in the below figure. In the figure, we are storing 5 objects in a collection object.

A simple example of a collection is given below:


Difference between Arrays & Collections in Java

Key

Array

​ Collections

Size

​Arrays are fixed in size

The Collection is growable in nature.

Memory Consumption

Arrays consume more memory

Collections consume less memory

Data type

An Array can hold only homogeneous type elements

Collection can hold homogeneous and heterogeneous type elements

Data Structure

No underlying data structure is available for arrays

​Every collection class is implemented based on some standard data structure.

Primitives storage

Arrays can hold both object and primitive elements

Collection can hold only object types but primitive elements


Advantages Of Java Collections Framework

  • Reduced programming efforts.

  • Consistent methods and API.

  • Increase speed and accuracy.

  • Facilitates software reuse.

  • Interoperability among unrelated APIs.

  • Fewer efforts to design new APIs.

Java collection framework Hierarchy


The hierarchy of the entire collection framework consists of four core interfaces such as Collection, List, Set, Map, and two specialized interfaces named SortedSet and SortedMap for sorting.

All the interfaces and classes for the collection framework are located in java. util package. The diagram of the Java collection hierarchy is shown in the below figure.


These are the 5 key interfaces of the collection framework which we can use according to our use.

  1. Collection

  2. List

  3. Set

  4. Queue

  5. Map


Note:

Although the java Map interface is a Java Collection framework. It doesn’t inherit from the Java Collection framework. Instead, it has its own interface hierarchy that maintains key-value pairs.

Java Collections: Interface


Iterator interface: The Iterator is an interface that iterates the elements. It is used to traverse the list and modify the elements. Iterator interface has three methods which are mentioned below:

  1. public boolean hasNext() – This method returns true if the iterator has more elements.

  2. public Object next() – It returns the element and moves the cursor pointer to the next element.

  3. public void remove() – This method removes the last elements returned by the iterator.

There are three components that extend the collection interface i.e List, Queue and Sets. Let’s learn about them in detail:

List

Set

Queue

Insertion order is maintained

Insertion order is not maintained in a Hash set

Insertion order is maintained

Can contain duplicate elements

Cannot contain duplicate elements

Can contain duplicate elements.

Insertion and removal of an array can be done for any index.

Remove the specified element.


Only the last inserted element can be popped out. Also, the insertion of elements happens at the end.

General syntax.

List<String> list = new ArryList<String>();


General syntax.

HashSet<String> set = new HashSet<String>();


General syntax.

Queue<String> queue = new PriorityQueue<String>();


Example 1: A simple example of List Interface using ArrayList


import java.util.*; public class ListInterface{ public static void main(String args[]){ //Creating a List List<String> list=new ArrayList<String>(); //Adding elements in the List list.add("Ford"); list.add("Tata"); list.add("Mahindra"); list.add("Suzuki"); list.add("Honda"); //Iterating the List element using for-each loop for(String Vehicle:list) { System.out.println(Vehicle); } }

}


Output

 Ford 
 Tata  
 Mahindra 
 Suzuki
 Honda

Example 2: A simple example of Set Interface using HashSet

import java.util.HashSet;

import java.util.Set;


public class SetInterface {

public static void main(String[] args) {

//creating set

Set<String> set = new HashSet<String>();

//Adding elements in the Set

set.add("Ford");

set.add("Tata");

set.add("Mahindra");

set.add("Suzuki");

set.add("Honda");


//Iterating the Set element using for-each loop

for(String vehicle:set) {

System.out.println(vehicle);


}

}

}


Output

 Mahindra
 Suzuki
 Ford
 Tata
 Honda

MAP Interface

  • The Map interface is not the part of the collection framework

  • Objects are stored in form of key and value pairs known as entry

  • A duplicate key is not allowed but the value can be duplicated

  • Heterogeneous type of objects is allowed

  • Insertion order is not preserved




  • The important implementations of Map are HashMap, Treemap, LinkedHashMap, and HashTable.

Difference between HashMap, Treemap, LinkedHashMap, and HashTable:

HashMap

TreeMap

LinkedHashmap

HashTable

Null keys and values are allowed

Key cannot be null, values can be null

Null keys and values are allowed.

It won’t allow null keys and values.


Not synchronized


Not synchronized


Not synchronized


Synchronized

There is no guarantee to maintain order in the iteration

Sorting will be done based on the natural order.

An Insertion order will be maintained


Insertion order not maintained.


Example: Basic program of map interface in Java


import java.util.HashMap;

class Main {

public static void main(String[] args) {

// create a hashmap

HashMap<String, Integer> languages = new HashMap<>();

// add elements to hashmap

languages.put("Java", 8);

languages.put("C", 11);

languages.put("C++", 9);

languages.put("Python", 5);

System.out.println("HashMap elements : " + languages);

}

}

Output

 HashMap elements : {Java=8, C++=9, C=11, Python=5}


Frequently Asked Questions


#1) What is the use of a collection framework in Java?

Answer: The collection framework offers prepackaged algorithms, interfaces, and classes that allow programmers to write highly efficient programs that can store and process data.

#2) What is Java Collections API in Java?

Answer: The Java collections API provides interfaces and classes that can be extended and implemented in order to use data structures that are nothing but collections.


Conclusion


This blog gave us an introduction to the Java Collections Framework.

I hope this blog explained most of the topics in the Java collections framework, its hierarchy, interface, list, queue, and sets that I have discussed above. Please share your opinion with comments.





Recent Posts

See All
bottom of page