import java.util.*;import org.apache.commons.collections.Transformer;import org.apache.commons.collections.map.LazyMap;import org.apache.commons.lang.StringUtils;public class LazyMapTest {public static void main(String[]args){// Create a Transformer to reverse strings - defined belowfinal Transformer reverseString = new Transformer( ) { public Object transform( Object object ) {//定义转换的方法 String name = (String) object; String reverse = StringUtils.reverse( name ); return reverse; }};// Create a LazyMap called lazyNames, which uses the above TransformerMap names = new HashMap( );Map lazyNames = LazyMap.decorate(names, reverseString );//将Map和transformer传递给lazymap// Get and print two namesString name = (String) lazyNames.get("Thomas");//调用LazyMap里面的get()方法如果,没有这个key会调用transform方法获得value,返回getSystem.out.println("name:"+name);}}###使用transformer chainimport java.util.*;import org.apache.commons.collections.Transformer;import org.apache.commons.collections.functors.ChainedTransformer;import org.apache.commons.collections.map.LazyMap;import org.apache.commons.lang.StringUtils;public class LazyMapTest {public static void main(String[]args){ // Create a Transformer to reverse strings - defined below final Transformer reverseString = new Transformer( ) { public Object transform( Object object ) { Long number = (Long) object; return( new Long( number.longValue() * 100 ) ); } }; Transformer increment = new Transformer( ) { public Object transform(Object input) { Long number = (Long) input; return( new Long( number.longValue( ) + 1 ) ); } }; Transformer[] chainElements = new Transformer[] { reverseString , increment };//一个transformer 结果作为另外一个transform的输入值 Transformer chain = new ChainedTransformer( chainElements ); Long original = new Long( 34 ); Long result = (Long) chain.transform(original); System.out.println( "Original: " + original ); System.out.println( "Result: " + result ); // Create a LazyMap called lazyNames, which uses the above Transformer } }