Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface ICollectors

Hierarchy

  • ICollectors

Index

Methods

average

  • average<T>(converter?: TypedFunction<T, number>): Collector<T, any, number>
  • Computes the (arithmetic) average of all the collected items, ie. (x_1 + x_2 + ... + x_n)/n, where n is the number of items and the x_i are the individual items. Returns NaN when there are no items.

    factory.stream([]).collect(Collectors.average());
    // => NaN
    
    factory.stream([1, 2, 3]).collect(Collectors.average());
    // => 2
    
    const letterAverage = Collectors.sum(x => x.length);
    factory.stream(["fish", "and", "chips"]).collect(letterAverage);
    // => 4
    

    Type parameters

    • T

      Type of the items to be collected.

    Parameters

    • Optional converter: TypedFunction<T, number>

      An optional converter for turning items into numbers. Default to item => Number(item).

    Returns Collector<T, any, number>

    A collector that converts each item into a number and computes their (arithmetic) mean.

averageGeometrically

  • averageGeometrically<T>(converter?: TypedFunction<T, number>): Collector<T, any, number>
  • Computes the geometric average of all the collected items, ie. (x_1 * x_2 * ... * x_n)/n, where n is the number of items and the x_i are the individual items. Returns NaN when there are no items.

    factory.stream([]).collect(Collectors.averageGeometrically());
    // => NaN
    
    factory.stream([2, 4, 8]).collect(Collectors.averageGeometrically());
    // => 4
    
    const letterAverage = Collectors.averageGeometrically(x => x.length);
    factory.stream(["fish", "is", "allergic"]).collect(letterAverage);
    // => 4
    

    Type parameters

    • T

      Type of the items to be collected.

    Parameters

    • Optional converter: TypedFunction<T, number>

      An optional converter for turning items into numbers. Default to item => Number(item).

    Returns Collector<T, any, number>

    A collector that converts each item into a number and computes their geometric mean.

averageHarmonically

  • averageHarmonically<T>(converter?: TypedFunction<T, number>): Collector<T, any, number>
  • Computes the harmonic average of all the collected items, ie. n/(1/x_1 + 1/x_2 + ... + 1/x_n), where n is the number of items and the x_i are the individual items. Returns NaN when there are no items.

    factory.stream([]).collect(Collectors.averageHarmonically());
    // => NaN
    
    factory.stream([2, 4, 6, 8]).collect(Collectors.averageHarmonically());
    // => 3.84
    
    const letterAverage = Collectors.averageHarmonically(x => x.length);
    factory.stream(["is", "fish", "likely", "allergic"]).collect(letterAverage);
    // => 3.84
    

    Type parameters

    • T

      Type of the items to be collected.

    Parameters

    • Optional converter: TypedFunction<T, number>

      An optional converter for turning items into numbers. Default to item => Number(item).

    Returns Collector<T, any, number>

    A collector that converts each item into a number and computes their harmonic mean.

count

  • count(): Collector<any, any, number>
  • Counts the number of items.

    factory.stream([4,1,0,4]).collect(Collectors.count())
    // => 4
    
    typeparam

    Type of the items to be collected.

    Returns Collector<any, any, number>

    A collector that counts the number of items.

group

  • group<T, K>(classifier: TypedFunction<T, K>): Collector<T, any, Map<K, T[]>>
  • Splits the items into several groups, according to the given classifier.

    factory.stream(["a", "foo", "is", "some", "buzz"]).collect(Collectors.group(x => x.length))
    // => Map [ 1 => ["a"], 2 => ["is"], 3 => "is", 4 => ["some", "buzz"] ]
    
    typearam

    K Type of the group key.

    Type parameters

    • T

      Type of the items to be collected.

    • K

    Parameters

    • classifier: TypedFunction<T, K>

      Returns the group for each item.

    Returns Collector<T, any, Map<K, T[]>>

    A map with the groups as keys and arrays as values, containing all the items for that group.

groupDown

  • groupDown<T, K, A, D>(classifier: TypedFunction<T, K>, downstream: Collector<T, A, D>): Collector<T, any, Map<K, D>>
  • Applies the classifier on items and groups those items into a group for which the classifier returned the same value. Equality is determined by ===. The downstream collector is then applied to the items of each group. The result is a map between the key returned by the classifier and the value returned by the downstream collector.

    factory.stream(factory.times(11)).collect(Collectors.groupDown(x => x % 3, Collectors.sum()))
    // => Map{0: 0+3+6+9, 1: 1+4+7+10, 2: 2+5+8}
    
    // Returns a map between the city and the people living in that city.
    factory.stream(people).groupDown(person => person.city, Collectors.toSet())
    // => Map<City, Set<Person>>
    

    Type parameters

    • T

      Type of the items to be collected.

    • K

      Type of the group key.

    • A

      Type of the intermediate value use by the downstream.

    • D

      Type of the group value.

    Parameters

    • classifier: TypedFunction<T, K>

      Determines the group key of each item. Items with the same group key are grouped together.

    • downstream: Collector<T, A, D>

      Collector that is applied to the items of each group.

    Returns Collector<T, any, Map<K, D>>

    A collector that groups the items and then collects the items of each group.

join

  • join<T>(delimiter?: undefined | string, prefix?: undefined | string, suffix?: undefined | string): Collector<T, any, string>
  • Turns the items into strings and joins these strings together with the given delimiter. Optionally, if prefix or suffix is given, prepends or appends it to the result.

    factory.stream(["foo", "bar"]).collect(Collectors.join())
    // => "foobar"
    
    factory.stream(["foo", "bar"]).collect(Collectors.join(","))
    // => "foo,bar"
    
    factory.stream(["foo", "bar"]).collect(Collectors.join(",", "[", "]"))
    // => "[foo,bar]"
    
    factory.stream([]).collect(Collectors.join(","))
    // => ""
    

    Type parameters

    • T

      Type of the items to be collected.

    Parameters

    • Optional delimiter: undefined | string

      A separator inserted between two items. Default to the empty string.

    • Optional prefix: undefined | string

      If given, it is prepended to the joined result.

    • Optional suffix: undefined | string

      If given, it is appended to the joined result.

    Returns Collector<T, any, string>

    A collector that joins the items into one string with the given delimiter, prefix and suffix.

map

  • map<T, U, A, R>(mapper: TypedFunction<T, U>, downstream: Collector<U, A, R>): Collector<T, any, R>
  • Maps the items with the given mapper and passed them on to the given downstream collector.

    const countLetters = Collectors.map(x => x.length), Collectors.sum();
    factory.stream(["foo", "foobar"]).collect(countLetters)
    // => 9
    

    Type parameters

    • T

      Type of the items to be collected.

    • U

      Type of the mapped items.

    • A

      Type of the intermediate value used by the downstream.

    • R

      Type of the downstream result.

    Parameters

    • mapper: TypedFunction<T, U>

      Mapping function applied to each item.

    • downstream: Collector<U, A, R>

      Collector that takes the mapped items.

    Returns Collector<T, any, R>

    The result of the given downstream collector.

multiply

  • multiply<T>(converter?: TypedFunction<T, number>): Collector<T, any, number>
  • Computes the product of all the collected items, ie. (x_1 * x_2 * ... * x_n), where n is the number of items and the x_i are the individual items. Returns NaN when there are no items.

    factory.stream([]).collect(Collectors.multiply());
    // => NaN
    
    factory.stream([2, 4, 6]).collect(Collectors.multiply());
    // => 48
    
    const multiply = Collectors.multiply(x => x.length);
    factory.stream(["is", "fish", "likely", "allergic"]).collect(multiply);
    // => 384
    

    Type parameters

    • T

      Type of the items to be collected.

    Parameters

    • Optional converter: TypedFunction<T, number>

      An optional converter for turning items into numbers. Default to item => Number(item).

    Returns Collector<T, any, number>

    A collector that converts each item into a number and multiplies them.

partition

  • partition<T>(predicate: Predicate<T>): Collector<T, any, object>
  • Splits the items into two groups according to the given discriminator.

    factory.stream([-3,2,9,-4]).collect(Collectors.partition(x => x > 0))
    // => { false: [-3,-4], true: [2,9]}
    
    factory.stream([-3,2,9,-4]).collect(Collectors.partition(x => x > 0))
    // => { false: [], true: []}
    

    Type parameters

    • T

      Type of the elements in the given iterable.

    Parameters

    • predicate: Predicate<T>

    Returns Collector<T, any, object>

    An object containing the partitioned items.

partitionDown

  • partitionDown<T, A, D>(predicate: Predicate<T>, downstream: Collector<T, A, D>): Collector<T, any, object>
  • Applies the predicate on the items, grouping them in one of two classes according to the result of the predicate. The downstream collector is then applied to the items of each group. The result is an object with the keys false and true and the value as returned by the downstream collector.

    factory.stream(factory.times(11)).collect(Collectors.groupDown(x => x % 2 === 1, Collectors.sum()))
    // => {false: 0+2+4+6+8+10, true: 1+3+5+7+9}
    

    Type parameters

    • T

      Type of the items to be collected.

    • A

      Type of the intermediate value used by the downstream.

    • D

      Type of the group result.

    Parameters

    • predicate: Predicate<T>
    • downstream: Collector<T, A, D>

      Collector that is applied to the items of each group.

    Returns Collector<T, any, object>

    A collector that partitions the items and then collects the items of the true and false group.

sum

  • sum<T>(converter?: TypedFunction<T, number>): Collector<T, any, number>
  • Computes the sum of all the collected items, ie. (x_1 + x_2 + ... + x_n), where n is the number of items and the x_i are the individual items. Returns NaN when there are no items.

    factory.stream([]).collect(Collectors.sum());
    // => NaN
    
    factory.stream([1, 2, 3]).collect(Collectors.sum());
    // => 6
    
    const letterCount = Collectors.sum(x => x.length);
    factory.stream(["fish", "and", "chips"]).collect(letterCount);
    // => 12
    

    Type parameters

    • T

      Type of the items to be collected.

    Parameters

    • Optional converter: TypedFunction<T, number>

      An optional converter for turning items into numbers. Default to item => Number(item).

    Returns Collector<T, any, number>

    A collector that converts each item into a number and computes their sum.

summarize

  • summarize<T>(converter?: TypedFunction<T, number>): Collector<T, any, IStatistics>
  • Computes a statistic for the items, such as the variance and the mean. Returns an instance of IStatistics, see IStatistics for an in-depth description.

    factory.stream([]).collect(Collectors.summarize());
    // => Statistics(count=3, mean=NaN, min=NaN, ...)
    
    factory.stream([1,3,8]).collect(Collectors.summarize());
    // => Statistics(count=3, mean=4, min=1, ...)
    
    const letterAverage = Collectors.summarize(x => x.length);
    factory.stream(["is", "fish", "likely", "allergic"]).collect(letterAverage);
    // => Statistics(count=4, mean=5, min=2, ...)
    

    Type parameters

    • T

      Type of the items to be collected.

    Parameters

    • Optional converter: TypedFunction<T, number>

      An optional converter for turning items into numbers. Default to item => Number(item).

    Returns Collector<T, any, IStatistics>

    A collector that converts each item into a number and computes statistics for these numbers.

toArray

  • toArray<T>(): Collector<T, any, T[]>
  • Collects all items into an array, in the order in which the items are provided.

    factory.stream().toArray()
    // => []
    
    factory.stream("foo").toArray()
    // => ["f", "o", "o"]
    

    Type parameters

    • T

      Type of the items to be collected.

    Returns Collector<T, any, T[]>

    A collector that adds all items into an array.

toMap

  • toMap<T, K, V>(keyMapper: TypedFunction<T, K>, valueMapper: TypedFunction<T, V>, merger?: BinaryOperator<V>): Collector<T, any, Map<K, V>>
  • Collects all items into a map. An item is added to the map with the key as returned by the key mapper and the value as returned by the value mapper.

    When two items have the same key, the optional merge function is called with the two items and the result of the merge function is used as the value for that key. If no merge function is given, the value of the item encountered first is taken.

    factory.stream().toMap(x=>x, x=>x)
    // => Map()
    
    const data = [{id: 0, name: "foo"}, {id: 0, name: "bar"}];
    
    factory.stream(data).toMap(x => x.id, x => x.name)
    // => Map[ 0 => "bar" ]
    
    factory.stream(data).toMap(x => x.id, x => x.name, (name1, name2) => name1 + "," + name2)
    // => Map[ 0 => "foo,bar" ]
    

    Type parameters

    • T

      Type of the items to be collected.

    • K

      Type of the key of the map.

    • V

      Type of the value of the map.

    Parameters

    • keyMapper: TypedFunction<T, K>

      Takes an item and returns the key for that item.

    • valueMapper: TypedFunction<T, V>

      Takes an items and returns the value for that item.

    • Optional merger: BinaryOperator<V>

      A merge function called when two items map to the same key and returns the merged value. Called with two items having the same key, the first argument is the item encountered first in the stream.

    Returns Collector<T, any, Map<K, V>>

    A collector that adds all items to a Map with the key and value as computed by the key and value mapper.

toSet

  • toSet<T>(): Collector<T, any, Set<T>>
  • Collects all items into a set. Duplicate items are ignored.

    factory.stream().toSet()
    // => Set()
    
    factory.stream("foo").toSet()
    // => Set("f", "o")
    

    Type parameters

    • T

      Type of the items to be collected.

    Returns Collector<T, any, Set<T>>

    A collector that adds all items to a Set.

Generated using TypeDoc