简体   繁体   中英

What's the easiest way to get a list from a Flowable in java?

My database returns a Flowable like so:

Flowable<List<Users>> users = MyActivity.myDatabase.myDAO().getUsers();

I would like to quickly and easily convert it to a list so I can count it, iterate through it and perhaps pass it as an ArrayList. What's the best way to do that using Java?

You will have to use a blocking call to actually retrieve the values. However, it's not clear from the question whether you are expecting a single list of users or a list of user lists.

If you are expecting a single List<Users> , you can do this:

List<Users> usersList = users.blockingFirst();

If you are expecting a list of user lists, you would need to do something like this:

List<List<Users>> userLists = users.toList().blockingGet();

In either case, if you specifically need an ArrayList , you will have to construct one and copy over the elements, as there is no telling exactly what kind of List implementation the above codes return. (You can just do, for instance, new ArrayList<>(users.blockingFirst()) in the first case and something similar in the second.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM