INSTACK 360

Java Collections Framework

A deep dive into Java's powerful Collections Framework. Master ArrayLists, HashSets, HashMaps, and Iterators.

Advanced 5 min read

The Collections Framework

The Collection in Java is a framework that provides an architecture to store and manipulate the group of objects. Java Collections can achieve all the operations that you perform on a data such as searching, sorting, insertion, manipulation, and deletion.

1. ArrayList

The ArrayList class is a resizable array, which can be found in the java.util package.

import java.util.ArrayList;

ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.get(0); // Volvo

2. HashSet

A HashSet is a collection of items where every item is unique.

import java.util.HashSet;

HashSet<Integer> numbers = new HashSet<Integer>();
numbers.add(4);
numbers.add(4); // Ignored

3. HashMap

A HashMap stores items in "key/value" pairs, and you can access them by an index of another type (e.g. a String).

import java.util.HashMap;

HashMap<String, String> capitalCities = new HashMap<String, String>();
capitalCities.put("England", "London");
System.out.println(capitalCities.get("England"));