What is Double Brace initialization syntax ({{ ... }}
) in Java?
You can put some Java statements as loop to initialize collection:
List characters = new ArrayList() {
{
for (char c = 'A'; c <= 'E'; c++) add(c);
}
};
Random rnd = new Random();
List integers = new ArrayList() {
{
while (size() < 10) add(rnd.nextInt(1_000_000));
}
};