0

I have a RealmResults object. I need to copy one column of it into an other object which supports Collections.shuffle()

Currently I am running a for loop to load an ArrayList object. However, this is taking lot of time, around 1 second which is impacting my app's recyclerview.

Is there any other alternative I can use to load ArrayList? Or anything other than ArrayList which can do the same task in lot less time.

---------------Below code for clarity---------------

mResults = mRealm.where(Quote.class).equalTo("AUTH_TITLE", mAuthorNameReceived).findAll();
        indices = new ArrayList<>();
        for (int i = 0; i < mResults.size(); i++) {
            indices.add(i);
            description.add(mResults.get(i).getPOST_DESCRIPTION());
        }
        Collections.shuffle(description);
jait
  • 93
  • 8
  • 4
    im sure you dont need all 50k items at once – tyczj Apr 07 '16 at 13:51
  • Shuffling a realm list: http://stackoverflow.com/a/36242080/2413303 and picking one random element from realm http://stackoverflow.com/a/33828274/2413303 – EpicPandaForce Apr 07 '16 at 13:52
  • can you share your code please? – Nicolas Filotto Apr 07 '16 at 13:52
  • Wait a second, you're the same person as the one on the shuffling question. Yeah, the solution doesn't scale well for 50000 objects (building a list of 50000 integers still takes lots of memory and time). As long as you can somehow guarantee that your random number isn't picked twice, you can simplify the logic. – EpicPandaForce Apr 07 '16 at 13:54
  • What kind of answers do you expect without posting the code? – tak3shi Apr 07 '16 at 14:43
  • Sure there are many ways to load an ArrayList, but i do not know what you are doing and what type of objects it holds. – tak3shi Apr 07 '16 at 14:49
  • @tyczj I want to load all 50k since I am running a SearchView.OnQueryTextListener on the object. Since the input query has to search among all 50k items, I have to have all of them in an object. – jait Apr 08 '16 at 02:07
  • @EpicPandaForce : Yes, I am the same guy :) and your last answer helped a lot to solve that purpose. In this case, I want to perform a SearchView.OnQueryTextListener on a list of 50k text based items. I'll update my question with the query I am using. – jait Apr 08 '16 at 02:09
  • You shouldn't read the Realm objects to shuffle the description of the Realm objects, shuffle the indices and randomize the access to the Realm... Then access the description through the Realm object marked by the randomized index. – EpicPandaForce Apr 08 '16 at 10:20

1 Answers1

0

It depends on how many of the 50,000 you want to select?

If there are few then a much simpler strategy could be used.


Perhaps pick one random one out of every 500. This would give you 100 kind-of-randomly chosen.

Up-Side: Quick and O(1).

Down-Side: Not really random.


Use a BitSet to keep track of which ones you've already picked and just keep picking at random (marking each one in the BitSet), discarding any already picked, until you've got enough.

Up-Side: Fast for small selections.

Down-Side: Degrades drastically when the number selected approaches all of them. Imagine how long it would take to pick the last one.


Use a database.

Up-Side: Is unlikely to degrade noticeably until many millions of records need to be processed. Down-Side: You need a database.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213