tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Tools and Libs > Google Guava > Collections > Map Entry Transform

Map Entry Transform 

Google Guava is a java library with lot of utilities and reusable components. This requires the library guava-10.0.jar to be in classpath. The following example shows using Maps.transformEntries() API. It transforms the given source map to target map by applying EntryTransformer callback.

File Name  :  
com/bethecoder/tutorials/guava/collection_tests/MapEntryTransformTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.guava.collection_tests;

import java.util.Map;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.common.collect.Maps.EntryTransformer;

public class MapEntryTransformTest {

  /**
   @param args
   */
  public static void main(String[] args) {
    Map<String, Integer> immutableMap = ImmutableMap.of("ONE"1"TWO"2"THREE"3);
    System.out.println(immutableMap);
    
    Map<String, String> transMap = 
      Maps.transformEntries(immutableMap, new EntryTransformer<String, Integer, String>() {

        @Override
        public String transformEntry(String key, Integer value) {
          return key.toLowerCase() 
            "-" (value * 100)//new value for this key in transformed map
        }
    });
    
    System.out.println(transMap);
  }
}
   

It gives the following output,
{ONE=1, TWO=2, THREE=3}
{ONE=one-100, TWO=two-200, THREE=three-300}



 
  


  
bl  br