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 TypeMethodDescriptionvoidAccept the element for min-max calculation.Combine twoMinMaxobjects.longcount()Returns the count of values recorded.max()Return the current maximal object ornullif no element has been accepted yet.static <T> Tmax(Comparator<? super T> comp, T a, T b) Return the maximum of two values, according the given comparator.min()Return the current minimal object ornullif no element has been accepted yet.static <T> Tmin(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 newMinMaxconsumer.static <T> MinMax<T> of(Comparator<? super T> comparator) Create a newMinMaxconsumer with the givenComparator.booleanCompares the state of twoLongMomentStatisticsobjects.static <C extends Comparable<? super C>>
Collector<C, ?, MinMax<C>> toMinMax()Return aCollectorwhich calculates the minimum and maximum value.toMinMax(Comparator<? super T> comparator) Return aCollectorwhich 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 twoMinMaxobjects.- Parameters:
other- the otherMinMaxobject to combine- Returns:
this- Throws:
NullPointerException- if theotherobject isnull.
-
count
Returns the count of values recorded.- Returns:
- the count of recorded values
-
min
Return the current minimal object ornullif no element has been accepted yet.- Returns:
- the current minimal object
-
max
Return the current maximal object ornullif no element has been accepted yet.- Returns:
- the current maximal object
-
sameState
Compares the state of twoLongMomentStatisticsobjects. This is a replacement for theObject.equals(Object)which is not advisable to implement for this mutable object. If two objects have the same state, it has still the same state when updated with the same value.final MinMax<Long> mm1 = ...; final MinMax<Long> 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:
truethethisand theotherobjects have the same state,falseotherwise- Since:
- 3.7
-
toString
-
min
Return the minimum of two values, according the given comparator.nullvalues 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
nullif both values arenull. If only one value isnull, the nonnullvalues is returned.
-
max
Return the maximum of two values, according the given comparator.nullvalues 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
nullif both values arenull. If only one value isnull, the nonnullvalues is returned.
-
toMinMax
Return aCollectorwhich calculates the minimum and maximum value. The givencomparatoris 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- theComparatorto use- Returns:
- a
Collectorimplementing the min-max reduction - Throws:
NullPointerException- if the givenmapperisnull
-
toMinMax
Return aCollectorwhich 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
Collectorimplementing the min-max reduction - Throws:
NullPointerException- if the givenmapperisnull
-
of
Create a newMinMaxconsumer with the givenComparator.- Type Parameters:
T- the element type- Parameters:
comparator- the comparator used for comparing two elements- Returns:
- a new
MinMaxconsumer - Throws:
NullPointerException- if thecomparatorisnull.
-
of
Create a newMinMaxconsumer.- Type Parameters:
C- the element type- Returns:
- a new
MinMaxconsumer
-
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:
-