我最近一直在学习HashMaps,但我有一个问题似乎无法得到明确的答案.主要区别 -
HashMap hash1 = new HashMap();
Run Code Online (Sandbox Code Playgroud)
VS
HashMap<,>hash1 = new HashMap <,> (); //Filled in with whatever Key and Value you want.
Run Code Online (Sandbox Code Playgroud)
我想当你定义一个HashMap时,它需要Key和Value.任何帮助将非常感激.谢谢.
And*_*ysh 20
这些是你有的选择:
J2SE <5.0风格:
Map map = new HashMap();
Run Code Online (Sandbox Code Playgroud)
J2SE 5.0+样式(使用泛型):
Map<KeyType, ValueType> map = new HashMap<KeyType, ValueType>();
Run Code Online (Sandbox Code Playgroud)
Google Guava风格(更短更灵活):
Map<KeyType, ValueType> map = Maps.newHashMap();
Run Code Online (Sandbox Code Playgroud)
Her*_*nis 11
你应该看看Java泛型,如果你没有指定HashMap的类型,key和value都将是Objecttype.
因此,如果您希望HashMap具有Integer键和String值,例如:
HashMap<Integer, String> hashMap= new HashMap<Integer, String>();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
66822 次 |
| 最近记录: |