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. web服务器 - ubuntu下布置apache加wsgi加python2. dockerfile - 为什么docker容器启动不了?3. css - 关于Flex布局的问题4. css - 图片的宽度发生变化而高度却没有相应变?5. docker - 如何修改运行中容器的配置6. docker gitlab 如何git clone?7. 关docker hub上有些镜像的tag被标记““This image has vulnerabilities””8. css3 - 背景图自定义比例缩小9. javascript - sublime快键键问题10. docker 17.03 怎么配置 registry mirror ?

网公网安备