Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse - auto import java.util classes on ambiguous imports

Is there a way Eclipse will auto import classes from java package as java.util.List without the need to choose it explicitly in every class?

(even without Ctrl + Shift + O)

When I write List and eclipse auto import java.util.List instead of suggesting irrelevant List as org.apache.xmlbeans.impl.xb.xsdschema.ListDocument.List

I know I can exclude by Type Filters, but I just want specific objects as List to be automatically imported.

like image 994
user7294900 Avatar asked Apr 08 '18 08:04

user7294900


People also ask

When using Eclipse whichever classes are needed for the present class can be imported automatically?

In above situation, Eclipse comes with a nice feature called “Organize Imports” to imports all the classes that are used, automatically.

How do I fix all imports in Eclipse?

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.

How to auto import java class in Eclipse?

Click Source —> Organize Imports menu item or Shift+Ctrl+O in Eclipse, then the java class will be imported in the java source file automatically. 2. Auto Import Java Class In All Java Project Files. If you have a lot of java source file need auto import used java classes, you can config your java project to achieve it.

How do I import a class into a single java file?

Auto Import Java Class In Single Java File. Input the class name in the java source file. Click Source —> Organize Imports menu item or Shift+Ctrl+O in Eclipse, then the java class will be imported in the java source file automatically.

How do I change the number of classes imported in Eclipse?

By default, Eclipse imports classes individually but you can tell it to always convert them into wildcards: Go to Window > Preferences > Java > Code Style > Organize Imports. Change the value of Number of imports need for .* to 0.

What happens if you have too many unused imports in Java?

Though the unused import in a Java file does not create any harm, it’s unnecessary to increase the length and size of a Java source file, and if you have too many unused imports in your Java source file, those yellow underlines and Eclipse warnings affect the readability of your code, at least it distracts you.


Video Answer


1 Answers

Automatically organise import statements whenever you save

  • Go to Window > Preferences > Java > Editor > Save Actions.
  • Select Perform the selected actions on save (off by default).
  • Ensure that Organize imports is selected (on by default).

Let Eclipse collapse imports in the same package a wildcard (.*) or always expand them

  • Go to Window > Preferences > Java > Code Style > Organize Imports.
  • Change the value of Number of imports need for .* to 0.
  • (Optional) Change the value of Number of static imports needed for .* to 0.

Exclude unwanted packages using Type Filters

  • Go to Window > Preferences > Java > Appearance > Type Filters.
  • Click Add… to add a package/class.
  • Enter java.awt.List (or java.awt.* if you don’t intend using any AWT classes).

some these other features

  • Folding: By default, Eclipse folds all import statements into one line so your class takes up less space on the screen. You can change this by going to Window > Preferences > Java > Editor > Folding and deselecting Imports.

  • Sorting: If you’re really particular about the order of packages, you can go to Window > Preferences > Java > Code Style > Organize Imports and define the order of the packages as you want Eclipse to order them. It’s not really worth the effort though so I’d skip it.

  • Compress/abbreviate package names in the Package Explorer (not in your class but in the view): You can display packages compressed (eg. o~.e~.swt) or abbreviated (eg. org.eclipse.swt.custom becomes {SWT}.custom). Go to Window > Preferences > Java > Appearance and define the settings there. The dialog has decent examples of how to do this.
like image 166
Pawan Maurya Avatar answered Nov 02 '22 11:11

Pawan Maurya