December
02
A metaClass example of getting a random arraylist element
Yeah, yeah, OK, I can't name blog post titles very creatively :)
This is simply a repost of a blog post I did a few months ago, but I'm not sure many people here have read my other more generalized blog,
so I'll repost the Groovyish content here now and then.
Someone recently asked how to get a random element from an array. In PHP it’s pretty straightforward - array_rand($x) - but there’s not (AFAICT) a built-in way in Groovy to do this. So I wrote a little something which should hopefully demonstrate the power of the metaClass stuff to boot.
ArrayList.metaClass.getRand = { number ->
if(number==0) {
return delegate[new Random().nextInt(delegate.size)]
} else {
def tempList = []
def counter = 0
while(counter>number) {
tempList.add(delegate[new Random().nextInt(delegate.size)])
counter
}
return tempList
}
}
def names = ['mike','matt','mark','lesley','jean','ron','jeff','martine']
println names.getRand()
println names.getRand(5)
The first call to getRand() on the names list will just return one name from the list. The second will return 5 random entries (and some could theoretically be repeated). I’ve added this getRand() method to the base ArrayList class at runtime, even though it’s likely declared ‘final’ in Java’s base libraries. This is one of the core powers of Groovy, and is pretty slick, imo.
Share and Enjoy:
These icons link to social bookmarking sites where you can share and discover new web sites.
Great minds think alike ;). Here is a class I use as a Category or Mixin for Groovy Lists:
class RandomListUtils {
static def r = new Random()
static def rand(List self) {
self[r.nextInt(self.size())]
}
static def rand(List self, count) {
def newList = self.collect { it }
def result = []
count.times {
def randItem = newList.rand()
result
Note - this blog thing takes < div > tags and such - I do < div style='background-color:#ccc'> around code blocks.
Yeah, my comment was actually cut off somewhere within the code. :(
great information dude, definitely something that is really useful, now I can complete some projects, with this in my hands I have the chance to do something interesting.
I have noticed that over the course of building a relationship with real estate proprietors, you'll be able to get them to understand that, in every real estate deal, a percentage is paid. In the long run, FSBO sellers don't "save" the commission payment. Rather, they struggle to earn the commission through doing a good agent's task. In this, they devote their money along with time to execute, as best they will, the jobs of an representative. Those tasks include getting known the home via marketing, introducing the home to all buyers, making a sense of buyer emergency in order to induce an offer, scheduling home inspections, taking on qualification assessments with the loan provider, supervising maintenance tasks, and assisting the closing of the deal.
Can I simply say what a relief to search out someone who really knows what theyre talking about on the internet. You positively know how you can carry a difficulty to mild and make it important. Extra folks need to learn this and perceive this aspect of the story. I cant imagine youre not more fashionable because you undoubtedly have the gift.
I was curious if you ever considered changing the structure of your site? Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so people could connect with it better. Youve got an awful lot of text for only having one or 2 pictures. Maybe you could space it out better?
I haven’t checked in here for a while since I thought it was getting boring, but the last few posts are good quality so I guess I’ll add you back to my daily bloglist. You deserve it friend :)
Add a comment