[英]Java Collection: what to convert Collection<List<String>> to ArrayList<List<String>> or List<List<String>>?
I am using Multimap<String, List<String>>
in one of my API's. 我在我的API之一中使用Multimap<String, List<String>>
。 Now to get all the values means list of list I used the .values()
method of the multimap. 现在要获取所有值,意味着列表列表,我使用了multimap的.values()
方法。 But this method is returning me Collection<List<String>>
. 但是此方法返回Collection<List<String>>
。 Now to play on the index on this collection I want to convert it into List<List<String>>
or ArrayList<List<String>>
. 现在要播放此集合的索引,我想将其转换为List<List<String>>
或ArrayList<List<String>>
。
How to cast or convert without building new arrayList and explicit add list values from collections to that arraylist. 如何在不构建新的arrayList的情况下进行强制转换或转换,以及将集合中的显式添加列表值添加到该arraylist。
You can use the appropriate constructor: 您可以使用适当的构造函数:
List<List<String>> yourList = new ArrayList<>(yourCollection);
The order of the elements in the list is the order of the iterator of the collection. 列表中元素的顺序是集合的迭代器的顺序。
If you don't want to build a new collection (and don't forget, that may not be costly since you'll create the new collection but not clone the actual members), why not just do 如果您不想建立一个新集合(并且不要忘记,这可能不会很昂贵,因为您将创建新集合但不克隆实际成员),为什么不做呢?
int i = 0;
for (List<String> list : collection) {
// whatever
i++;
}
Not hugely nice, I appreciate. 不太好,我很感激。
或使用Google Collection API
List<List<String>> = Lists.newArrayList(myMap.values());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.