Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we use Optionals in Android programming?

I have been reading about the Optional type in Java 8. I want to implement in some of my Android code.

But does the Dalvik machine for the latest versions of Android (5.1 and 6) implement everything in Java 8?

I'm still running Java 7 with the Eclipse and Android Studio installed on my computers.

like image 563
Jungle Jim Avatar asked Oct 30 '15 03:10

Jungle Jim


2 Answers

You can use StreamSupport lib which adds support for the new Streams API and as a side effect it has Optional.

For gradle use:

compile "net.sourceforge.streamsupport:streamsupport:1.5.1"
like image 75
Ognyan Avatar answered Oct 08 '22 15:10

Ognyan


Yes. But only on API level 24 / Nougat (in current time of writing).

For pre-Nougat, you may opt to these libraries:

  • Options (https://github.com/tomaszpolanski/Options)
  • Arrow (https://github.com/android10/arrow)
  • StreamSupport (https://github.com/streamsupport/streamsupport)
  • Optional (https://github.com/MrHadiSatrio/Optional)
  • Guava (https://github.com/google/guava)
like image 33
aldok Avatar answered Oct 08 '22 16:10

aldok