{
  "id": "accumulateby",
  "title": "AccumulateBy",
  "url": "https://redis.io/docs/latest/operate/oss_and_stack/stack-with-enterprise/deprecated-features/gears-v1/jvm/classes/gearsbuilder/accumulateby/",
  "summary": "Groups records and reduces each group to a single record per group.",
  "tags": [
    "docs",
    "operate",
    "stack"
  ],
  "last_updated": "2026-04-22T11:55:45+02:00",
  "page_type": "content",
  "content_hash": "57b43a574c3fc4d51fb89d30401835846ed853fcfd17857ed2d9b4b7e31aa001",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "[code example]\n\nIterates through the records in the pipe, groups them based on the provided extractor, and then reduces each group to a single record per group with the accumulator function.\n\nThe initial value of the accumulator is null unless you provide a value initializer operation as a parameter."
    },
    {
      "id": "parameters",
      "title": "Parameters",
      "role": "parameters",
      "text": "Type parameters:\n\n| Name | Description |\n|------|-------------|\n| I | The template type of the returned builder |\n\nFunction parameters:\n\n| Name | Type | Description |\n|------|------|-------------|\n| accumulator | <nobr>AccumulateByOperation<T,​I></nobr> | A function with logic to update the accumulator value with each record |\n| extractor | ExtractorOperation<T> | Extracts a specific value from each record |\n| valueInitializer | ValueInitializerOperation<I> | Whenever the accumulated value is null, use this function to initialize it |"
    },
    {
      "id": "returns",
      "title": "Returns",
      "role": "returns",
      "text": "Returns a GearsBuilder object with a new template type."
    },
    {
      "id": "examples",
      "title": "Examples",
      "role": "example",
      "text": "Both of the following examples count the number of unique values.\n\nWithout the `valueInitializer` parameter:\n\n[code example]\n\nWith the `valueInitializer` parameter:\n\n[code example]"
    }
  ],
  "examples": [
    {
      "id": "overview-ex0",
      "language": "java",
      "code": "public <I extends java.io.Serializable> GearsBuilder<I> accumulateBy​(\n\tgears.operations.ExtractorOperation<T> extractor, \n\tgears.operations.AccumulateByOperation<T,​I> accumulator)\n\npublic <I extends java.io.Serializable> GearsBuilder<I> accumulateBy​(\n\tgears.operations.ValueInitializerOperation<I> valueInitializer, \n\tgears.operations.ExtractorOperation<T> extractor, \n\tgears.operations.AccumulateByOperation<T,​I> accumulator)",
      "section_id": "overview"
    },
    {
      "id": "examples-ex0",
      "language": "java",
      "code": "GearsBuilder.CreateGearsBuilder(reader).\n   \taccumulateBy(r->{\n   \t\treturn r.getStringVal();\n   \t},(k, a, r)->{\n   \t\tInteger ret = null;\n   \t\tif(a == null) {\n   \t\t\tret = 0;\n   \t\t}else {\n   \t\t\tret = (Integer)a;\n   \t\t}\n   \t\treturn ret + 1;\n});",
      "section_id": "examples"
    },
    {
      "id": "examples-ex1",
      "language": "java",
      "code": "GearsBuilder.CreateGearsBuilder(reader).\n    \taccumulateBy(()->{\n   \t\treturn 0;\n   \t},r->{\n   \t\treturn r.getStringVal();\n   \t},(k, a, r)->{\n   \t\treturn a + 1;\n   \t});",
      "section_id": "examples"
    }
  ]
}
