Closures with CollectionUtils
Here's a pretty nifty trick for using closure's with Jakarta Commons CollectionUtils:
List list = new ArrayList();
|
I wonder if there's any performance hit from doing things this way vs. a good ol' Iterator? Hat tip to Anand Subramanian
.


Posted by Anonymous Coward on April 13, 2005 at 03:26 PM MDT #
Posted by sam newman on April 13, 2005 at 03:37 PM MDT #
Posted by Nikolay Kolev on April 13, 2005 at 06:12 PM MDT #
Posted by Dmitri Maximovich on April 13, 2005 at 06:13 PM MDT #
Posted by Colin Sampaleanu on April 13, 2005 at 06:26 PM MDT #
Posted by Keith Donald on April 13, 2005 at 06:32 PM MDT #
Posted by Keith Donald on April 13, 2005 at 06:42 PM MDT #
One object per iteration is nothing performance wise. The coding overhead on the other hand:
CollectionUtils.forAllDo(list, new Closure() { public void execute(Object obj) { // execute something for each item obj } });vs.
for (Object obj : list) { // execute something for each item obj }What's the point of the closure?
Posted by Bob Lee on April 13, 2005 at 07:05 PM MDT #
<em>What's the point of the closure?</em>
But...but...it's a <strong>CLOSURE</strong>. That <em>has</em> to be better. :-)
Posted by Dennis Doubleday on April 13, 2005 at 08:06 PM MDT #
Collection foos = ...; Collection fooBars = new SimpleConstraint<Foo>() { public boolean test(Foo foo) { return foo.isBar(); } }.findAll(foos);I like to call that "simulated internal iteration" -- or something, well, I just made that term up now... The alternative would be this:Collection foos = ...; Collection fooBars = new ArrayList<Foo>(); for (Foo foo : foos) { if (foo.isBar()) { fooBars.add(foo); } }The latter is obviously more sensitive to bugs, and the block of code (the if conditional logic) is not reusable in any way. Now for this trivial example it's not that big of deal, but had the block done something more useful then the benefits of simulated Closures in java are more apparent. How's little Bob? :-)Posted by Keith Donald on April 13, 2005 at 08:29 PM MDT #
Posted by Alex Winston on April 13, 2005 at 08:34 PM MDT #
Posted by Bob Lee on April 13, 2005 at 10:39 PM MDT #
Posted by Eric Pramono on April 14, 2005 at 04:58 AM MDT #
Posted by rory on April 14, 2005 at 08:49 AM MDT #
Rory - I use a JSPWiki Plugin for Roller.
Posted by Matt Raible on April 14, 2005 at 02:24 PM MDT #
public interface ElementProcessorVisitor { public void process(Foo fooElement); public void process(Boo barElement); }The visitable:public interface VistableElementCollection { public void iterate(ElementProcessorVisitor visitor); }Usage: Some similiarity, but the patterns are different. Visitor is all about polymorphic dispatch, to vary behaivioral logic based on an object's type.Posted by Keith Donald on April 14, 2005 at 06:41 PM MDT #
Posted by Eric Pramono on April 15, 2005 at 04:43 AM MDT #
Posted by eu on April 28, 2005 at 03:20 AM MDT #