java - List<List<model>>如何更快捷的取里面的model?
問題描述
訪問接口返回?cái)?shù)據(jù)類型為L(zhǎng)ist<List<model>>,現(xiàn)在想將其中的model插入數(shù)據(jù)庫(kù),感覺一點(diǎn)點(diǎn)循環(huán)有點(diǎ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. docker 下面創(chuàng)建的IMAGE 他們的 ID 一樣?這個(gè)是怎么回事????2. 在應(yīng)用配置文件 app.php 中找不到’route_check_cache’配置項(xiàng)3. javascript - vue-cli與后端框架集成config/index.js配置問題4. 關(guān)于layuiadmin中表格按鈕提交問題求解!!!!5. javascript - 為什么我無(wú)法通過$stateParams在父子State之間傳遞參數(shù)?跟State之間的父子關(guān)系有關(guān)嗎?6. javascript - 關(guān)于mongose刪除一次多個(gè)字段的問題7. javascript - 使用reaaft route 報(bào)錯(cuò)8. 微信瀏覽器怎么取消緩存?9. html按鍵開關(guān)如何提交我想需要的值到數(shù)據(jù)庫(kù)10. html5 - 在微信上掃二維碼看到有趣的h5頁(yè)面,怎樣才可以看到它的代碼呢?
