Jun 23, 2009

A Generic Way To Create a Collection

How many times do you find yourself creating this code over and over again in tests or even production code?
List<String> stringList = new ArrayList<String>();
stringList.add("some test string");
stringList.add("some other test string");

Rather than writing this same code over and over again, there is an easier way to create a simple generic list of objects in one method. The following method makes use of two Java 5 features: variable list arguments and generics. Here’s what it looks like:
public static <E> List<E> createArrayList(final E... values) {
   final List<E> list = new ArrayList<E>(values.length);
   for (final E value : values) {
   return list;

Let me break down what’s happening here.

public static <E> List<E>
this creates a static method and declaring E as a generic type that this method will use and returns a list of that type.

createArrayList(final E... values)
Here a variable length argument array of any type is passed in and “E” is typed to what is passed in. For example if String were passed in, E would be then typed to an array of String, or if BigDecimal were passed in, E would be typed to an array of BigDecimal (The JVM treats variable length arguments as arrays).   Anywhere there is an E, this gets typed to whatever is passed in.

In the rest of the method, an ArrayList of E is created and then the array is looped over and each type is added to the list and the list is then returned.  Here’s the same code as above using our new method:

aMethodThatTakesAListOfStrings(createArrayList("some test string", "some other test string"));

Much nicer isn’t it? That can be used with any object too such as:

aMethodThatTakesAListOfIntegers(createArrayList(1, 2, 3));

The same can be done with any other collection type such as Set, SortedSet, etc. and makes testing and writing code much eaiser.

About the Author

Object Partners profile.

One thought on “A Generic Way To Create a Collection

  1. George says:

    If you don’t mind that the list is read-only (for example parameters to a function) you can use the built in java.util.Arrays class:

    List stringList = Arrays.asList(“s1”, “s2”, “s3”);
    List intList = Arrays.asList(1, 2, 3, 4, 5, 6);

    If you use this a lot you can statically import the ‘asList’ method to clean up your code

Leave a Reply

Your email address will not be published.

Related Blog Posts
Building Better Data Visualization Experiences: Part 1 of 2
Through direct experience with data scientists, business analysts, lab technicians, as well as other UX professionals, I have found that we need a better understanding of the people who will be using our data visualization products in order to build them. Creating a product utilizing data with the goal of providing insight is fundamentally different from a typical user-centric web experience, although traditional UX process methods can help.
Kafka Schema Evolution With Java Spring Boot and Protobuf
In this blog I will be demonstrating Kafka schema evolution with Java, Spring Boot and Protobuf.  This app is for tutorial purposes, so there will be instances where a refactor could happen. I tried to […]
Redis Bitmaps: Storing state in small places
Redis is a popular open source in-memory data store that supports all kinds of abstract data structures. In this post and in an accompanying example Java project, I am going to explore two great use […]
Let’s build a WordPress & Kernel updated AMI with Packer
First, let’s start with What is an AMI? An Amazon Machine Image (AMI) is a master image for the creation of virtual servers in an AWS environment. The machine images are like templates that are configured with […]