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. 为什么span的color非要内联样式才起作用?2. 小白问题getDay()3. html5 - H5做的手机分享页微信更新后,分享出去不再默认显示第一个图 作为缩略图4. 关于docker下的nginx压力测试5. golang - 用IDE看docker源码时的小问题6. docker start -a dockername 老是卡住,什么情况?7. css - div设置float:left后高度设置自动会无效 ?8. 请问一下各位老鸟 我一直在学习独孤九贱 现在是在tp5 今天发现 这个系列视频没有实战9. 求救一下,用新版的phpstudy,数据库过段时间会消失是什么情况?10. 老师,请问我打开browsersync出现这个问题怎么解决啊?

网公网安备