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. javascript - 求解答:实例对象调用constructor,此时constructor内的this的指向?2. python中生产者消费者线程问题3. Windows系统能否利用Docker使用Ubuntu吗?Ubuntu能使用本机的显卡吗?4. 请教,关于python字典,合并相同值的键的实现方法5. python的MySQLdb库中的executemany方法如何改变默认加上的单引号?6. android - 京东移动端网页和其app加载的url所做的呈现不应该是完全一样的吗?7. mysql5.7就没有官方性质的详细配置文件吗?求大神告知8. 百度地图api - Android 百度地图 集成了定位,导航 相互的jar包有冲突?9. css3中translate(-50%,-50%)对 transform-origin的奇葩影响?10. html5 - 在一个页面中 初始了两个swiper 不知道哪里错了 一直不对

网公网安备