When to use functional programming approach and when not? (in Java)

Posted by john smith optional on Programmers See other posts from Programmers or by john smith optional
Published on 2012-10-03T07:25:48Z Indexed on 2012/10/03 15:50 UTC
Read the original article Hit count: 251

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?

© Programmers or respective owner

Related posts about java

Related posts about design-patterns