java - List<List<model>>如何更快捷的取里面的model?
問(wèn)題描述
訪問(wèn)接口返回?cái)?shù)據(jù)類型為L(zhǎng)ist<List<model>>,現(xiàn)在想將其中的model插入數(shù)據(jù)庫(kù),感覺(jué)一點(diǎn)點(diǎn)循環(huán)有點(diǎn)傻,0.0...,各位有沒(méi)有其他的方法?
問(wèn)題解答
回答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ù)來(lái)幫你完成循環(huán)。
相關(guān)文章:
1. node.js - nodejs如何發(fā)送請(qǐng)求excel文件并下載2. angular.js - 為什么給 Angular 指令綁定事件無(wú)法生效3. docker-machine添加一個(gè)已有的docker主機(jī)問(wèn)題4. 為什么我ping不通我的docker容器呢???5. golang - 用IDE看docker源碼時(shí)的小問(wèn)題6. html5 - 使用angular中,圖片上傳功能中選擇多張圖片是怎么實(shí)現(xiàn)的?有什么好的思路嗎?7. docker - 各位電腦上有多少個(gè)容器啊?容器一多,自己都搞混了,咋辦呢?8. mysql - 詳細(xì)論述聯(lián)表查詢與分步查詢的優(yōu)缺點(diǎn)9. mysql - 求SQL語(yǔ)句10. PHP注冊(cè)功能
