public final class MinMax<C> extends Object implements Consumer<C>
Comparator
.
This class is designed to work with (though does not require) streams. For example, you can compute minimum and maximum values with:
final Stream<Integer> stream = ...
final MinMax<Integer> minMax = stream.collect(
MinMax::of,
MinMax::accept,
MinMax::combine
);
Implementation note:
This implementation is not thread safe. However, it is safe to use on a
parallel stream, because the parallel implementation of
Stream.collect()
provides the
necessary partitioning, isolation, and merging of results for safe and
efficient parallel execution.
Modifier and Type | Method and Description |
---|---|
void |
accept(C object)
Accept the element for min-max calculation.
|
MinMax<C> |
combine(MinMax<C> other)
Combine two
MinMax objects. |
long |
getCount()
Returns the count of values recorded.
|
C |
getMax()
Return the current maximal object or
null if no element has been
accepted yet. |
C |
getMin()
Return the current minimal object or
null if no element has been
accepted yet. |
static <T> T |
max(Comparator<? super T> comp,
T a,
T b)
Return the maximum of two values, according the given comparator.
|
static <T> T |
min(Comparator<? super T> comp,
T a,
T b)
Return the minimum of two values, according the given comparator.
|
static <C extends Comparable<? super C>> |
of()
Create a new
MinMax consumer. |
static <T> MinMax<T> |
of(Comparator<? super T> comparator)
Create a new
MinMax consumer with the given
Comparator . |
static <C extends Comparable<? super C>> |
toMinMax()
Return a
Collector which calculates the minimum and maximum value. |
static <T> Collector<T,?,MinMax<T>> |
toMinMax(Comparator<? super T> comparator)
Return a
Collector which calculates the minimum and maximum value. |
public MinMax<C> combine(MinMax<C> other)
MinMax
objects.other
- the other MinMax
object to combinethis
NullPointerException
- if the other
object is
null
.public C getMin()
null
if no element has been
accepted yet.public C getMax()
null
if no element has been
accepted yet.public long getCount()
public static <T> T min(Comparator<? super T> comp, T a, T b)
null
values are allowed.T
- the type of the compared objectscomp
- the comparator used for determining the min valuea
- the first value to compareb
- the second value to comparenull
if both values are null
.
If only one value is null
, the non null
values is
returned.public static <T> T max(Comparator<? super T> comp, T a, T b)
null
values are allowed.T
- the type of the compared objectscomp
- the comparator used for determining the max valuea
- the first value to compareb
- the second value to comparenull
if both values are null
.
If only one value is null
, the non null
values is
returned.public static <T> Collector<T,?,MinMax<T>> toMinMax(Comparator<? super T> comparator)
Collector
which calculates the minimum and maximum value.
The given comparator
is used for comparing two objects.
final Comparator<SomeObject> comparator = ...
final Stream<SomeObject> stream = ...
final MinMax<SomeObject> moments = stream
.collect(doubleMoments.toMinMax(comparator));
T
- the type of the input elementscomparator
- the Comparator
to useCollector
implementing the min-max reductionNullPointerException
- if the given mapper
is
null
public static <C extends Comparable<? super C>> Collector<C,?,MinMax<C>> toMinMax()
Collector
which calculates the minimum and maximum value.
The reducing objects must be comparable.
final Stream<SomeObject> stream = ...
final MinMax<SomeObject> moments = stream
.collect(doubleMoments.toMinMax(comparator));
C
- the type of the input elementsCollector
implementing the min-max reductionNullPointerException
- if the given mapper
is
null
public static <T> MinMax<T> of(Comparator<? super T> comparator)
MinMax
consumer with the given
Comparator
.T
- the element typecomparator
- the comparator used for comparing two elementsMinMax
consumerNullPointerException
- if the comparator
is
null
.public static <C extends Comparable<? super C>> MinMax<C> of()
MinMax
consumer.C
- the element typeMinMax
consumer© 2007-2016 Franz Wilhelmstötter (2016-04-24 10:25)