`

在Java中使用generic arguments时的基本问题

阅读更多

      问题怎么来的?

      在编码时,声明一个HashMap类型的变量时,在generic arguments部分将Integer写为了int,像下面这样:

HashMap<int, String> var;

      未等编译,IDE已经给出了提示,有问题。


      问题原因:Java中,在generic arguments不能使用基本数据类型,比如int等。


      对这个问题的回答,可以看看下面这个web:

      http://stackoverflow.com/questions/1780385/java-hashmapstring-int-not-working

      其中,回答者cletus,还就auto-boxing/unboxing进行说明了。下面是其回答的原文:

         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的文章,它的内容以及附录的参考资料,有帮助。

      多角度看 Java 中的泛型

分享到:
评论

相关推荐

    JDK 1.5的泛型實現(Generics in JDK 1.5)

    generic classes,所以製造元素時必須使用泛型語 法(角括號)。請與圖 2b比較。 圖 6和圖 7的泛型語法自 JDK1.3+GJ以來不曾改變過。它迥異於 C++,後者要求 程式必須在 class名稱前加上語彙單元 template,...

    Thinking in Java 4th Edition

    Methods, arguments, and return values ................. 48 The argument list ......................... 49 Building a Java program ...... 50 Name visibility ............................. 50 Using other...

    play framework 框架手册 word 版

    更多公共类型generic typing问题 - 77 - 08.Play.libs库包 - 78 - 用XPath解析XML - 78 - Web Service client - 79 - Functional programming with Java功能扩展? - 79 - Option, Some&lt;T&gt; and None&lt;T&gt; - 80 - Tuple...

    play框架手册

    更多公共类型generic typing问题 - 77 - 08.Play.libs库包 - 78 - 用XPath解析XML - 78 - Web Service client - 79 - Functional programming with Java功能扩展? - 79 - Option, Some&lt;T&gt; and None&lt;T&gt; - 80 - Tuple...

    Scala for the Impatient 2nd (完整英文第二版 带书签)

    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 ...

    Addison.Wesley.C++.by.Dissection.2002.pdf

    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..........

    JDK5.0的11个主要新特征.doc

    1 泛型(Generic) 2 增强循环(Enhanced for Loop) 3 可变参数(Variable Arguments) 4 自动实现装箱和解箱操作(Boxing/Unboxing Conversions) 5 静态导入(Static Imports) 6 枚举类(Enumeration Classes) 7 元数据...

    spring-boot-reference.pdf

    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...

    python3.6.5参考手册 chm

    Python参考手册,官方正式版参考手册,chm版。以下摘取部分内容:Navigation index modules | next | Python » 3.6.5 Documentation » Python Documentation contents What’s New in Python ...

    Python程序设计(第二版).chm

    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 ...

    Object-Oriented Software Construction 2nd

    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 ...

Global site tag (gtag.js) - Google Analytics