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. angular.js - Web应用,单页面应用Cache问题2. android - webview 自定义加载进度条3. position:absolute、float、display:inline-block 都能实现相同效果,区别是什么?4. javascript - vue更改当前节点元素5. Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?6. javascript - echart2.0 饼图不能自适应???7. 如何解决Centos下Docker服务启动无响应,且输入docker命令无响应?8. angular.js - angular post的Content-Type被设置,导致不能上传图片,求助!!9. vue.js - vue 打包后 nginx 服务端API请求跨域问题无法解决。10. css3 - 这个效果用 CSS 可以实现吗?border-image

网公网安备