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. javascript - 按鈕鏈接到另一個網址 怎么通過百度統計計算按鈕的點擊數量2. java - jdbc如何返回自動定義的bean3. python - 請問這兩個地方是為什么呢?4. 請教一個mysql去重取最新記錄5. 大家都用什么工具管理mysql數據庫?6. Python處理Dict生成json7. mysql的循環語句問題8. python - 為什么match匹配出來的結果是<_sre.SRE_Match object; span=(0, 54), match=’’>9. mysql優化 - mysql 一張表如果不能確保字段列長度一致,是不是就不需要用到char。10. mysql updtae追加數據sql語句
