Can we use Optionals in Android programming?

柔情痞子 提交于 2019-12-19 15:42:08

问题


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.


回答1:


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"



回答2:


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)



回答3:


I don't think you can do it now or in near future. As you can see in here Android start listing Java 7 as a requirement since Android 5.0 that was released at Nov 2014.
That is 3 years after Java 7 first release, so maybe we can use Java 8 from 2017 ;)

On another note you can use lambda using retrolambda to support lambda in Android dev which is really nice.



来源:https://stackoverflow.com/questions/33428276/can-we-use-optionals-in-android-programming

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!