1
0

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 5 years have passed since last update.

Java8で イミュータブル(変更不可)なListオブジェクトの変換関数

Last updated at Posted at 2019-06-30

なんかで使ったから、備忘録がわりに。

※ Java8 での実装。
➡︎Java9 以降では、もうちょっと短い書き方(java.util.List#of)があるっぽい。


@SafeVarargs
public static <E> List<E> toConstList(E... data) {
    int length = (data == null) ? 0 : data.length;

    switch (length) {
    case 0:
        return Collections.emptyList();
    case 1:
        return Collections.singletonList(data[0]);
    default:
        return Collections.unmodifiableList(Arrays.asList(data));
    }
}
1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?