Reputation: 33623
The Java Collections.max
method takes only a collection of a sortable (Comparable
) object. However since the collection is not necessarily sorted, I don't see any reason not to implement the same max function for Iterable
types.
Is there a max method for Iterable<T extends Comparable<? super T>>
in Java's standard library?
Upvotes: 9
Views: 10545
Reputation: 21256
My observation of Iterable
is that, while it could be used in preference to Collection
in many places where a stream of data is needed but not the various collection methods like size, it typically isn't within the Java core libraries.
This is just an inference on my part, but it feels like the Java designers are really sticking to Iterable
just being a way to use the enhanced for
statement for a given stream of data, and use Collection
where a bunch of data items are needed, even if Iterable
would be sufficient.
A look at the Javadocs for Iterable
adds weight to this viewpoint, as this is the only thing called out in the class-level Javadocs:
Implementing this interface allows an object to be the target of the enhanced
for
statement (sometimes called the "for-each loop" statement).
Upvotes: 0
Reputation: 21256
With the introduction of streams with Java 8, operations like this that could be done on an Iterable
are generally instead done on a Stream
.
StreamSupport.stream
can convert any Iterable
to a Stream
, which means that the stream-based approach to finding a max value can be used for an arbitrary Iterable
:
Optional<Integer> max = StreamSupport.stream(iterable.spliterator(), false)
.max(Comparator.naturalOrder());
Upvotes: 0
Reputation: 147164
Collections.max
was introduced in 1.2. Iterable
was introduced in 1.5.
It's rare to have an Iterable
that is not a Collection
. If you do then it's straightforward to implement (be careful to read the spec). If you think it is really important you can submit an RFE in the Java Bug Database (or vote if there is already one there).
Upvotes: 5
Reputation: 13257
While Guava is not Java's standard library, it's close enough...
E com.google.common.collect.Ordering#max(Iterable<E> iterable)
e.g. T max = Ordering.natural().max(myIterable);
As to why the standard library does not implement it, it may be because a Collection must be finite, but an Iterable need not be—and, arguably, one should never accept an Iterable if a non-terminating Iterable would cause your code to loop forever.
Upvotes: 16
Reputation: 103827
By definition the elements of the collection must be "sortable" (specifically, they must implements Comparable
) since in order to compute the maximum, it must be possible to work out whether one element is greater than another (which is exactly what Comparable means).
The max() method in the Collections class has essentially the exact type signature you posted there, so it should suit your purpose.
Upvotes: -2
Reputation: 83938
Hmm… no, there isn’t. If you want to use Collections.max() you have to convert your Iterable into a Collection first, probably by adding all of the elements into a List (or Set, depending on the data).
Upvotes: 0