[英]Java Collection: what to convert Collection<List<String>> to ArrayList<List<String>> or List<List<String>>?
我在我的API之一中使用Multimap<String, List<String>>
。 現在要獲取所有值,意味着列表列表,我使用了multimap的.values()
方法。 但是此方法返回Collection<List<String>>
。 現在要播放此集合的索引,我想將其轉換為List<List<String>>
或ArrayList<List<String>>
。
如何在不構建新的arrayList的情況下進行強制轉換或轉換,以及將集合中的顯式添加列表值添加到該arraylist。
您可以使用適當的構造函數:
List<List<String>> yourList = new ArrayList<>(yourCollection);
列表中元素的順序是集合的迭代器的順序。
如果您不想建立一個新集合(並且不要忘記,這可能不會很昂貴,因為您將創建新集合但不克隆實際成員),為什么不做呢?
int i = 0;
for (List<String> list : collection) {
// whatever
i++;
}
不太好,我很感激。
或使用Google Collection API
List<List<String>> = Lists.newArrayList(myMap.values());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.