arraylist - how to find the max value of nested arraylist of string using streams
问题描述
I have an ArrayList
in the below format
List<List<String>> ll = [["0", "a"], ["1", "b"], ["0", "c"], ["1", "d"]]
I want to find the maximum value by considering the first position in the nested list. How can I do it using streams?
Using streams how to find the maximum value by taking the position at
Integer.parseInt(ll.get(i).get(0))
解决方案
首先,您发布的代码甚至无法编译。从嵌套列表中获取最大数字的一种方法。
List<List<String>> ll = Arrays.asList(Arrays.asList("10", "a"), Arrays.asList("21", "b"), Arrays.asList("10", "c"),
Arrays.asList("11", "d"));
OptionalInt max = ll.stream().flatMap(l -> l.stream()).filter(str -> Character.isDigit(str.charAt(0)))
.distinct().mapToInt(i -> Integer.parseInt(i)).max();
System.out.println(max.getAsInt());
推荐阅读
- python - 根据另一列的当前行和同一列的上一行递增一列的值
- ssl - Hashicorp Consul - 如何从 Kubernetes 集群中的 Pod 中执行经过验证的 TLS
- laravel - 使用 Spate 媒体库时出错:“BadMethodCallException 调用未定义的方法 Illuminate\Foundation\Auth\User::addMediaFromRequest()”
- angular - 背景图片网址在我的角度项目中不起作用
- html - 使用 HTTP post 方法而不是 DELETE 是否有任何危害
- c# - 更新 UnitAmount of Stripe 结帐价格
- javascript - office-ui-fabric / fluent-ui Grouped DetailsList
- docker - 如何使用 H2 数据库容器化一个简单的 Java 应用程序
- vue.js - 防止vue cli删除dist中的所有文件
- c++ - 你必须在 OpenGL 程序中调用 glfwSwapInterval(1) 吗?