Wednesday April 13, 2005
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
.
Matt Raible is a Web Architect who enjoys developing applications with open source technologies. Contact me for rates.
Search This Site
Recent Entries
- What's Next
- Jack's Mohawk
- LinkedIn Cuts 10% (a.k.a. The Journey is Over)
- Happy Birthday Abbie!
- Moving from Spring's XML to Annotations in AppFuse
- Free Maven Training in New Orleans on Election Day
- AppFuse Light ยป AppFuse, Maven Archetypes and Shared Web Assets
- Great Weekend in Montana
- Colorado Software Summit 2008 Wrapup
- RESTful Web Applications with Subbu Allamaraju
Posted by Anonymous Coward on April 13, 2005 at 09:26 AM MDT #
Posted by sam newman on April 13, 2005 at 09:37 AM MDT #
Posted by Nikolay Kolev on April 13, 2005 at 12:12 PM MDT #
Posted by Dmitri Maximovich on April 13, 2005 at 12:13 PM MDT #
Posted by Colin Sampaleanu on April 13, 2005 at 12:26 PM MDT #
Posted by Keith Donald on April 13, 2005 at 12:32 PM MDT #
Posted by Keith Donald on April 13, 2005 at 12: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 01: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 02: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 02:29 PM MDT #
Posted by Alex Winston on April 13, 2005 at 02:34 PM MDT #
Posted by Bob Lee on April 13, 2005 at 04:39 PM MDT #
Posted by Eric Pramono on April 13, 2005 at 10:58 PM MDT #
Posted by rory on April 14, 2005 at 02:49 AM MDT #
Rory - I use a JSPWiki Plugin for Roller.
Posted by Matt Raible on April 14, 2005 at 08:24 AM 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 12:41 PM MDT #
Posted by Eric Pramono on April 14, 2005 at 10:43 PM MDT #
Posted by eu on April 27, 2005 at 09:20 PM MDT #