Package io.jenetics.stat
Class MinMax<C>
- java.lang.Object
-
- io.jenetics.stat.MinMax<C>
-
- All Implemented Interfaces:
Consumer<C>
public final class MinMax<C> extends Object implements Consumer<C>
This consumer class is used for calculating the min and max value according to the givenComparator.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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaccept(C object)Accept the element for min-max calculation.MinMax<C>combine(MinMax<C> other)Combine twoMinMaxobjects.longcount()Returns the count of values recorded.Cmax()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.Cmin()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.booleansameState(MinMax<C> other)Compares the state of twoLongMomentStatisticsobjects.static <C extends Comparable<? super C>>
Collector<C,?,MinMax<C>>toMinMax()Return aCollectorwhich calculates the minimum and maximum value.static <T> Collector<T,?,MinMax<T>>toMinMax(Comparator<? super T> comparator)Return aCollectorwhich calculates the minimum and maximum value.static <C extends Comparable<? super C>>
Function<C,Stream<C>>toStrictlyDecreasing()Return a new flat-mapper function, which guarantees a strictly decreasing stream, from an arbitrarily ordered source stream.static <T> Function<T,Stream<T>>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>>toStrictlyIncreasing()Return a new flat-mapper function, which guarantees a strictly increasing stream, from an arbitrarily ordered source stream.StringtoString()
-
-
-
Method Detail
-
combine
public MinMax<C> combine(MinMax<C> other)
Combine twoMinMaxobjects.- Parameters:
other- the otherMinMaxobject to combine- Returns:
this- Throws:
NullPointerException- if theotherobject isnull.
-
count
public long count()
Returns the count of values recorded.- Returns:
- the count of recorded values
-
min
public C min()
Return the current minimal object ornullif no element has been accepted yet.- Returns:
- the current minimal object
-
max
public C max()
Return the current maximal object ornullif no element has been accepted yet.- Returns:
- the current maximal object
-
sameState
public boolean sameState(MinMax<C> other)
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 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:
truethethisand theotherobjects have the same state,falseotherwise- Since:
- 3.7
-
min
public static <T> T min(Comparator<? super T> comp, T a, T b)
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
public static <T> T max(Comparator<? super T> comp, T a, T b)
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
public static <T> Collector<T,?,MinMax<T>> toMinMax(Comparator<? super T> comparator)
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
public static <C extends Comparable<? super C>> Collector<C,?,MinMax<C>> 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
public static <T> MinMax<T> of(Comparator<? super T> comparator)
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
public static <C extends Comparable<? super C>> MinMax<C> of()
Create a newMinMaxconsumer.- Type Parameters:
C- the element type- Returns:
- a new
MinMaxconsumer
-
toStrictlyIncreasing
public static <C extends Comparable<? super C>> Function<C,Stream<C>> 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
public static <C extends Comparable<? super C>> Function<C,Stream<C>> 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
public static <T> Function<T,Stream<T>> toStrictlyImproving(Comparator<? super T> comparator)
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:
toStrictlyIncreasing(),toStrictlyDecreasing()
-
-