I've see that since java 1.5 or maybe a later version, you can initialize java collection leaving generics blank, i.e. using <> instead of writing out the whole < A,B >. but I can't find the official documents on this, and I'm wondering whether this has any benefits (or maybe I'm not remembering this correctly, in which case do point out the correct form). Thank you.!
Asked
Active
Viewed 121 times
2 Answers
3
It's called the diamond operator. It was introduced in Java 1.7.
The benefit is just that you need to write less code.
Compare
List<Map<String, Integer>> list = new ArrayList<Map<String, Integer>>();
to
List<Map<String, Integer>> list = new ArrayList<>();

Paul Boddington
- 37,127
- 10
- 65
- 116
-
Note: This is/was a lazy specialized syntax shortcut that didn't require adding type inference. (Type inference works in the other direction.) – user2864740 Oct 04 '15 at 23:11
-
Thanks for the link. And what about using List instead of explicitly using ArrayList during declaration? – DanSoren Oct 04 '15 at 23:11
-
@DanSoren Search for 'programming to interfaces' – user2864740 Oct 04 '15 at 23:12
-
@DanSoren http://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface – Paul Boddington Oct 04 '15 at 23:13
-
In short, the diamond operator allows you to write [DRY](https://en.wikipedia.org/wiki/Don't_repeat_yourself) code. – Mick Mnemonic Oct 04 '15 at 23:20
0
What you're looking for is called the diamond operator. It was introduced in Java 7.

Ken Wayne VanderLinde
- 18,915
- 3
- 47
- 72