{"id":16104870,"url":"https://github.com/dbc2201/streamsinjava","last_synced_at":"2025-07-17T12:35:10.659Z","repository":{"id":133011943,"uuid":"317618070","full_name":"dbc2201/StreamsInJava","owner":"dbc2201","description":null,"archived":false,"fork":false,"pushed_at":"2020-12-01T18:48:27.000Z","size":19,"stargazers_count":1,"open_issues_count":0,"forks_count":16,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-13T05:42:16.327Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dbc2201.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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,"zenodo":null}},"created_at":"2020-12-01T17:31:01.000Z","updated_at":"2021-08-28T13:59:24.000Z","dependencies_parsed_at":null,"dependency_job_id":"70c004dd-db7f-441a-a640-460a8dda52d5","html_url":"https://github.com/dbc2201/StreamsInJava","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/dbc2201/StreamsInJava","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dbc2201%2FStreamsInJava","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dbc2201%2FStreamsInJava/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dbc2201%2FStreamsInJava/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dbc2201%2FStreamsInJava/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dbc2201","download_url":"https://codeload.github.com/dbc2201/StreamsInJava/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dbc2201%2FStreamsInJava/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265606854,"owners_count":23797024,"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":[],"created_at":"2024-10-09T19:06:47.195Z","updated_at":"2025-07-17T12:35:10.653Z","avatar_url":"https://github.com/dbc2201.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"Functional Programming in Java (8 or above) using The Stream API\n---\n_The Stream API_ and _The Lambda Expressions_ are two of the most important features\nthat have been added to the Java language in a while (2014).  \n\n_The Stream API_ provides methods that can perform \nvery sophisticated operations like\n- search\n- filter\n- map  \nor otherwise manipulate a set of data (or a large/very large set of data) easily.\n\n_The Stream API_ can also perform tasks \"in parallel\" thus making the operations\nmore efficient than the traditional approaches that we have learnt until now.\n\n---\n**CAUTION!!!**  \nTo properly understand the Stream API, you need to be able to \nwrap your head around complicated topics like\n1. Generics and Type Erasure in Java\n2. Lambda Expressions and Functional Interfaces in Java\n3. Parallel Execution of programs\n4. The Collections Framework in Java\n---\n\n## Stream API in the JDK\n_The Stream API_ defines quite some _Interfaces/Classes/Abstract Classes_ in the Java JDK \nwhich can be observed in the `java.util.stream` package.  \nJust navigate to the `java.base` module, and the follow the `java.util.stream` package.\n\n---\n\n## The `BaseStream` Interface  \nThe `java.util.stream.BaseStream` interface defines the functionality available in all streams.\nIt is a generic interface which is declared something like this:\n```\ninterface BaseStream\u003cT, S extends BaseStream\u003cT, S\u003e\u003e {\n// ...\n}\n```\n\n---\n\nThe very first thing to learn about the Stream API is\nthat is consists of three basic things\n1. A \"Data Source\" from where the data flows in.\n2. One or more intermediate operations. An intermediate operation produces a separate stream to perfom \nthe operation, it can also be used to create a \"pipeline\" to perform\na number of operations on the stream.\n3. An optional terminal operation. A terminal operation\n\"consumes\" the stream i.e once a stream is \"consumed\", it can't be reused.\nMore about that later...\n\n---\n\nFirst let us see how we can create a simple Stream in a Java program.\n\nTo Create a \"Stream\" in Java, we can use the 'java.util.stream.Stream' interface.\nSince this interface is a Generic Type, we can actually use it to create a stream\nof any \"reference\" type in Java (pre-defined or programmer-defined).\n\nTo handle the Stream(s) of primitive type in Java, we have the following interfaces\n* IntStream\n* DoubleStream\n* and LongStream  \nall defined in the same 'java.util.stream' package.\n---\n\nLet us see an example, we will use the `IntStream` interface's `range()` to create\na range of integers in a stream.\n\n```\nimport java.util.stream.IntStream;\n\nclass Scratch {\n    public static void main(String[] args) {\n        var range = IntStream.range(1, 10);\n        // In the range() method, the first argument is included in the range \n        // but the second number is excluded. \n    }\n}\n```\nSimilarily we can create ranges with the `LongStream` interface and the `DoubleStream` interface as well.\n\n---\n\nLet us see how we can print all the numbers in this range now.  \nSee [here](/src/examples/Example2.java).  \nIt does print the numbers in the range, but the output is a little wacky right?  \nNo spaces between the numbers makes it look like just one number with a lot of digits!  \nLet's fix that!  \n[here](/src/examples/Example3.java)\nWoah nice! Lambda Expressions!  \nSince the Stream API was built while keeping Lambda Expressions in mind\nit is able to work with Lambdas freely! \n\n---\n\nAs we have seen, the Stream API is able to benefit from the \ngreat deal of features available in the JDK, we can also use streams\nto work with collections of some data with the Collections Framework.  \n\nSo, we can use a collection in our program to create a stream.\nAlso, we can use an array to create a stream by using the `Arrays.stream()` method.\n\n---\n\n### Creating a stream from a collection as the data source\n\nLet us see an example where we can create a stream with a collection as the data source.\n[StreamCollectionExample](/src/examples/Example4.java)\n\n---\n\n### Creating a stream from an array as the data source\n[StreamArrayExample](/src/examples/Example5.java)\n\n---\n\nNow, let us perform some operations on the stream!\n[SimpleStreamOperations](/src/examples/Example1.java)\n ","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdbc2201%2Fstreamsinjava","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdbc2201%2Fstreamsinjava","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdbc2201%2Fstreamsinjava/lists"}