As of Java 7 we can take out the type arguments required to instantiate a generic class as long as the compiler can infer the type arguments from the context. Many developers name the remaining pair of angle brackets as ‘diamond’.

package com.abelski.samples;

import java.util.LinkedList;
import java.util.List;

public class GenericsTypeInference
{
	public static void main(String[] args)
	{
		List<String> list = new LinkedList<>();
		list.add("david");
		list.add("mike");
		list.add("james");
		System.out.println(list);
	}

}

The following video clip shows the execution of this code sample and overviews it.

Leave a Reply

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