13

Groovy is extremely powerful managing collections. I have a list like this one:

def nameList = ["Jon", "Mike", "Alexia"]

What I am trying to do is iterating 10 times to get ten people with a random name from the first list.

10.times{
    Person person = new Person(
    name: nameList.get() //I WANT TO GET A RANDOM NAME FROM THE LIST
    )
}

This is not working for two obvious reasons, I am not adding any index in my nameList.get and I am not creating 10 different Person objects.

  1. How can I get a random element from my name list using groovy?
  2. Can I create a list with 10 people with random names (in a simple way), using groovy's collections properties?

1 Answer 1

27

Just use the Java method Collections.shuffle() like

class Person {
    def name
}

def nameList = ["Jon", "Mike", "Alexia"]
10.times {
    Collections.shuffle nameList // or with Groovy 3: nameList.shuffle()
    Person person = new Person(
        name: nameList.first()
    )
    println person.name
}

or use a random index like

class Person {
    def name
}

def nameList = ["Jon", "Mike", "Alexia"]
def nameListSize = nameList.size()
def r = new Random()
10.times {
    Person person = new Person(
        name: nameList.get(r.nextInt(nameListSize))
    )
    println person.name
}
4
  • Isn't the first suggestion a bit expensive?
    – Berg
    Jan 4, 2018 at 16:33
  • 1
    Not really. Just for you I made a quick measure. I commented out the println as it is the same in both and output usually is expensive and increased the 10 to 100_000_000, using System.currentTimeMillis() for measuring the time both variants need. The first one needed 32 seconds, the second one 30 seconds, so not too much of a difference for the iterations imho. Besides that if you need performance, don't use Groovy, but Assembler. ;-) Basically it is is 1. version more object oriented vs 2. version slightly more performant like often. There may even faster ones, but that wasn't asked.
    – Vampire
    Jan 4, 2018 at 19:46
  • 1
    Nah, this first is much slower, regardless of programming language used: If you take a list of 1000 names and run it 100,000 times the first approach takes 3.6 s and the second 0.8. Not a surprise because the first loop has to randomise all elements of the list while the second only one element.
    – Berg
    Jan 5, 2018 at 7:27
  • 1
    Well, as I seid, like often it is more object oriented code vs. more performance. And OP did not say how important performance is, so I provided both alternatives. Besides that, you can also shuffle once and then take one after the other until the collection is exhausted and the shuffle again. But that is slightly different semantic then, as the names will not repeat before the collection is exhausted.
    – Vampire
    Jan 5, 2018 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.