Problems

Use the following problems to test your Optional programming prowess. I strongly encourage you to give each problem a try before you turn to the solutions and download the example programs:

  1. Initializing Optional: Write a program that exemplifies the right and wrong approaches for initializing Optional.
  2. Optional.get() and missing value: Write a program that exemplifies the right and wrong usage of Optional.get().
  1. Returning an already-constructed default value: Write a program that, when no value is present, sets (or returns) an already-constructed default value via the Optional.orElse() method.
  2. Returning a non-existent default value: Write a program that, when no value is present, sets (or returns) a non-existent default value via the Optional.orElseGet() method.
  3. Throwing NoSuchElementException: Write a program that, when no value is present, throws an exception of the NoSuchElementException type or another exception.
  4. The Optional and null references: Write a program that exemplifies the correct usage of Optional.orElse(null).
  5. Consuming a present Optional class: Write a program that consumes a present Optional class via ifPresent() and via ifPresentElse().
  6. Returning a present Optional class or another one: Let's assume that we have Optional. Write a program that relies on Optional.or() for returning this Optional (if its value is present) or another Optional class (if its value is not present).
  7. Chaining lambdas via orElseFoo(): Write a program that exemplifies the usage of orElse() and orElseFoo() for avoiding disrupting lambda chains.
  8. Do not use Optional just for getting a value: Exemplify the bad practice of chaining the Optional methods with the single purpose of getting some values.
  9. Do not use Optional for fields: Exemplify the bad practice of declaring fields of the Optional type.
  10. Do not use Optional in constructor args: Exemplify the bad practice of using Optional in constructors arguments.
  11. Do not use Optional in setters args: Exemplify the bad practice of using Optional in setter arguments.
  12. Do not use Optional in methods args: Exemplify the bad practice of using Optional in method arguments.
  13. Do not use Optional to return empty or null collections or arrays: Exemplify the bad practice of using Optional for returning the empty/null collections or arrays.
  1. Avoiding Optional in collections: Using Optional in collections can be a design smell. Exemplify a typical use case and possible alternatives for avoiding Optional in collections.
  2. Confusing of() with ofNullable(): Exemplify the potential consequences of confusing Optional.of() with ofNullable().
  3. Optional<T> versus OptionalInt: Exemplify the usage of non-generic OptionalInt instead of Optional<T>.
  4. Asserting equality of Optional classes: Exemplify asserting the equality of Optional classes.
  5. Transforming values via map() and flatMap(): Write several snippets of code for exemplifying the usage of Optional.map() and flatMap().
  6. Filter values via Optional.filter(): Exemplify the usage of Optional.filter() for rejecting wrapped values based on a predefined rule.
  7. Chaining the Optional and Stream APIs: Exemplify the usage of Optional.stream() for chaining the Optional API with the Stream API.
  8. Optional and identity-sensitive operations: Write a snippet of code that sustains the fact that identity-sensitive operations should be avoided in the case of Optional.
  9. Return  boolean if Optional is empty: Write two snippets of code for exemplifying two solutions for returning boolean if the given Optional class is empty.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.51.228