What is the diamond operator in Java? [duplicate]

不羁的心 提交于 2019-11-26 14:41:27
Suresh Atta

Don't worry. It's not an evil. It's feature of Java 7.

The purpose of the diamond operator is to simplify instantiation of generic classes.

For example, instead of

List<Map<Integer,Set<String>>> p = new ArrayList<Map<Integer,Set<String>>>();

with the diamond operator we can write only

List<Map<Integer,Set<String>>> p = new ArrayList<>();

If you want to know more about it and want to use it, please have a quick look here and decide whether it's useful to you or not.

prasanth

The diamond operator is used to specify what type of data you are going to use in the Collections.

For example, ArrayList<String> list = new ArrayList<String>().

In Java 7, we can eliminate the type like:

ArrayList<String> list = new ArrayList<>()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!