Java, Collection constructor

前端 未结 3 1890
青春惊慌失措
青春惊慌失措 2021-01-25 15:00

is there any difference between :

TreeMap myMap = new TreeMap<>();

and

TreeMap

        
3条回答
  •  囚心锁ツ
    2021-01-25 15:43

    They are the same in java 7 where the diamond operator <> was introduced. In older versions of java the diamond operator will not work.

    The diamond operator brings type inference to constructors. Type inference on generic methods is available in java 5 and higher. Prior to java 7, to create a generic class using the compiler's type inference you had to use generic factory methods like static Map createMap().

提交回复
热议问题