首页 > 解决方案 > 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))

标签: arraylistjava-8java-stream

解决方案


首先,您发布的代码甚至无法编译。从嵌套列表中获取最大数字的一种方法。

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());

推荐阅读