Different Ways of Creating HashMaps

前端 未结 4 1425
盖世英雄少女心
盖世英雄少女心 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:38

    - Generics can be implied to classes, interfaces, methods, variables etc.. but the most important reason for which its used is making the Collection more type safe.

    - Generics make sure that only specific type of object enters and comes out of the Collections.

    - Moreover its worth mentioning that there is a process known as Erasure,

    -> Erasure is a process where the type parameters and type arguments are removed from the generic classes and interfaces by the compiler, making it back compatible with the codes that where written without Generics.

    So,

    HashMap map = new HashMap();

    becomes of Raw type,

    HashMap map = new HashMap();

提交回复
热议问题