Reputation: 161
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))
Upvotes: 2
Views: 994
Reputation: 21124
You may do it like so,
int max = ll.stream().mapToInt(l -> Integer.parseInt(l.get(0))).max()
.orElseThrow(IllegalArgumentException::new);
Upvotes: 1
Reputation: 15423
Firstly, the code you posted doesn't even compile. One approach of fetching the largest number from a nested list.
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());
Upvotes: 1