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 - yo webapp构建项目时报错了,如图2. MYSQL代码执行错误:FUNCTION any_value does not exist3. python - Win7调用flup报错’module’ object has no attribute ’fromfd’4. javascript - 在vuejs中如何根据变量去引用不同的css样式呢5. javascript - 求赐教:网易邮箱Web端模拟登录看信的加密参数_ntes_nnid、_ntes_nuid6. css - 图片的宽度发生变化而高度却没有相应变?7. python打包代码上传PYPI出现错误500,虚心求教8. 请问老师的PS编辑器,安装了哪些插件?感觉自己显示的风格太原始了!谢谢!9. mysql怎么表示两个字段的差10. 网页爬虫 - 关于python3.x 编写爬虫异常问题请各位大神帮忙!

网公网安备