0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

空の ArrayList を新規生成するラムダ式

Last updated at Posted at 2020-11-19

Want

ファクトリメソッドとして空のArrayListを新規作成する Lambda 式を渡したいとき、次のようなラムダを渡すと Sonarqube や IntelliJ 等に "Replace this lambda with a method reference."や"Lambda can be replaced with method reference." というように直接実装を書くんじゃなくてメソッド参照にしろと怒られる。

() -> new ArrayList()

Do

次を渡せばOK。

ArrayList::new

Don't

空のArrayListを用意したあとでそのインスタンスに要素を追加していく場合、次はアウト。

Collections.emptyList()

Why

Collections.emptyList() で生成したリストは、基本的に操作できません。
add メソッドなどを呼び出すと UnsupportedOperationException が発生します。

次も参考にどうぞ。
https://www.baeldung.com/java-collections-emptylist-new-list

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?