简体   繁体   中英

Autocomplete with generic types in concrete class of implementation

When coding java in Ecipse (Kepler), I am having a problem when I am making a new variable of a generic interface type and I autocomplete its concrete implementation when initializing the variable. I'm talking about generic interfaces/concrete implementations like List/ArrayList and Map/HashMap.

Example: you type this into your IDE:

List<String> stringList = new ArrayL

Then, you use autocomplete (ctrl+space) to fill the code with ArrayList() , but then this is what the IDE puts into the code:

List<String> stringList = ArrayList<>()

So they are completely ignoring the String generic type parameterization. Any ideas on how I can get my Eclipse IDE to detect the generic type parameter in the variable declaration and place it into the type parameter of the implementation's constructor? This used to work for me automatically in Eclipse, but suddenly stopped working over the last couple of months. I am not sure what configuration change I did to my workspace to cause this to happen...besides upgrading from Eclipse Juno to Eclipse Kepler.

Eclipse would ignore the generic type if it auto-completed with new ArrayList() , which is an instance of the raw type.

But it auto-completes with new ArrayList<>() , which uses the diamond operator . This operator exists since Java 7 and is equivalent to new ArrayList<String>() , but without the redundancy.

So, in short, Eclipse does the right thing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM