[TUT] Easier / Quicker Generic Collection Creation

Sick of repeating your generics declaration?Sick of repeating your generics declaration?

List<SomeObject> listOfSomething = new ArrayList<SomeObject>();
Map<SomeKey, SomeObject> mapOfSomething = new HashMap<SomeKey, SomeObject>();

For a quick and easy way to create a collection of objects you can do the below.

Create yourself a util class:

package com.blundell.util;

import java.util.ArrayList;
import java.util.HashMap;

public class Collections {

	public static <E> ArrayList<E> newArrayList() {
		return new ArrayList<E>();

	public static <K, V> HashMap<K, V> newHashMap() {
		return new HashMap<K, V>();


Then each time you only need to declare your generics once.

List<SomeObject> listOfSomething = Collections.newArrayList();
Map<SomeKey, SomeObject> mapOfSomething = Collections.newHashMap();

This is due to Type Inference. Inspired by Joshua Bloch’s – Effective Java (second edition)


One thought on “[TUT] Easier / Quicker Generic Collection Creation

Leave a Reply

Your email address will not be published. Required fields are marked *