简体   繁体   English

java类中的导入顺序是什么以及如何解决它

[英]what is Imports Order in java class and how to resolve it

In my java class there are multiple imports are there like showen below.在我的 java 类中有多个导入,如下所示。

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

my question is sonar report is telling minor violation under category of Import Order .我的问题是声纳报告在进口订单类别下显示轻微违规。

Wrong order for 'java.util.ArrayList' import. 'java.util.ArrayList' 导入顺序错误。

what exactly it means and how to resolve this sonar violations.它究竟意味着什么以及如何解决这种声纳违规问题。

Thanks in advance.提前致谢。

There is nothing wrong with the import java.util.ArrayList in isolation—it just happens to be the first one that breaks the rule.孤立地import java.util.ArrayList没有任何问题——它恰好是第一个违反规则的。 The full answer to your question will require all of your imports to be shown.您的问题的完整答案将需要显示您的所有导入。 The most probable cause is import group ordering.最可能的原因是导入组排序。

As I've learned from my experience, classes within one package must be sorted alphabetically.正如我从我的经验中了解到的,一个包中的类必须按字母顺序排序。 So look at classes located before ArrayList and refer to java.util package, if they are any.所以查看位于 ArrayList 之前的类并参考 java.util 包,如果有的话。 In my case it was to be as follows:就我而言,情况如下:

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

I know I'm late to the party here but this could help others.我知道我在这里参加聚会迟到了,但这可以帮助其他人。

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

The above does not break the rule as it is well sorted alphabetically, you get the "Wrong order for 'java.util.'上面没有违反规则,因为它按字母顺序排列得很好,你会得到“'java.util.' 的错误顺序。 import."进口。” when any of these imports are in between other imports without the proper sort order.当这些导入中的任何一个在没有正确排序顺序的其他导入之间时。 For instance:例如:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

The above break the rule as the imports are not well sorted.以上违反了规则,因为进口没有很好地排序。 To fix this, just move the "java.util" imports above the "org.slf4j" imports.要解决此问题,只需将“java.util”导入移动到“org.slf4j”导入上方。 Kind of like:有一些像:

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Also, there seems to be an order to how imports should be arranged apart from alphabetical sorting.此外,除了按字母顺序排序外,似乎还有一个关于如何排列导入的顺序。

  1. Static imports静态导入
  2. imports from other packages in the same project从同一项目中的其他包导入
  3. Imports from java从 Java 导入
  4. Imports from the framework从框架导入
  5. All other imports所有其他进口

If you are using eclipse then try Ctrl+Shift+O to fix imports.如果您使用的是 eclipse,请尝试Ctrl+Shift+O来修复导入。

Also see this question .另请参阅此问题

The order of the import statements does not matter to the compiler or JVM. import 语句的顺序与编译器或 JVM 无关。 It's just a coding style preference.这只是一种编码风格偏好。 Personally, I like to use import some.package.* if I am using more than 3 classes for a particular package.就个人而言,如果我为特定包使用 3 个以上的类,我喜欢使用import some.package.*

将 ArrayList 导入放在 List 导入之后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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