I'd like to start off by saying this is a little more of a general question; not one pertaining to the specific examples that I have given, but simply a conceptual topic.
Example #1: I'm creating a truly random string with UUID.java. Let's say I never want to have the same UUID generated, ever. Here's an idea of the circumstance: (Let's assume that I'm saving/loading the List at the top- that's not the point)
Gist URL (I'm new to StackExchange- sorry!)
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class Example {
/**
* A final List<String> of all previous UUIDs generated with
* generateUniqueID(), turned into a string with uuid.toString();
*/
private static final List<String> PREVIOUS = new ArrayList<String>();
/**
* Generates a truly unique UUID.
*
* @param previous
* A List<String> of previous UUIDs, converted into a string with
* uuid.toString();
* @return a UUID generated with UUID.randomUUID(); that is not included in
* the given List<String>.
*/
public static UUID generateUniqueID(List<String> previous) {
UUID u = UUID.randomUUID();
if (previous.contains(u.toString())) {
return generateUniqueID(previous);
}
return u;
}
/**
* Generates a truly unique UUID using the final List<String> PREVIOUS
* variable defined at the top of the class.
*
* @return A truly random UUID created with generateUniqueID(List<String>
* previous);
*/
public static UUID generateUniqueID() {
UUID u = generateUniqueID(PREVIOUS);
PREVIOUS.add(u.toString());
return u;
}
}
Example #2: Okay, maybe UUID was a bad example, so let's use Random and a double. Here's another example:
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Example2 {
/**
* A final List<Double> of all previous double generated with
* generateUniqueDouble(), turned into a string with Double.valueOf(d);
*/
private static final List<Double> PREVIOUS = new ArrayList<Double>();
/**
* The RANDOM variable used in the class.
*/
private static final Random RANDOM = new Random();
/**
* Generates a truly unique double.
*
* @param previous
* A List<Double> of previous doubles, converted into a Double
* with Double.valueOf(d);
* @return a UUID generated with UUID.randomUUID(); that is not included in
* the given List<Double>.
*/
public static double generateUniqueDouble(List<Double> previous) {
double d = RANDOM.nextDouble();
if (previous.contains(Double.valueOf(d))) {
return generateUniqueDouble(previous);
}
return d;
}
/**
* Generates a truly unique double using the final List<Double> PREVIOUS
* variable defined at the top of the class.
*
* @return A truly random double created with generateUnique(List<Double>
* previous);
*/
public static double generateUnique() {
double d = RANDOM.nextDouble();
PREVIOUS.add(Double.valueOf(d));
return d;
}
}
The point: Is this the most efficient method of doing something like this? Keep in mind I gave you examples, so they're pretty vague. Preferrably I wouldn't like to use any libraries for this, but if they really are a substantial difference in efficency please let me know about them.
Please let me know what you think in the responses :)