Question

As the question says, I've got multiple finals strings in a final class like this:

public final class MyStrings {

    public static final String stringOne = "this is string one";
    public static final String stringTwo = "this is string two";
    public static final String stringThree = "this is string three";
    public static final String stringFour = "this is string four";
    public static final String stringFive = "this is string five";

}

I know it could be easier to put them in a list or an array, but I would like to avoid runtime filling of those structures. Is it possible? What is the simplest way to choose randomly a string from that class? Thank you.

Was it helpful?

Solution

Your Strings seem very related, since you have a use case that needs to randomly choose one among them. Therefore, they should probably be gathered in an array anyway (on a semantic point of view).

There won't be more "runtime filling" than with your current multiple fields:

public final class MyStrings {

    public static final String[] strings = {
        "this is string one",
        "this is string two",
        "this is string three",
        "this is string four",
        "this is string five"
    };
}

Then you can pick one String randomly this way:

Random random = new Random();
String randomString = strings[random.nextInt(strings.length)]);

OTHER TIPS

String[] mystring = {
    "this is string one",
    "this is string two",
    "this is string three",
    "this is string four",
    "this is string five"
};

int idx = new Random().nextInt(mystring.length);
String random = (mystring [idx]);

Put those strings into final array and generate a random number from 0 to array.length-1

You shouldn't worry about the performance cost of filling an array with those strings, as it's by far the most logical way of doing this!

Load each string into an array and generate a random number from 0 (array indexes start at 0, be careful!) to the end of the array - 1 (because the array starts at 0, you have to minus 1 from the length of the array). And then use the random number as an index for choosing a string.

Use an enum:

public enum MyStrings {
    ONE("This is string one"),
    ETCETERA("Other strings");

    private final String label;

    private MyStrings(String label) {
        this.label = label;
    }
}

then

String randomLabel = MyStrings.values()[random.nextInt(MyStrings.values().length].label;

This has 2 advantages:

  1. it allows you to delay turning the enum values into Strings (possibly making type signatures in the rest of the code more helpful).
  2. it prevents you from being able to replace the array values with other strings.

Performance caveat: this approach does have the slight performance impact of creating a new (short) array in memory every time MyStrings.values() is called. That is unlikely to have any measurable impact. In the unlikely case that you do measure that this is a bottleneck (you'd probably want to use a custom random function, then, too), you can either optimize this away or choose a different structure after all.

use reflection api to get all public static final String fields from MyStrings.class into an array, called strings

then randomly generate an integer n from 0 to numStrings - 1,

random string is strings[n]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top