报告对最多包含一个参数的 Arrays.asList() 的调用。

在 JDK 9 及更高版本中,这样的调用可以替换为 Collections.singletonList()Collections.emptyList()List.of(),从而节省内存。

特别是,无参数的 Collections.emptyList()List.of() 总是返回共享实例,而无参数的 Arrays.asList() 每次调用时都会创建一个新对象。

注意: Collections.singletonList()List.of() 返回的列表不可变,而列表返回的 Arrays.asList() 允许调用 set() 方法。 这在极少数情况下可能会破坏代码。

示例:

  List<String> empty = Arrays.asList();
  List<String> one = Arrays.asList("one");

在应用快速修复后:

  List<String> empty = Collections.emptyList();
  List<String> one = Collections.singletonList("one");