I have a list of Integer
list
and from the list.stream()
I want the maximum value.
What is the simplest way? Do I need comparator?
I have a list of Integer
list
and from the list.stream()
I want the maximum value.
What is the simplest way? Do I need comparator?
You may either convert the stream to IntStream
:
OptionalInt max = list.stream().mapToInt(Integer::intValue).max();
Or specify the natural order comparator:
Optional<Integer> max = list.stream().max(Comparator.naturalOrder());
Or use reduce operation:
Optional<Integer> max = list.stream().reduce(Integer::max);
Or use collector:
Optional<Integer> max = list.stream().collect(Collectors.maxBy(Comparator.naturalOrder()));
Or use IntSummaryStatistics:
int max = list.stream().collect(Collectors.summarizingInt(Integer::intValue)).getMax();
int
, then mapToInt(...).max().getAsInt()
or reduce(...).get()
to the method chains –
Faction list.stream().max()
using the default natural order comparator. Honestly all above options are shit to write, read and remember from a developer point of view. Thats why i come back to the same lambda questions all the time in java my daily language. Just compare same things with c# which i not even work a day per month in and dont even need to lookup things so easy is their lambda. Java messed up big again similar to Date API and now we are stuck. –
Onward .filter(Objects::nonNull)
–
Sachasachem int max = list.stream().reduce(Integer.MIN_VALUE, (a, b) -> Integer.max(a, b));
Another version could be:
int maxUsingCollectorsReduce = list.stream().collect(Collectors.reducing(Integer::max)).get();
Correct code:
int max = list.stream().reduce(Integer.MIN_VALUE, (a, b) -> Integer.max(a, b));
or
int max = list.stream().reduce(Integer.MIN_VALUE, Integer::max);
You can also use below code snipped:
int max = list.stream().max(Comparator.comparing(Integer::valueOf)).get();
Another alternative:
list.sort(Comparator.reverseOrder()); // max value will come first
int max = list.get(0);
int value = list.stream().max(Integer::compareTo).get();
System.out.println("value :"+value );
I think another easy way is
IntSummaryStatistics statistics = List.of(1, 2, 3).stream()
.mapToInt(Integer::intValue)
.summaryStatistics();
int max = statistics.getMax();
With this you can also getMin()
amd other stuff like mean. A SummaryStatistics Object can be created from other Streams by supplying appropriate parameters.
With stream and reduce
Optional<Integer> max = list.stream().reduce(Math::max);
Integer::max
but that's exactly the same). –
Gare you can also find the max element in a collection using bellow code:
List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
Optional<Integer> max = intList.stream().max((a, b) -> a - b);
In my case, need to convert a String (SeqNum) to integer and find a max value of it.
list.stream().map(TxnCharges::getSeqNum)
.max(Comparator.comparingInt(s -> Integer.parseInt(s.trim())))
.orElse(null);
You could use int max= Stream.of(1,2,3,4,5).reduce(0,(a,b)->Math.max(a,b)); works for both positive and negative numbers
Integer.MIN_VALUE
to make it work with negative numbers. –
Gare © 2022 - 2024 — McMap. All rights reserved.
Collections.max
.. – Goldiegoldilocks