Different Ways of Creating HashMaps

前端 未结 4 1391
盖世英雄少女心
盖世英雄少女心 2021-01-04 03:29

I\'ve been learning about HashMaps recently but I have one question that I can\'t seem to get a clear answer on. The main difference between -

HashMap hash1          


        
4条回答
  •  再見小時候
    2021-01-04 03:37

    Specifying the key and the value types allows for greater type-safety by enabling compile-time typing enforcement.

    This makes it easier to write code that doesn't accidentally mix up the key and value types, and reduces the amount of casts that you must explicitly declare in the code.

    However, it is important to be aware the these type-checks are compile-time only, i.e. once the application is running, the JVM will allow you to use any types for the keys and values.

提交回复
热议问题