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

List 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 Lists.transform() API. It transforms the given source list to target list by applying Function callback.

File Name  :  
com/bethecoder/tutorials/guava/collection_tests/ListTransformTest.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.guava.collection_tests;

import java.util.List;

import com.google.common.base.Function;
import com.google.common.collect.Lists;

public class ListTransformTest {

  /**
   @param args
   */
  public static void main(String[] args) {
    List<Integer> intList = Lists.newArrayList(123456);
    System.out.println(intList);
    
    List<String> transList = Lists.transform(intList, new Function<Integer, String>() {

      @Override
      public String apply(Integer input) {
        return "BTC-" + input; //new value in transformed list
      }
      
    });
    
    System.out.println(transList);
  }

}
   

It gives the following output,
[1, 2, 3, 4, 5, 6]
[BTC-1, BTC-2, BTC-3, BTC-4, BTC-5, BTC-6]


                    Print

   



Books Recommendation

 
 
bl  br