java.lang.Object
io.jenetics.stat.MinMax<C>
- All Implemented Interfaces:
Consumer<C>
This consumer class is used for calculating the min and max value
according to the given
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
);
- Since:
- 3.0
- Version:
- 6.0
- 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.
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Accept the element for min-max calculation.Combine twoMinMax
objects.long
count()
Returns the count of values recorded.max()
Return the current maximal object ornull
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.min()
Return the current minimal object ornull
if no element has been accepted yet.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>>
MinMax<C>of()
Create a newMinMax
consumer.static <T> MinMax<T>
of
(Comparator<? super T> comparator) Create a newMinMax
consumer with the givenComparator
.boolean
Compares the state of twoLongMomentStatistics
objects.static <C extends Comparable<? super C>>
Collector<C,?, MinMax<C>> toMinMax()
Return aCollector
which calculates the minimum and maximum value.toMinMax
(Comparator<? super T> comparator) Return aCollector
which calculates the minimum and maximum value.static <C extends Comparable<? super C>>
Function<C,Stream<C>> Return a new flat-mapper function, which guarantees a strictly decreasing stream, from an arbitrarily ordered source stream.toStrictlyImproving
(Comparator<? super T> comparator) Return a new flat-mapper function, which guarantees a strictly improving stream, from an arbitrarily ordered source stream.static <C extends Comparable<? super C>>
Function<C,Stream<C>> Return a new flat-mapper function, which guarantees a strictly increasing stream, from an arbitrarily ordered source stream.toString()
-
Method Details
-
accept
Accept the element for min-max calculation. -
combine
Combine twoMinMax
objects.- Parameters:
other
- the otherMinMax
object to combine- Returns:
this
- Throws:
NullPointerException
- if theother
object isnull
.
-
count
Returns the count of values recorded.- Returns:
- the count of recorded values
-
min
Return the current minimal object ornull
if no element has been accepted yet.- Returns:
- the current minimal object
-
max
Return the current maximal object ornull
if no element has been accepted yet.- Returns:
- the current maximal object
-
sameState
Compares the state of twoLongMomentStatistics
objects. This is a replacement for theObject.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); }
- Parameters:
other
- the other object for the test- Returns:
true
thethis
and theother
objects have the same state,false
otherwise- Since:
- 3.7
-
toString
-
min
Return the minimum of two values, according the given comparator.null
values are allowed.- Type Parameters:
T
- the type of the compared objects- Parameters:
comp
- the comparator used for determining the min valuea
- the first value to compareb
- the second value to compare- Returns:
- the minimum value, or
null
if both values arenull
. If only one value isnull
, the nonnull
values is returned.
-
max
Return the maximum of two values, according the given comparator.null
values are allowed.- Type Parameters:
T
- the type of the compared objects- Parameters:
comp
- the comparator used for determining the max valuea
- the first value to compareb
- the second value to compare- Returns:
- the maximum value, or
null
if both values arenull
. If only one value isnull
, the nonnull
values is returned.
-
toMinMax
Return aCollector
which calculates the minimum and maximum value. The givencomparator
is used for comparing two objects.final Comparator<SomeObject> comparator = ... final Stream<SomeObject> stream = ... final MinMax<SomeObject> moments = stream .collect(doubleMoments.toMinMax(comparator));
- Type Parameters:
T
- the type of the input elements- Parameters:
comparator
- theComparator
to use- Returns:
- a
Collector
implementing the min-max reduction - Throws:
NullPointerException
- if the givenmapper
isnull
-
toMinMax
Return aCollector
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));
- Type Parameters:
C
- the type of the input elements- Returns:
- a
Collector
implementing the min-max reduction - Throws:
NullPointerException
- if the givenmapper
isnull
-
of
Create a newMinMax
consumer with the givenComparator
.- Type Parameters:
T
- the element type- Parameters:
comparator
- the comparator used for comparing two elements- Returns:
- a new
MinMax
consumer - Throws:
NullPointerException
- if thecomparator
isnull
.
-
of
Create a newMinMax
consumer.- Type Parameters:
C
- the element type- Returns:
- a new
MinMax
consumer
-
toStrictlyIncreasing
Return a new flat-mapper function, which guarantees a strictly increasing stream, from an arbitrarily ordered source stream. Note that this function doesn't sort the stream. It just skips the out of order elements.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]
- Type Parameters:
C
- the comparable type- Returns:
- a new flat-mapper function
- Since:
- 5.0
-
toStrictlyDecreasing
Return a new flat-mapper function, which guarantees a strictly decreasing stream, from an arbitrarily ordered source stream. Note that this function doesn't sort the stream. It just skips the out of order elements.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]
- Type Parameters:
C
- the comparable type- Returns:
- a new flat-mapper function
- Since:
- 5.0
-
toStrictlyImproving
Return a new flat-mapper function, which guarantees a strictly improving stream, from an arbitrarily ordered source stream. Note that this function doesn't sort the stream. It just skips the out of order elements.final ISeq<Integer> values = new Random().ints(0, 100) .boxed() .limit(100) .flatMap(MinMax.toStrictlyImproving(Comparator.naturalOrder())) .collect(ISeq.toISeq()); System.out.println(values); // [6,47,65,78,96,96,99]
- Type Parameters:
T
- the element type- Parameters:
comparator
- the comparator used for testing the elements- Returns:
- a new flat-mapper function
- Since:
- 6.0
- See Also:
-