import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
import java.util.Random;
import java.util.stream.IntStream;
class Robot {
private String name = RobotNameGen.GetName();
public void reset() {
name = RobotNameGen.GetName();
}
public String getName() {
return name;
}
static class RobotNameGen {
private static int namesIndex = -1;
private static List < String > names = new ArrayList();
static {
IntStream.rangeClosed('A', 'Z')
.forEach(letter1 -> IntStream.rangeClosed('A', 'Z').forEach(letter2 ->
IntStream.range(0, 1000).forEach(num ->
names.add(String.format("%c%c%03d", (char) letter1, (char) letter2, num)))));
Collections.shuffle(names, new Random(System.currentTimeMillis()));
}
public static String GetName() {
return names.get(++namesIndex);
}
}
}
This approach starts by importing from packages for what will be needed.
The work of generating the names is done by the nested static class RobotNameGen inside the Robot class.
It is static because it doesn't need to differ between object instantiations, so it can belong to the Robot class itself.
It defines an ArrayList to hold the names, defined as private, since it doesn't need to be seen outside the class.
It also defines an index to be used for getting an element from the ArrayList.
A static initialization block is used for populating the ArrayList.
The IntStream.rangeClosed() method is used for generating an ASCII value from A through Z.
In a forEach() method, the first letter is passed to a lambda function which in turn calls IntStream.rangeClosed()
to generate the second letter.
In another forEach(), the second letter is passed in a lambda function that calls IntStream.range() to generate
a number from 0 up to but not including 1000.
Note that the difference between IntStream range() and IntStream rangeClosed() is that the upper bound is exclusive
for range() and is inclusive for rangeClosed().
So, IntStream.range(1, 10) iterates from 1 up to but not including 10,
and IntStream.rangeClosed(1, 10) iterates from 1 through 10.
The number is passed to a lambda which formats the two ASCII values and the number into a string.
The %03d format specifier indicates having leading zeros for a number up to three places,
so the number 1 would be formated as 001 and the number 999, already taking three places, would have no leading zeroes.
The formatted string is added to the ArrayList.
After all possible names are in the ArrayList, the ArrayList is shuffled,
seeding it with a new Random object initialized with the current time in milliseconds.
Whenever a Robot object needs a new name, it calls the RobotNameGen.GetName() method.
The method increments the index and gets the name from the ArrayList at that index.
Since the index is initialized with -1, the first name will be taken from index 0.