42

I'm using Android Studio and I write this :

List<Button> buttons = new ArrayList<Button>();

I have this message :

Explicit type argument Button should be replaced by <>

I'm curious, why would it be better to use diamond instead ?

List<Button> buttons = new ArrayList<>();

EDIT :
I don't agree with the duplicate at all ! I saw that answer before and it compares explicit argument to no argument at all, whereas I compare explicit argument to implicit argument !

Dan Chaltiel
  • 7,811
  • 5
  • 47
  • 92

1 Answers1

51

It is less verbose, consider the following

Map<String,List<String>> map = new HashMap<String,List<String>>();

vs

Map<String,List<String>> map = new HashMap<>();

I think you would go for the second option

Sleiman Jneidi
  • 22,907
  • 14
  • 56
  • 77
  • 15
    So not any difference except "beautiful" ? I'll go for it ! – Dan Chaltiel Oct 15 '15 at 09:21
  • 2
    @DanChaltiel I guess you could also argue that it's easier to change later because you would need to perform the change on every usage of the explicit version. I mean e.g. changing from `String` to `Integer`. – lucidbrot Nov 18 '17 at 21:32