Autocomplete for generic types in Eclipse

“Access objects by their interfaces” is good practice, as stated in Effective Java. So for example, I prefer

List<String> al = new ArrayList<String>(); 

above

 ArrayList<String> al = new ArrayList<String>(); 

in my code. One of the nasty things is that if I type ArrayList<String> al = new and then press Ctrl + Space in Eclipse, I get ArrayList<String>() as propostal. But if I type List al = new and then press Ctrl + Space, I get only propostal to define an anonymous inner class, but not propositions like new ArrayList<String>() , which is 99% more, or, for example, new Vector<String>() .

Question : Is there a way to get subclasses as propositions for generic types?

+8
java generics eclipse autocomplete effective-java
source share
3 answers

I would suggest just writing the expression first, and then press Ctrl+2, L Then you can name the variable, and then Enter, Down, Enter . Done.

+9
source share

Depending on your personal style of writing such code, this is an alternative inspired by @Ben Schulz's answer. If you usually write an appointment first, like

 al = new ArrayList<String>(); 

then you can use Ctrl+1 for quick fix "Create a local variable". Pressing Tab will now directly open the type selection in which you select the List type.

+2
source share

JDK 1.7 does not need to specify a generic type to the right of the equal. Preference -> Java -> Compiler up to 1.6

+2
source share

All Articles