Collections.max function for iterable <Integer> in java

Java Collections.max accepts only a collection of the sortable object. However, since the collection is not necessarily sorted, I see no reason not to implement the same maximum function for duplicate types.

Is there a max method for Iterable<T extends Comparable<? super T>>in the standard Java library?

+5
source share
4 answers

Collections.maxwas introduced in 1.2. Iterablewas introduced in 1.5.

It is rare to have Iterableone that is not Collection. If you do this, then it is easy to implement (be careful to read the specification). If you think this is really important, you can send an RFE to bugs.sun.com (or vote if you already have one).

+5
source

Although Guava is not a standard Java library, it is close enough ...

E com.google.common.collect.Ordering#max(Iterable<E> iterable)

eg. T max = Ordering.natural().max(myIterable);

Due to the fact that the standard library does not implement it, it may be because the Collection must be finite , but Iterable does not have to be and, perhaps, It should never be accepted if the disjoint Iterable makes your code loop forever.

+13
source
0

"" ( , Comparable), , ( , Comparable).

max() Collections , , , , .

-1
source

All Articles