You can't use primitive types as generic arguments in Java. Use instead:
Map<String, Integer> myMap = new HashMap<String, Integer>();
With auto-boxing/unboxing there is little difference in the code. Auto-boxing means you can write:
myMap.put("foo", 3);
instead of:
myMap.put("foo", new Integer(3));
Auto-boxing means the first version is implicitly converted to the second. Auto-unboxing means you can write:
int i = myMap.get("foo");
instead of:
int i = myMap.get("foo").intValue();
The implicit call to intValue() means if the key isn't found it will generate a NullPointerException, for example:
int i = myMap.get("bar"); // NullPointerException
The reason is type erasure. Unlike, say, in C# generic types aren't retained at runtime. They are just "syntactic sugar" for explicit casting to save you doing this:
Integer i = (Integer)myMap.get("foo");
To give you an example, this code is perfectly legal:
Map<String, Integer> myMap = new HashMap<String, Integer>();
Map<Integer, String> map2 = (Map<Integer, String>)myMap;
map2.put(3, "foo");
另外,附上一篇关于Java generics的文章,它的内容以及附录的参考资料,有帮助。
相关推荐
generic classes,所以製造元素時必須使用泛型語 法(角括號)。請與圖 2b比較。 圖 6和圖 7的泛型語法自 JDK1.3+GJ以來不曾改變過。它迥異於 C++,後者要求 程式必須在 class名稱前加上語彙單元 template,...
Methods, arguments, and return values ................. 48 The argument list ......................... 49 Building a Java program ...... 50 Name visibility ............................. 50 Using other...
更多公共类型generic typing问题 - 77 - 08.Play.libs库包 - 78 - 用XPath解析XML - 78 - Web Service client - 79 - Functional programming with Java功能扩展? - 79 - Option, Some<T> and None<T> - 80 - Tuple...
更多公共类型generic typing问题 - 77 - 08.Play.libs库包 - 78 - 用XPath解析XML - 78 - Web Service client - 79 - Functional programming with Java功能扩展? - 79 - Option, Some<T> and None<T> - 80 - Tuple...
2.8 Default and Named Arguments L1 26 2.9 Variable Arguments L1 26 2.10 Procedures 28 2.11 Lazy Values L1 28 2.12 Exceptions 29 Exercises 31 3 WORKING WITH ARRAYS A1 35 3.1 Fixed-Length Arrays 35 3.2 ...
1.8 C++ Compared with Java....... 17 Summary.......... 20 Review Questions........ . 21 Exercises.......... 22 2 Native Types and Statements 25 2.1 Program Elements........ . 26 2.1.1 Comments..........
1 泛型(Generic) 2 增强循环(Enhanced for Loop) 3 可变参数(Variable Arguments) 4 自动实现装箱和解箱操作(Boxing/Unboxing Conversions) 5 静态导入(Static Imports) 6 枚举类(Enumeration Classes) 7 元数据...
23.7. Accessing Application Arguments 23.8. Using the ApplicationRunner or CommandLineRunner 23.9. Application Exit 23.10. Admin Features 24. Externalized Configuration 24.1. Configuring Random Values...
Python参考手册,官方正式版参考手册,chm版。以下摘取部分内容:Navigation index modules | next | Python » 3.6.5 Documentation » Python Documentation contents What’s New in Python ...
PyTree: A Generic Tree Object Viewer Chapter 18. Text and Language Section 18.1. "See Jack Hack. Hack, Jack, Hack" Section 18.2. Strategies for Parsing Text in Python Section 18.3. String ...
10.3 GENERIC CLASSES 320CONTENTS xx 10.4 ARRAYS 325 10.5 THE COST OF GENERICITY 328 10.6 DISCUSSION: NOT DONE YET 329 10.7 KEY CONCEPTS INTRODUCED IN THIS CHAPTER 329 10.8 BIBLIOGRAPHICAL NOTES 330 ...