java - List<List<model>>如何更快捷的取里面的model?
問題描述
訪問接口返回數(shù)據(jù)類型為List<List<model>>,現(xiàn)在想將其中的model插入數(shù)據(jù)庫,感覺一點點循環(huán)有點傻,0.0...,各位有沒有其他的方法?
問題解答
回答1:C#的話:
var flat = list.SelectMany(l=>l).ToList();
Java的話:
List<model> flat = list.stream().flatMap(List::stream).collect(Collectors.toList());回答2:
list.stream().flatMap(model-> model.stream()).forEach(System.out::println);
回答3:數(shù)據(jù)結(jié)構(gòu)使然,循環(huán)吧
回答4:public static IEnumerable<T> GetItems<T>(this List<List<T>> list){ foreach (var child in list) {foreach (var item in child){ yield return item;} }}public static IEnumerable<T> GetNestItems<T>(this System.Collections.IList list){ Type type = null; foreach (var item in list) {if (type == null) type = item.GetType();if (type == typeof(T)){ yield return (T)item;}else if (type.GetGenericTypeDefinition() == typeof(List<>)){ var items = GetNestItems<T>((System.Collections.IList)item); foreach (var t in items) {yield return t; }} }}回答5:
自己要不循環(huán)。要不接住其他函數(shù)來幫你完成循環(huán)。
相關(guān)文章:
1. javascript - immutable配合react提升性能?2. 如何設(shè)置一個無限循環(huán)并打破它。(Java線程)3. python小白 自學(xué)看書遇到看不懂的地方4. javascript - sublime快鍵鍵問題5. macos - 如何徹底刪除mac自帶的apache和php6. docker 17.03 怎么配置 registry mirror ?7. shell - mysql更新錯誤8. DADB.class.php文件的代碼怎么寫9. php對mysql提取數(shù)據(jù)那種速度更快10. 實現(xiàn)bing搜索工具urlAPI提交

網(wǎng)公網(wǎng)安備