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. node.js - 怎么在初始化的时候使得一个子组件不加载?2. 代码如下,java用BigDecimal四舍五入怎么还有问题呀?哪种方式是完全没有问题的?3. angular.js - 请教angularjs里html和js通信的问题4. 求解决方法:Parse error: syntax error, unexpected5. 新人做网站求帮助6. java - mybatis里的REGEXP7. Linux服务器下怎么把phpStudy卸载干净8. 请问连接文件怎么写9. 没太明天这样写的作用10. mysql - thinkphp5 在MAC电脑本地正常,部署LINUX服务器之后,模型不存在