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. 我的html頁面一提交,網頁便顯示出了我的php代碼,求問是什么原因?2. 我在centos容器里安裝docker,也就是在容器里安裝容器,報錯了?3. 數據庫 - 使用讀寫分離后, MySQL主從復制延遲會導致讀不到數據嗎?4. tp6表單令牌5. docker 17.03 怎么配置 registry mirror ?6. 老哥們求助啊7. django - 后臺返回的json數據經過Base64加密,獲取時用python如何解密~!8. node.js - node 客戶端socket一直報錯Error: read ECONNRESET,用php的socket沒問題哈。。9. 如何解決docker宿主機無法訪問容器中的服務?10. javascript - canvas 可以實現 PS 魔法橡皮擦的功能嗎?
