Home  >  Q&A  >  body text

java - HashSet<int> 为何有错误?

代码里用了HashSet<int>
结果出错,说

 Syntax error, insert "Dimensions" to complete ReferenceType
 

改成HashSet<Integer>才OK
为何?

怪我咯怪我咯2712 days ago640

reply all(4)I'll reply

  • PHP中文网

    PHP中文网2017-04-18 10:52:54

    The type parameters in generics must be a subclass of Object, so int cannot be used, but the wrapper type Integer of int should be used.

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-18 10:52:54

    int is a basic data type, so it won’t work; you should use its corresponding packaging class, Interger

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 10:52:54

    Simply put, because int is a basic type and does not contain the methods required in the collection framework. Take this as an example. Even hashCode is not implemented. How to calculate the hash value? So you need to use Integer.

    The types in generics must be subclasses of Object.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:52:54

    int is the data type, and Integer is the packaging class. What should be placed in generics is the object type

    reply
    0
  • Cancelreply