Why Android Studio imports the complete package on Reformatting the code

Aada :

I have a class which uses HashMap and I have imported import java.util.HashMap.

On executing reformat code -> eclipse shortcut used Cntrl+Shift+R

It reformats and convert to import java.util.*

Why android studio imports all classes if we need only one , here HashMap? Isn't it unnecessary.

Jeel Vankhede :

If any package is having more than N numbers of import of the same package in Android studio, it automatically converts it to * imports for that package (default are 5 for Top-level Symbols and 3 for Java Statics and Enum Members).

So, for example if you're having some N number of imports from package java.util, it gets converted to java.util.*.

Where to find that setting to change it?

  1. Open Settings from File-> Settings in Android Studio (ctrl+alt+s).

  2. Go to Editor -> Code Style -> Java/Kotlin and open imports tab then change that N number to any of your suitable case.

enter image description here

There are other options available as well.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=121181&siteId=1