5.16. Transforming Collections

Problem

You need to perform a transformation using each element in a Collection.

Solution

Use CollectionUtils.transform() . Supply a Collection and a Transformer to this method, and this utility will pass each element in the Collection to that Transformer, returning a new Collection containing the results of each transformation. The following example demonstrates the use of transform( ) with a custom Transformer object:

import java.util.*;
import org.apache.commons.collections.Transformer;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

// Create a transformer that reverses strings.
               Transformer reversingTransformer = new Transformer( ) {
                   public Object transform(Object object) {
                       String string = (String) object;
                       return StringUtils.reverse( string );
                   }
               }

String[] array = new String[] { "Aznar", "Blair", "Chirac", "Putin", "Bush" };
List stringList = Arrays.asList( ArrayUtils.clone( array ) );

// Transform each element with a Transformer
               CollectionUtils.transform( stringList, reversingTransformer );

System.out.println( "Original: " + ArrayUtils.toString( array ) );
System.out.println( "Transformed: " + 
                    ArrayUtils.toString( stringList.toArray( ) ) );

This example creates a List of strings, transforming each element with a Transformer that produces reversed strings. The List is transformed in-place, which means that the contents of the List are replaced with the output from the transform( ) method of the Transformer. As expected, the transformed List contains reversed strings:

Original: Aznar, Blair, Chirac, Putin, Bush
Transformed: ranzA, rialB, carihC, nituP, hsuB

CollectionUtils.transform( ) does not only apply to Lists; any object implementing the Collection interface can be used with this utility.

Discussion

Transformers are another form of functor that transforms one object into another, leaving the original object unchanged. Transformer is a very straightforward interface, simply requiring the presence of a transform( ) method. The Transformer interface, like Predicate, is augmented by a number of useful built-in implementations, which can combine the effects of multiple transformers. One of these Transformer implementations is the ChainedTransformer, which allows you to create a linear pipeline of transformations. The following example demonstrates the use of a ChainedTransformer to transform the contents of a Collection:

import java.util.*;
import org.apache.commons.collections.Transformer;
import org.apache.commons.collections.finctors.ChainedTransformer;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

// Create a transformer that multiplies a number by 10
Transformer multiplyTransform = new Transformer( ) {
    public Object transform(Object object) {
        Integer integer = (Integer) object;
            return new Integer( integer.intValue( ) * 10 );
    }
}

// Create a transformer that subtracts 20 from a number
Transformer subtractTransform = new Transformer( ) {
    public Object transform(Object object) {
        Integer integer = (Integer) object;
            return new Integer( integer.intValue( ) - 20 );
    }
}

// Chain the transformers together
               Transformer transformChain = new ChainedTransformer( 
                   new Transformer[] { multiplyTransform, subtractTransform } );

// Create a List of Integer objects
int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };
List intList = Arrays.asList( ArrayUtils.toObject( array ) );

CollectionUtils.transform( intList, transformChain );

System.out.println( "Original: " + ArrayUtils.toString( array ) );
System.out.println( "Transformed: " + 
                    ArrayUtils.toString( intList.toArray( ) ) );

Two transformers, multiplyTransform and subtractTransform, are defined as anonymous inner classes, which implement the Transformer interface. These two Transformers are combined in a ChainedTransformer. An array of int primitives is converted to an array of Integer objects using ArrayUtils.toObject( ). This object array is then converted to a List, which is transformed using CollectionUtils.transform( ). The transformation multiples each element by 10 and subtracts 20, producing the following output:

Original: 1, 2, 3, 4, 5, 6, 7, 8
Transformed: -10, 0, 10, 20, 30, 40, 50, 60

See Also

In addition to the ChainedTransformer , there are a number of other useful Transformer implementations that ship with Commons Collections. The ChainedTransformer and other Transformer implementations are discussed in depth in Chapter 4. For more information about the various implementations of Transformer, see the Commons Collections project (http://jakarta.apache.org/commons/collections).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.176.72