java - List<List<model>>如何更快捷的取里面的model?
問題描述
訪問接口返回數據類型為List<List<model>>,現在想將其中的model插入數據庫,感覺一點點循環有點傻,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:數據結構使然,循環吧
回答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:
自己要不循環。要不接住其他函數來幫你完成循環。
相關文章:
1. node.js - nodejs+express+vue2. javascript - vue2.0中使用vue2-dropzone的demo,vue2-dropzone的github網址是什么??百度不到。3. node.js - win7下,npm 無法下載依賴包,淘寶鏡像也裝不上,求幫忙???4. Python 子類能否覆蓋全局函數?5. javascript - JS如何取對稱范圍的隨機數?6. 前端 - @media query 使用出現的問題?7. java軟引用在android中有實際應用場景嗎?8. kv存儲 - 怎樣用 Redis 緩存微博的評論列表?9. javascript - vue上傳圖片,并顯示在頁面中的插件10. mysql - sql 找出2個數據庫的差異表名
