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. windows-7 - Wamp集成环境Apache无法启动2. angular.js - 关于ng-model和ng-bind的疑问3. android - NavigationView 的侧滑菜单中如何保存新增项(通过程序添加)4. nginx 80端口反向代理多个域名,怎样隐藏端口的?5. node.js - 跑antd的的模板例子!想修改端口,怎么修改呢!!(里面好像用了什么dora插件!!!)6. javascript - webpack 报错 新人 求解7. node.js - 问个问题 Uncaught (in promise)8. angular.js - angular做点击购买时的遮罩层9. 急急急!!!求大神解答网站评论问题,有大神帮帮小弟吗10. 求一个PHP编程码、。
