127

I have a list of Integer values named list, and from the list.stream() I want the maximum value.

What is the simplest way? Do I need a comparator?

2

11 Answers 11

273

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(); 
Sign up to request clarification or add additional context in comments.

9 Comments

Would be interesting to know which one is more efficient.
May I ask why, Tagir?
@elect, it first unboxes all the integers, then compares the unboxed ones. 2nd, 3rd and 4th solutions do the unboxing on each comparison effectively doing twice as much unboxing operations. The last one computes more statistics (like sum and min) which is unnecessary here, but will surely take some time.
If you want to just get an int, then mapToInt(...).max().getAsInt() or reduce(...).get() to the method chains
@Bogdan, this is solveable, though apparently was not required in question. Nevertheless you can post your own answer covering this situation.
|
14
int max = list.stream().reduce(Integer.MIN_VALUE, (a, b) -> Integer.max(a, b)); 

2 Comments

This works only, if all of your values are positive. Use Integer.MIN_VALUE instead of 0 in reduce().
Could also be int max = list.stream()reduce(Integer.MIN_VALUE, Integer::max);
5

Another version could be:

int maxUsingCollectorsReduce = list.stream().collect(Collectors.reducing(Integer::max)).get(); 

Comments

4

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

Comments

3

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

Comments

3
int value = list.stream().max(Integer::compareTo).get(); System.out.println("value :"+value ); 

1 Comment

There are other answers that provide the OP's question, and they were posted many years ago. When posting an answer, please make sure you add either a new solution, or a substantially better explanation, especially when answering older questions.
3

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.

Comments

0

With stream and reduce

Optional<Integer> max = list.stream().reduce(Math::max); 

1 Comment

It seems you posted this answer twice and removed the other one, but as I commented on the other one, this solution is already included in Tagir's answer (with Integer::max but that's exactly the same).
0

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

1 Comment

a - b is dangerous because of integer overflow → Arrays.asList(-2147483640, 100).stream().max((a, b) -> a - b) will result in Optional[-2147483640] but -2147483640 is not greater than 100
0

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

1 Comment

more Stream-like: list.stream().map(TxnCharges::getSeqNum).map(String::trim).mapToInt(Integer::parseInt).max().orElse(0) (but not applicable to List<Integer> as asked for)
-2

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

1 Comment

You should start from Integer.MIN_VALUE to make it work with negative numbers.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.