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
);
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 . |
boolean |
sameState(MinMax<C> other)
Compares the state of two
LongMomentStatistics objects. |
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. |
static <C extends Comparable<? super C>> |
toStrictlyDecreasing()
Return a new flat-mapper function, which guarantees a strictly decreasing
stream, from an arbitrarily ordered source stream.
|
static <C extends Comparable<? super C>> |
toStrictlyIncreasing()
Return a new flat-mapper function, which guarantees a strictly increasing
stream, from an arbitrarily ordered source stream.
|
String |
toString() |
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 boolean sameState(MinMax<C> other)
LongMomentStatistics
objects. This is
a replacement for the Object.equals(Object)
which is not advisable to
implement for this mutable object. If two object have the same state, it
has still the same state when updated with the same value.
final MinMax mm1 = ...;
final MinMax mm2 = ...;
if (mm1.sameState(mm2)) {
final long value = random.nextInt(1_000_000);
mm1.accept(value);
mm2.accept(value);
assert mm1.sameState(mm2);
assert mm2.sameState(mm1);
assert mm1.sameState(mm1);
}
other
- the other object for the testtrue
the this
and the other
objects have
the same state, false
otherwisepublic 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
consumerpublic static <C extends Comparable<? super C>> Function<C,Stream<C>> toStrictlyIncreasing()
final ISeq<Integer> values = new Random().ints(0, 100)
.boxed()
.limit(100)
.flatMap(MinMax.toStrictlyIncreasing())
.collect(ISeq.toISeq());
System.out.println(values);
// [6,47,65,78,96,96,99]
C
- the comparable typepublic static <C extends Comparable<? super C>> Function<C,Stream<C>> toStrictlyDecreasing()
final ISeq<Integer> values = new Random().ints(0, 100)
.boxed()
.limit(100)
.flatMap(MinMax.toStrictlyDecreasing())
.collect(ISeq.toISeq());
System.out.println(values);
// [45,32,15,12,3,1]
C
- the comparable type© 2007-2019 Franz Wilhelmstötter (2019-06-23 15:39)