let's assume I have a task to create a Set
of class names. To remove duplication of .getName()
method calls for each class, I used org.apache.commons.collections.CollectionUtils
and org.apache.commons.collections.Transformer
as follows:
Snippet 1:
Set<String> myNames = new HashSet<String>();
CollectionUtils.collect(
Arrays.<Class<?>>asList(My1.class, My2.class, My3.class, My4.class, My5.class),
new Transformer() {
public Object transform(Object o) {
return ((Class<?>) o).getName();
}
}, myNames);
An alternative would be this code:
Snippet 2:
Collections.addAll(myNames, My1.class.getName(), My2.class.getName(), My3.class.getName(), My4.class.getName(), My5.class.getName());
So, when using functional programming approach is overhead and when it's not and why?
Isn't my usage of functional programming approach in snippet 1 is an overhead and why?
for (Class<?> c : classes) { myNames.add(c.getName()) }
so I won't have to duplicategetName()
– scarfridge Oct 3 '12 at 8:56