I am using Multimap<String, List<String>>
in one of my API's. Now to get all the values means list of list I used the .values()
method of the multimap. But this method is returning me 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>>
.
How to cast or convert without building new arrayList and explicit add list values from collections to that 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());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.