An efficient way to shuffle a JSON array in java?
Which would be the best way to do it? Right now, I convert my JSONArray
to an ArrayList
of a custom class, use Collections.shuffle()
to perform the action, and convert back to JSONArray
, which seems to be too much overhead.
The answer may开发者_开发问答 be just to implement a Fisher-Yates shuffle for it, but my guess is that this may be already done so I would like to avoid reinventing the wheel. I looked at the standard JSON api and Google's Gson but they don't seem to have any implementation.
There are also simple options for a standard array in this question that could be easily ported to java, but I would gladly hear your input. I am amazed that the query http://www.google.com/search?q=java+shuffle+jsonarray did not flood me with methods.
Sorry for posting an answer to my own question, but right now, since there was no out-of-the-box quick solution, I'm implementing my own static shuffle function based on the code from this post: Random shuffling of an array . Still looking forward to hear about the best implementation. This is what I did:
public static JSONArray shuffleJsonArray (JSONArray array) throws JSONException {
// Implementing Fisher–Yates shuffle
Random rnd = new Random();
for (int i = array.length() - 1; i >= 0; i--)
{
int j = rnd.nextInt(i + 1);
// Simple swap
Object object = array.get(j);
array.put(j, array.get(i));
array.put(i, object);
}
return array;
}
Your method works great, but don't forget to:
rnd.setSeed(System.currentTimeMillis());
so that the results are unique each time.
Sorry for the new answer, I don't have enough rep to post a comment :/
Use a JSON library that doesn't require you to convert to some kind of JSON data structure when the language already has perfectly good List
and Map
interfaces built in.
http://code.google.com/p/prebake/source/browse/trunk/code/src/org/prebake/js/JsonSink.java and http://code.google.com/p/prebake/source/browse/trunk/code/src/org/prebake/js/JsonSource.java for example.
精彩评论