Stop Using Dirty Diamonds in Java

Jorvon Marchel Carter
1 min readJun 4, 2021

Fellow Java developers, I’m tired of seeing generics like this.

List<String> strings = new ArrayList<String>();

You see that “<String>” in the constructor for the ArrayList? Yeah, that’s a dirty diamond. As of Java 7, dirty diamonds are not necessary. You should use clear diamonds, like this.

List<String> strings = new ArrayList<>();

As long as the parameterized type is included within the declaration, then the Java compiler will know how to parameterize the constructor. In this case, because List is parameterized with as a String, the compiler will know that the ArrayList will contain Strings.

This also applies to methods.

Foo<T> bar(){   //Some code that processes a bazz and something   return new Foo<T>(bazz, something);
}

Another dirty diamond! The compiler knows that the method will return a Foo<T>, so you don’t need to specify the type in the return statement.

Foo<T> bar(){//Some code that processes a bazz and somethingreturn new Foo<>(bazz, something);
}

There, a clear diamond.

Java is already verbose. Let’s not make it worse.

--

--

Jorvon Marchel Carter

I write as best as I can though I’m not an educated man. I’ve never climbed Mount Parnasso, no, nor read a Marcus Tullius Cithero.