{"id":26096306,"url":"https://github.com/echocat/java-stream-utils","last_synced_at":"2025-10-24T12:56:58.538Z","repository":{"id":57745495,"uuid":"91347317","full_name":"echocat/java-stream-utils","owner":"echocat","description":"Utilities for handling Java Streams.","archived":false,"fork":false,"pushed_at":"2025-03-18T07:31:41.000Z","size":173,"stargazers_count":3,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-26T06:34:29.145Z","etag":null,"topics":["batch","java","java-8","jdbc","streams","until","utils"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/echocat.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-05-15T14:27:51.000Z","updated_at":"2025-03-18T07:31:44.000Z","dependencies_parsed_at":"2023-11-14T17:32:30.272Z","dependency_job_id":"28446ae1-cbf1-462b-abf8-52045e279f36","html_url":"https://github.com/echocat/java-stream-utils","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/echocat%2Fjava-stream-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/echocat%2Fjava-stream-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/echocat%2Fjava-stream-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/echocat%2Fjava-stream-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/echocat","download_url":"https://codeload.github.com/echocat/java-stream-utils/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248564451,"owners_count":21125407,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["batch","java","java-8","jdbc","streams","until","utils"],"created_at":"2025-03-09T14:44:57.980Z","updated_at":"2025-10-12T08:04:04.964Z","avatar_url":"https://github.com/echocat.png","language":"Java","readme":"# Utils for Java Streams\n\nProvides utils to deal with Java Streams. Especially add missing features like `takeWhile` and `batch`.\n\n## Topics\n\n* [Features](#features)\n* [Getting Started](#getting-started)\n* [FAQ](#faq)\n* [Contributing](#contributing)\n* [License](#license)\n\n## Features\n\n* [Stream `takeWhile`](#stream-takewhile)\n* [Stream `generate`](#stream-generate)\n* [Stream `batch`](#stream-batch)\n* [ResultSet `toStream`](#resultset-tostream)\n* [ResultSet `toStream` with mapper](#resultset-tostream-with-mapper)\n\n### Stream `takeWhile`\n\n\u003e This is one of the missing features of Java 8 streaming API.\n\nContinue a stream only until all elements matches a specific predicate.\n\nReference: [`\u003cT\u003e Stream\u003cT\u003e StreamUtils.takeWhile(Stream\u003cT\u003e input, Predicate\u003cT\u003e predicate)`](/src/main/java/org/echocat/jsu/StreamUtils.java)\n\nExample:\n```java\n// Prints out random numbers to stdout until 66 was generated\n// 66 will not appear in the stdout.\nRandom random = new Random();\nStream\u003cInteger\u003e stream = Stream.generate(() -\u003e random.nextInt(100));\nStreamUtils.takeWhile(stream, number -\u003e number != 66)\n    .forEach(System.out::println);\n```\n\n### Stream `generate`\n\n\u003e This is one of the missing features of Java 8 streaming API.\n\nGenerate a stream and decide by your own when it should be done.\nThe regular Streaming API could only produce endless streams.\nA solution could be our [Stream `takeWhile`](#stream-takewhile) but our `generate` is sometimes a smarter solution.\n\nReference: [`\u003cT\u003e Stream\u003cT\u003e StreamUtils.generate(Stream\u003cT\u003e input, Predicate\u003cT\u003e predicate)`](/src/main/java/org/echocat/jsu/StreamUtils.java)\n\nExample:\n```java\n// Prints out random numbers to stdout until 66 was generated\n// 66 will not appear in the stdout.\nRandom random = new Random();\nStreamUtils.generate(() -\u003e {\n    int candidate = random.nextInt(100);\n    return candidate == 66 ? Value.end() : Value.valueOf(candidate);\n}).forEach(System.out::println);\n```\n\n### Stream `batch`\n\n\u003e This is one of the missing features of Java 8 streaming API.\n\nSlice a stream with an unknown length in batches with a fixed size.\n\nPerfect for example to query a batch of elements from a database to do with all of their IDs another\nbatch query to the database to enrich them with more information.\n\nReference: [`\u003cT\u003e Stream\u003cT\u003e StreamUtils.batch(Stream\u003cT\u003e input, int batchSize)`](/src/main/java/org/echocat/jsu/StreamUtils.java)\n\nExample:\n```java\n// Prints out random numbers to stdout in ten blocks until 66 was generated\n// 66 will not appear in the stdout.\nRandom random = new Random();\nStream\u003cInteger\u003e stream = StreamUtils.generate(() -\u003e {\n    int candidate = random.nextInt(100);\n    return candidate == 66 ? Value.end() : Value.valueOf(candidate);\n});\nStreamUtils.batch(stream, 10)\n    .forEach(System.out::println);\n```\n\nHow to integrate a batch back again into a single stream?\n```java\nStream\u003cInteger\u003e sourceStream = ...;\nStream\u003cList\u003cInteger\u003e\u003e batchedStream = StreamUtils.batch(sourceStream, 10);\nStream\u003cInteger\u003e enrichedStream = batchedStream.flatMap(batch -\u003e {\n    // Do something with the batch...\n    ...\n    // Flat map all elements again into a single element stream...\n    return batch.stream(); \n});\nenrichedStream.forEach(System.out::println);\n```\n\n### ResultSet `toStream`\n\n\u003e This is one of the missing features of JDBC API.\n\nSimply converts a `java.sql.ResultSet` into a `java.util.stream.Stream` of ResultSets.\nFor every element in this stream `ResultSet.next()` will be called\n... which means every element in this stream represents a row.\n\nReference: [`Stream\u003cResultSet\u003e JdbcUtils.toStream(ResultSet resultSet)`](/src/main/java/org/echocat/jsu/JdbcUtils.java)\n\nExample:\n```java\n// Prints of every row the first column as string to stdout.\n[..]\nStream\u003cResultSet\u003e stream = JdbcUtils.toStream(resultSet);\nstream\n    .forEach(row -\u003e System.out.println(row.getString(1)));\n```\n\n### ResultSet `toStream` with mapper\n\nIs similar to the [`toStream`](#resultset-tostream) version of above but also includes already mapping functionality\nwhich also handles transparent `java.sql.SQLException`s which means: You are not always required\nto implement annoying `try {..} catch {..}` blocks for you mappings.\n\nReference: [`\u003cT\u003e Stream\u003cT\u003e JdbcUtils.toStream(ResultSet resultSet, SqlFunction\u003cResultSet, T\u003e mapper)`](/src/main/java/org/echocat/jsu/JdbcUtils.java)\n\nExample:\n```java\n// Prints of every row the first column as string to stdout.\n[..]\nStream\u003cString\u003e stream = JdbcUtils.toStream(resultSet, row -\u003e row.getString(1));\nstream\n    .forEach(System.out::println));\n```\n\n## Getting started\n\n### Dependency\n\n#### 1. Register our repository (optional)\n\nYou can directly register our repository if you want always the latest version. The central can be versions behind.\n\n##### Maven\n\n```xml\n\u003crepositories\u003e\n    \u003crepository\u003e\n        \u003cid\u003ecentral\u003c/id\u003e\n        \u003curl\u003ehttps://repo.maven.apache.org/maven2\u003c/url\u003e\n    \u003c/repository\u003e\n    \u003crepository\u003e\n        \u003cid\u003eechocat\u003c/id\u003e\n        \u003curl\u003ehttps://packages.echocat.org/maven\u003c/url\u003e\n    \u003c/repository\u003e\n\u003c/repositories\u003e\n```\n\n##### Gradle\n\n```groovy\nrepositories {\n    mavenCentral()\n    maven {\n        url \"https://packages.echocat.org/maven\"\n    }\n}\n```\n\n#### 2. Pick your version\n\n[![Maven Central](https://maven-badges.herokuapp.com/maven-central/org.echocat.java-stream-utils/java-stream-utils/badge.svg)](https://maven-badges.herokuapp.com/maven-central/org.echocat.java-stream-utils/java-stream-utils)\n\nFind your desired version you want to install (usually the latest one) [by looking it up in our repository](https://github.com/echocat/java-stream-utils/packages/) or directly at [the Maven Central](http://search.maven.org/#search|ga|1|g:org.echocat.java-stream-utils%20AND%20a:java-stream-utils).\n\n#### 3. Add the dependency\n\n##### Maven\n\n```xml \n\u003cdependency\u003e\n    \u003cgroupId\u003eorg.echocat.java-stream-utils\u003c/groupId\u003e\n    \u003cartifactId\u003ejava-stream-utils\u003c/artifactId\u003e\n    \u003cversion\u003e\u003c!-- THE VERSION --\u003e\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n##### Gradle\n\n```groovy\ncompile 'org.echocat.java-stream-utils:java-stream-utils:\u003cTHE VERSION\u003e'\n```\n\n## FAQ\n\n### How to enable streaming result sets?\n\nThe MySQL JDBC driver by default stores at first the whole result of the database in the memory before\nit will be processed. This delivers in many cases with small result sets a better performance instead\nstreaming it one by one. But this is very memory consuming and slow for large result sets - and also\ndangerous - it may cause `OutOfMemoryError`s.\n\nYou can do the following thing to enable the streaming results (works for MySQL):\n```java\nStatement statement = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)\nstatement.setFetchSize(Integer.MIN_VALUE);  \n```\n\n## Contributing\n\njava-stream-utils is an open source project of [echocat](https://echocat.org). So if you want to make this project even better, you can\ncontribute to this project on [Github](https://github.com/echocat/java-stream-utils) by\n[fork us](https://github.com/echocat/java-stream-utils/fork).\n\nIf you commit code to this project you have to accept that this code will be released under the [license](#license) of this project.\n\n## License\n\nSee [LICENSE](LICENSE) file.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fechocat%2Fjava-stream-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fechocat%2Fjava-stream-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fechocat%2Fjava-stream-utils/lists"}