logo image

Collections in Java



Java Collections Framework is one of the biggest strength of Java, Collections are used to store, retrieve, manipulate data. Java has tons of in-built high-performance collections implementations which can be used for wide variety of use-cases.
Following are the important collection interfaces

  • List Interface
  • Set Interface
  • Map Interface

And following are the most used implementations for those

List : ArrayList, LinkedList Set : HashSet, LinkedHashSet Map : HashMap, LinkedHashMap, TreeMap, ConcurrentHashMap

List

List is an ordered collection and it allows duplicate data. You can access elements of a list using the index.
Following example shows you how to create Lists & manipulate the data in Lists.

import java.util.ArrayList;
import java.util.List;

public class ListsExample {
	public static void main(String[] args) {

		// initializing List
		List<String> colors = new ArrayList<>();

		// adding elements to List
		colors.add("red");
		colors.add("green");
		colors.add("blue");

		// reading elements from List
		System.out.println("Color at 2nd index is: " + colors.get(2)); // output: Color at 2nd index is: blue

		// Iterating all elements from List
		for (String color : colors) {
			System.out.println(color);
		}

	}
}

Following Tutorial shows you different ways of iterating over a list https://onecompiler.com/posts/3sd56y78n/iterating-on-a-list-in-java-all-possible-ways

Set

Set is an un ordered collection of unique data, it does not allow duplicates. HashSet, LinkedHashSet are the major implementations of Set. HashSet does not guaranty the insertion order where as LinkedHashSet guaranty that. Following example shows you how to create Sets & manipulate the data in Sets.

import java.util.HashSet;
import java.util.Set;

public class SetsExample {
	public static void main(String[] args) {

		// initializing List
		Set<String> colors = new HashSet<>();

		// adding elements to List
		colors.add("red");
		colors.add("green");
		colors.add("green");
		colors.add("green");
		colors.add("blue");
		colors.add("blue");

		// Iterating all elements from List
		for (String color : colors) {
			System.out.println(color);
		}

	}
}

Map

Map maps keys to values. Map does not allow duplicate keys. Key-Value mapping is one to one. HashMap, LinkedHashMap, TreeMap and ConcurrentHashMap most commonly used implementations of Map.

HashMap - does not preserve insertion order LinkedHashMap - preserves insertion order TreeMap - sorts the data based on Key ConcurrentHashMap - Can be used in Multi threaded environment

Following code shows you how to declare a Map and read/write values into it.

import java.util.HashMap;
import java.util.Map;

public class MapsExample {
	public static void main(String[] args) {

		// declaring a Map
		Map<String, String> employeeIdVsName = new HashMap<>();

		// adding key-values to map
		employeeIdVsName.put("E01", "foo");
		employeeIdVsName.put("E02", "bar");
		employeeIdVsName.put("E03", "bar"); // Map can contain duplicate values, but not the keys

		// reading a value with Key
		System.out.println("Name of E03 is: " + employeeIdVsName.get("E03")); // output: Name of E03 is: bar

		// iterating over map
		employeeIdVsName.forEach((key, value) -> {
			System.out.println(key + ": " + value);  // output: E02: bar \n E01: foo \n E03: bar
		});
	}
}