3

Google の ImmutableList クラスを使用していますhttp://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/collect/ImmutableList.html

既存の ImmutableList と追加の要素 (同じ型) を取り、古い要素と新しい要素を含む新しいImmutableList を返すメソッドを探しています。

おそらく、ドキュメントに明らかな何かが欠けていますか?

4

2 に答える 2

6
public static final ImmutableList<Foo> result
   = new ImmutableList.Builder<Foo>()
       .addAll(originalList)
       .add(new Foo())
       .build();
于 2015-03-04T12:01:57.567 に答える
2

次の方法で実行できます。

ImmutableList<YourType> newList = ImmutableList.copyOf(
    Iterables.concat(existingList, ImmutableList.of(newElement))
);

編集: @JB Nizetsソリューションはより読みやすく見えます:)

于 2015-03-04T12:02:54.570 に答える