{"id":25806826,"url":"https://github.com/johannesrabauer/optionals","last_synced_at":"2025-10-15T12:18:16.607Z","repository":{"id":203913391,"uuid":"434349849","full_name":"JohannesRabauer/optionals","owner":"JohannesRabauer","description":"This is my script for the corresponding youtube video.","archived":false,"fork":false,"pushed_at":"2021-12-16T20:07:04.000Z","size":152,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-10T20:19:40.787Z","etag":null,"topics":["introduction","java","optional","tutorial"],"latest_commit_sha":null,"homepage":"https://youtu.be/b-g09rcE0fU","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JohannesRabauer.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}},"created_at":"2021-12-02T19:31:24.000Z","updated_at":"2022-01-30T14:38:56.000Z","dependencies_parsed_at":null,"dependency_job_id":"665a8f07-14a3-4772-af50-6736b9643e67","html_url":"https://github.com/JohannesRabauer/optionals","commit_stats":null,"previous_names":["johannesrabauer/optionals"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/JohannesRabauer/optionals","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JohannesRabauer%2Foptionals","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JohannesRabauer%2Foptionals/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JohannesRabauer%2Foptionals/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JohannesRabauer%2Foptionals/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JohannesRabauer","download_url":"https://codeload.github.com/JohannesRabauer/optionals/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JohannesRabauer%2Foptionals/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279077248,"owners_count":26098234,"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","status":"online","status_checked_at":"2025-10-15T02:00:07.814Z","response_time":56,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["introduction","java","optional","tutorial"],"created_at":"2025-02-27T20:54:07.475Z","updated_at":"2025-10-15T12:18:16.589Z","avatar_url":"https://github.com/JohannesRabauer.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Intro\nHi, i’m Johannes and I’m a software developer. Some time ago, a colleage asked me, why I used a Java class called “Optional”. So I told him and I thought to myself: Johannes, you should tell people about this beautiful piece of code. \nWell, that’s what I’m going to do.\n\n# Overview\nIt’s basically just a container, that holds a single object. And this object can either be null or not null.\n![Sketch of Optional](Sketch.png)\nThe contained Object class is defined through the Generic “T”.\n\nSee [UsageExample](src/main/java/de/johannes_rabauer/optionals/UsageExample.java)\n \n\n# “Why is this useful?” you ask.\nWell it helps to show intent.\nIf you see a Optional-object anywhere, it is clear, that there might be nothing in it. It could be empty or there could be something inside. Either way it is easy to find out and handle it accordingly.\nAnd through this it makes code more readable. And that’s what it’s all about, isn’t it?\n\nSee [Comparison](src/main/java/de/johannes_rabauer/optionals/Comparison.java)\nOf course you can still misuse Optional and return null instead of Optional.empty(), but\nthis would be a very conscious decision for a developer.\n\n# Initialisation\nThere are multiple ways to create an Optional-Object.\nI find the `ofNullable` call to be the most useful for me. You simply call\n`Optional.ofNullable(someObject)`\nand someObject is wrapped in an Optional. Whether it is null or not.\nYou can also create a Not-Null-Optional by calling `Optional.of(someObject)` whereas the someObject must not be null.\nOr you call `Optional.empty()` to create an empty Optional-Instance.\n\n# Further usage\nNow that we have created these Optional-Objects, we can read them in a lot of different ways.\nThe simplest calls are `.get()` and `isEmpty()` which work exactly as one would think.\n`get()` returns the content or, if empty, throws an exception.\n`isEmpty()` tells the caller if there is any content in the Optional or not.\n\nAnother way would be to simply call `.orElse(placeholder)`\nThis way we can get the content of the Optional-Object, or, if it does not have any content, the placeholder object.\nSo we have a very clear and compact way to say: Give me the queried object, or an alternative. \nThis is way better to read then:\n\n\t\tObject result = object;\n\t\tif(result == null)\n\t\t{\n\t\t\tresult = placeholder;\n\t\t}\n\nLastly i want to point out `ifPresent(Conusmer)` where can execute some action,\nonly if there is any content in the Optional-Object.\n\n## Stream\nSince Java 9 the `stream` methode was added. With that you can easily map Optional of a stream.\nSee [BurgerExample](src/main/java/de/johannes_rabauer/optionals/BurgerExample.java)\n\n# What I did\nSounds like fun, right?\nWell, if you are a child at heart, like me, you learn some new technique or feature and everywhere you look, you’ll see use-cases. So that’s what I did.\nI used Optionals as Parameters, as return-values and within methods. I sent Optionals over the networks, I persisted them in databases. \nIt was wonderful. – No, it wasn’t.\nThere are several Problems when using Optionals:\n## Problem 1: Generic\nSee [OverloadExample](src/main/java/de/johannes_rabauer/optionals/OverloadExample.java)\n\nIt’s not possible to overload a method with generics because of type erasure.\n[https://stackoverflow.com/questions/7201231/java-erasure-with-generic-overloading-not-overriding](https://stackoverflow.com/questions/7201231/java-erasure-with-generic-overloading-not-overriding)\n\n## Problem 2: No Serialisation\nSince Optional was only designed to be used as a return value, the java expert group decided to not put any more then the bare necessities in the class. Which excludes being serializable.\n[https://stackoverflow.com/a/24564612/2351407](https://stackoverflow.com/a/24564612/2351407)\n\n# Conclusion\nOptionals can make your code more readable. But there a few banana peels along the way. \n\nThanks for watching and optionally: have fun!","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjohannesrabauer%2Foptionals","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjohannesrabauer%2Foptionals","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjohannesrabauer%2Foptionals/lists"}