[英]Casting an ImmutableList to List
盡管我的研究一直在進行,但看起來 Google Guava 的ImmutableList
實現了List
。 所以我可以將它轉換為List
,對嗎? 我知道如果使用List
的修改方法之一,它會拋出錯誤,但是這一切都會出錯嗎?
public List<Integer> getList() {
return ImmutableList.of(1,2,3);
}
如果它實現List,則無需將其強制轉換為List
。 您可以將其分配給List
變量,也可以將其傳遞給需要List
而不強制轉換的方法。
而且,是的,調用任何修改List
的方法都會引發異常,但是無論是否將其強制轉換為List
都會發生。
我看到一個更大的問題,我認為您不明白。 你可以不投一物降對象類型的對象(ImmutableList)實現。 List<E>
是ImmutableList
的接口。 您可以將其強制轉換為List<E>
類型,但是會丟失ImmutableList具有/提供的List沒有的所有內容。
您可以通過 toList() 將 ImmutableList 轉換為列表:
var immutableList= ImmutableList.Create<MyClass>();
List<MyClass> list = oldList.ToList();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.