If a class is used in a Java project in Eclipse, and it's not imported already, and there is only one class with that name, Eclipse will automatically import it.
If there are two or more classes with the same simple name, Eclipse will ask the user to select the desired one. I would like to avoid having to select one and instead prefer having a default class imported.
For example, I commonly use List
and ArrayList
. And each time I use them in a new class, I have to select java.util.ArrayList
and java.util.List
from the suggestions of Eclipse, because there is another class with the name List
(java.awt.List
).
Is there some way to set java.util.List
and java.util.ArrayList
as a default import if List
and ArrayList
is used?
For now, I created an Eclipse template that triggers on the word List
. Are there any other ideas or improvements?
${:import(java.util.List,java.util.ArrayList)} List<> list = new ArrayList<>();
In above situation, Eclipse comes with a nice feature called “Organize Imports” to imports all the classes that are used, automatically.
Try this: Go to Preferences » Java » Editor » Content Assist » Advanced. Make sure Other Java Proposals is ticked/checked. If that does not work simply close the project and reopen it.
Press: CTRL + SHIFT + O and you should see below dialog boxes. Choose your desired import package and click next. It will prompt you for your next import and thats it. You are done.
You can also use the "Type Filters" from the Eclipse Preferences menu. This allows you to 'forbid' java.awt.* for all of your projects rather than just one.
Java -> Appearance -> Type Filters (or search for "Type Filters")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With