41

I have a bit of code:

class MyClass<RCM> 
  private List<RCM> allPreExistingConfigsForCodes() {
    if(this.allCodesForThisType.size() == 0)
       return new ArrayList<RCM>(0);

IntelliJ is telling me I should replace new ArrayList<RCM> with new ArrayList<> what would that mean?

George Mauer
  • 117,483
  • 131
  • 382
  • 612
  • related http://stackoverflow.com/questions/4166966/what-is-the-point-of-the-diamond-operator-in-java-7 – vikramvi Jul 22 '16 at 14:07

2 Answers2

52

From the Java Tutorials generics lesson:

In Java SE 7 and later, you can replace the type arguments required to invoke the constructor of a generic class with an empty set of type arguments (<>) as long as the compiler can determine, or infer, the type arguments from the context. This pair of angle brackets, <>, is informally called the diamond. For example, you can create an instance of Box<Integer> with the following statement:

Box<Integer> integerBox = new Box<>();

Pang
  • 9,564
  • 146
  • 81
  • 122
Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
11

Are you using Java 7? If so, it is trying to take advantage of the new "diamond notation."

http://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html#type-inference-instantiation

Pang
  • 9,564
  • 146
  • 81
  • 122
Jeff Storey
  • 56,312
  • 72
  • 233
  • 406