Interface Partition<K,V>

Type Parameters:
K - the type of the partition key
V - the type of the partition value
All Superinterfaces:
Displayable, Formattable<org.apache.commons.lang3.tuple.Pair<K,V>>
All Known Implementing Classes:
Groups, ImmutablePartition, SampledResults

public interface Partition<K,V> extends Formattable<org.apache.commons.lang3.tuple.Pair<K,V>>
A stream of key-value pairs. Duplicate keys are permitted.
  • Method Summary

    Modifier and Type
    Method
    Description
    default org.apache.commons.lang3.tuple.Pair<K,V>
    any()
    Returns any key-value pair from this partition.
    default TabularData<org.apache.commons.lang3.tuple.Pair<K,V>>
    Returns the contents of this object as a TabularData instance, which can be used to save, print, or format the data in various ways.
    default Partition<K,V>
    Retains only the unique key-value pairs in this partition.
    default void
    enumerate(BiConsumer<Integer,org.apache.commons.lang3.tuple.Pair<K,V>> consumer)
    Similar to forEach(BiConsumer) except the index is included.
    default Partition<K,V>
    filter(Predicate<K> predicate)
    Filters this partition, keeping only those keys evaluating to true.
    default org.apache.commons.lang3.tuple.Pair<K,V>
    Returns the first key-value pair from this partition.
    default void
    forEach(BiConsumer<K,V> consumer)
    Invokes a method for each key-value pair in this partition.
    default <G> Groups<G,K,V>
    groupBy(Function<K,G> group)
    Applies a grouping function to the keys in this partition.
    default boolean
    Returns true if this partition is empty.
    default List<K>
    Returns the keys in this partition as a list.
    default K[]
    keys(IntFunction<K[]> generator)
    Returns the keys in this partition as an array.
    default <R> Partition<K,R>
    map(Function<V,R> map)
    Applies a function to each value in the partition, returning a partition of the results.
    default <R> R
    measure(Function<Stream<V>,R> measure)
    Applies a measurement function to the values in this partition.
    static <K, V> Partition<K,V>
    of()
    Creates an empty partition.
    static <V> Partition<V,V>
    of(Iterable<V> iterable)
    Creates a partition with the contents of an Iterable.
    static <K> Partition<K,Double>
    of(Function<Double,K> key, DoubleStream stream)
    Creates a partition with the contents of a DoubleStream.
    static <K> Partition<K,Integer>
    of(Function<Integer,K> key, IntStream stream)
    Creates a partition with the contents of a IntStream.
    static <K, V> Partition<K,V>
    of(Function<V,K> key, Iterable<V> iterable)
    Creates a partition with the contents of an Iterable.
    static <K, V> Partition<K,V>
    of(Function<V,K> key, Stream<V> stream)
    Creates a partition with the contents of a Stream.
    static <K, V> Partition<K,V>
    of(Function<V,K> key, V[] array)
    Creates a partition with the contents of an array.
    static <K, V> Partition<K,V>
    of(Map<K,V> map)
    Creates a partition with the contents of a Map.
    of(DoubleStream stream)
    Creates a partition with the contents of a DoubleStream.
    of(IntStream stream)
    Creates a partition with the contents of a IntStream.
    static <V> Partition<V,V>
    of(Stream<V> stream)
    Creates a partition with the contents of a Stream.
    static <V> Partition<V,V>
    of(V[] array)
    Creates a partition with the contents of an array.
    default V
    Applies a binary reduction operator to the values in this partition.
    default V
    reduce(V identity, BinaryOperator<V> op)
    Applies a binary reduction operator to the values in this partition.
    default org.apache.commons.lang3.tuple.Pair<K,V>
    Asserts this partition contains exactly one key-value pair, returning said item.
    default org.apache.commons.lang3.tuple.Pair<K,V>
    singleOrDefault(K defaultKey, V defaultValue)
    Returns the singular key-value pair contained in this partition, or if empty, returns the given default value.
    int
    Returns the number of key-value pairs in this partition.
    default Partition<K,V>
    Sorts the partition using the natural ordering of keys.
    default Partition<K,V>
    sorted(Comparator<K> comparator)
    Sorts the partition based on their keys.
    Stream<org.apache.commons.lang3.tuple.Pair<K,V>>
    Returns a Stream of the key-value pairs in this partition.
    default List<V>
    Returns the values in this partition as a list.
    default V[]
    values(IntFunction<V[]> generator)
    Returns the values in this partition as an array.
    static <K, V> Partition<K,V>
    zip(Iterable<K> keys, Iterable<V> values)
    Creates a partition by "zipping" together two iterables.
    static <K, V> Partition<K,V>
    zip(K[] keys, V[] values)
    Creates a partition by "zipping" together two arrays.

    Methods inherited from interface org.moeaframework.util.format.Displayable

    display

    Methods inherited from interface org.moeaframework.util.format.Formattable

    display, display, display, save, save, save
  • Method Details

    • size

      int size()
      Returns the number of key-value pairs in this partition.
      Returns:
      the number of items
    • stream

      Stream<org.apache.commons.lang3.tuple.Pair<K,V>> stream()
      Returns a Stream of the key-value pairs in this partition.
      Returns:
      the stream
    • isEmpty

      default boolean isEmpty()
      Returns true if this partition is empty.
      Returns:
      true if this partition is empty; false otherwise
    • keys

      default List<K> keys()
      Returns the keys in this partition as a list.
      Returns:
      the list of keys
    • keys

      default K[] keys(IntFunction<K[]> generator)
      Returns the keys in this partition as an array.
      Parameters:
      generator - generator for creating the array
      Returns:
      the array of keys
    • values

      default List<V> values()
      Returns the values in this partition as a list.
      Returns:
      the list of values
    • values

      default V[] values(IntFunction<V[]> generator)
      Returns the values in this partition as an array.
      Parameters:
      generator - generator for creating the array
      Returns:
      the array of values
    • map

      default <R> Partition<K,R> map(Function<V,R> map)
      Applies a function to each value in the partition, returning a partition of the results.
      Type Parameters:
      R - the result type
      Parameters:
      map - the mapping function
      Returns:
      the partition of results
    • sorted

      default Partition<K,V> sorted()
      Sorts the partition using the natural ordering of keys.
      Returns:
      the sorted stream
      Throws:
      ClassCastException - if the key type is not Comparable
    • sorted

      default Partition<K,V> sorted(Comparator<K> comparator)
      Sorts the partition based on their keys.
      Parameters:
      comparator - the comparator used to sort keys
      Returns:
      the sorted partition
    • first

      default org.apache.commons.lang3.tuple.Pair<K,V> first()
      Returns the first key-value pair from this partition.
      Returns:
      the selected key-value pair
      Throws:
      NoSuchElementException - if the partition is empty
    • any

      default org.apache.commons.lang3.tuple.Pair<K,V> any()
      Returns any key-value pair from this partition.
      Returns:
      the selected key-value pair
      Throws:
      NoSuchElementException - if the partition is empty
    • singleOrDefault

      default org.apache.commons.lang3.tuple.Pair<K,V> singleOrDefault(K defaultKey, V defaultValue)
      Returns the singular key-value pair contained in this partition, or if empty, returns the given default value.
      Parameters:
      defaultKey - the default key
      defaultValue - the default value
      Returns:
      the single key-value pair or default value
    • single

      default org.apache.commons.lang3.tuple.Pair<K,V> single()
      Asserts this partition contains exactly one key-value pair, returning said item.
      Returns:
      the key-value pair
      Throws:
      NoSuchElementException - if the partition was empty or contained more than one key-value pair
    • filter

      default Partition<K,V> filter(Predicate<K> predicate)
      Filters this partition, keeping only those keys evaluating to true.
      Parameters:
      predicate - the predicate function based on the key
      Returns:
      the resulting partition
    • groupBy

      default <G> Groups<G,K,V> groupBy(Function<K,G> group)
      Applies a grouping function to the keys in this partition. Keys with the same grouping key are grouped together.
      Type Parameters:
      G - the type of the grouping key
      Parameters:
      group - the grouping function
      Returns:
      the resulting groups
    • reduce

      default V reduce(BinaryOperator<V> op)
      Applies a binary reduction operator to the values in this partition. See Stream.reduce(BinaryOperator) for more details.
      Parameters:
      op - the binary reduction operator
      Returns:
      the final result from the reduction operator
      Throws:
      NoSuchElementException - if the partition is empty
    • reduce

      default V reduce(V identity, BinaryOperator<V> op)
      Applies a binary reduction operator to the values in this partition. See Stream.reduce(Object, BinaryOperator) for more details.
      Parameters:
      identity - the initial value supplied to the binary operator
      op - the binary reduction operator
      Returns:
      the final result from the reduction operator
    • distinct

      default Partition<K,V> distinct()
      Retains only the unique key-value pairs in this partition.
      Returns:
      the resulting partition
    • measure

      default <R> R measure(Function<Stream<V>,R> measure)
      Applies a measurement function to the values in this partition.
      Type Parameters:
      R - the return value
      Parameters:
      measure - the measurement function
      Returns:
      the measured value
    • forEach

      default void forEach(BiConsumer<K,V> consumer)
      Invokes a method for each key-value pair in this partition.
      Parameters:
      consumer - the method to invoke
    • enumerate

      default void enumerate(BiConsumer<Integer,org.apache.commons.lang3.tuple.Pair<K,V>> consumer)
      Similar to forEach(BiConsumer) except the index is included.
      Parameters:
      consumer - the method to invoke
    • asTabularData

      default TabularData<org.apache.commons.lang3.tuple.Pair<K,V>> asTabularData()
      Description copied from interface: Formattable
      Returns the contents of this object as a TabularData instance, which can be used to save, print, or format the data in various ways.
      Specified by:
      asTabularData in interface Formattable<K>
      Returns:
      the TabularData instance
    • of

      static <K, V> Partition<K,V> of()
      Creates an empty partition.
      Type Parameters:
      K - the type of the keys
      V - the type of the values
      Returns:
      the constructed partition
    • of

      static <V> Partition<V,V> of(Stream<V> stream)
      Creates a partition with the contents of a Stream. The values are also used as the keys.
      Type Parameters:
      V - the type of the stream
      Parameters:
      stream - the source stream
      Returns:
      the constructed partition
    • of

      static Partition<Integer,Integer> of(IntStream stream)
      Creates a partition with the contents of a IntStream. The values are also used as the keys.
      Parameters:
      stream - the source stream
      Returns:
      the constructed partition
    • of

      static Partition<Double,Double> of(DoubleStream stream)
      Creates a partition with the contents of a DoubleStream. The values are also used as the keys.
      Parameters:
      stream - the source stream
      Returns:
      the constructed partition
    • of

      static <V> Partition<V,V> of(Iterable<V> iterable)
      Creates a partition with the contents of an Iterable. The values are also used as the keys.
      Type Parameters:
      V - the type of the iterable
      Parameters:
      iterable - the iterable
      Returns:
      the constructed partition
    • of

      static <V> Partition<V,V> of(V[] array)
      Creates a partition with the contents of an array. The values are also used as the keys.
      Type Parameters:
      V - the type of the array
      Parameters:
      array - the array
      Returns:
      the constructed partition
    • of

      static <K, V> Partition<K,V> of(Map<K,V> map)
      Creates a partition with the contents of a Map.
      Type Parameters:
      K - the type of the keys
      V - the type of the values
      Parameters:
      map - the source map
      Returns:
      the constructed partition
    • of

      static <K, V> Partition<K,V> of(Function<V,K> key, Stream<V> stream)
      Creates a partition with the contents of a Stream.
      Type Parameters:
      K - the type of the keys
      V - the type of the values
      Parameters:
      key - the function returning the key for each value
      stream - the source stream
      Returns:
      the constructed partition
    • of

      static <K> Partition<K,Integer> of(Function<Integer,K> key, IntStream stream)
      Creates a partition with the contents of a IntStream.
      Type Parameters:
      K - the type of the keys
      Parameters:
      key - the function returning the key for each value
      stream - the source stream
      Returns:
      the constructed partition
    • of

      static <K> Partition<K,Double> of(Function<Double,K> key, DoubleStream stream)
      Creates a partition with the contents of a DoubleStream.
      Type Parameters:
      K - the type of the keys
      Parameters:
      key - the function returning the key for each value
      stream - the source stream
      Returns:
      the constructed partition
    • of

      static <K, V> Partition<K,V> of(Function<V,K> key, Iterable<V> iterable)
      Creates a partition with the contents of an Iterable.
      Type Parameters:
      K - the type of the keys
      V - the type of the values
      Parameters:
      key - the function returning the key for each value
      iterable - the source iterable
      Returns:
      the constructed partition
    • of

      static <K, V> Partition<K,V> of(Function<V,K> key, V[] array)
      Creates a partition with the contents of an array.
      Type Parameters:
      K - the type of the keys
      V - the type of the values
      Parameters:
      key - the function returning the key for each value
      array - the source array
      Returns:
      the constructed partition
    • zip

      static <K, V> Partition<K,V> zip(Iterable<K> keys, Iterable<V> values)
      Creates a partition by "zipping" together two iterables.
      Type Parameters:
      K - the type of the keys
      V - the type of the values
      Parameters:
      keys - the iterable of keys
      values - the iterable of values
      Returns:
      the constructed partition
    • zip

      static <K, V> Partition<K,V> zip(K[] keys, V[] values)
      Creates a partition by "zipping" together two arrays.
      Type Parameters:
      K - the type of the keys
      V - the type of the values
      Parameters:
      keys - the array of keys
      values - the array of values
      Returns:
      the constructed partition