package com.amg.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
public class collectTransform {
public static Map<Integer, Double> getMapRMB() {
Map<Integer, Double> map_rmb = ImmutableMap.of(1, 100.12, 2, 672.87, 3, 358.45);
return map_rmb;
}
public static Map<Integer, Double> getMapRMB2() {
Map<Integer, Double> map_rmb = new HashMap<Integer, Double>();
map_rmb.put(1, 100.12);
map_rmb.put(2, 672.87);
map_rmb.put(3, 358.45);
return map_rmb;
}
public static List<Double> getListRMB2() {
List<Double> list = Lists.newArrayList();
list.add(100.12);
list.add(672.87);
list.add(358.45);
return list;
}
public static List<Double> getListRMB() {
ImmutableList<Double> list = ImmutableList.of(100.12, 672.87, 358.45);
return list;
}
public static void main(String[] args) {
// old method
Map<Integer, Double> rmbMap = getMapRMB2();
Map<Integer, Double> usdMap = getMapUSD(getMapRMB2());
for (int i = 1; i <= usdMap.size(); i++) {
System.out.println("RMBMap:" + rmbMap.get(i) + "-" + usdMap.get(i) + ":USDMap" );
}
List<Double> rmbList = getListRMB2();
List<Double> usdList = getListUSD(getListRMB2());
for (int i = 0; i < usdList.size(); i++) {
System.out.println("RMBList:" + rmbList.get(i) + "-" + usdList.get(i) + ":USDList" );
}
System.out.println("-------------------------");
// new method
List<Double> rmbPriceList = getListRMB();
List<Double> usdPriceList = Lists.transform(rmbPriceList,
new Function<Double, Double>() {
double rate = 6.12;
@Override
public Double apply(Double from) {
return from / rate;
}
});
for (Double d : usdPriceList) {
System.out.println("guavaFinalList:" + d);
}
Map<Integer, Double> rmbPriceMap = getMapRMB();
Map<Integer, Double> usdPriceMap = Maps.transformValues(rmbPriceMap,
new Function<Double, Double>() {
double rate = 6.12;
@Override
public Double apply(Double from) {
return from / rate;
}
});
for (Integer d : usdPriceMap.keySet()) {
System.out.println("guavaFinalMap key:" + d + ",value:"
+ usdPriceMap.get(d));
}
}
public static List<Double> getListUSD(List<Double> listRmb) {
List<Double> listUsd = new ArrayList<Double>();
for (int i = 0; i < listRmb.size(); i++) {
double rate = 6.12;
double rmbPrice = listRmb.get(i);
double usdPrice = rmbPrice / rate;
listUsd.add(usdPrice);
}
return listUsd;
}
public static Map<Integer, Double> getMapUSD(Map<Integer, Double> mapRmb) {
Map<Integer, Double> map_usd = new HashMap<Integer, Double>();
for (int i = 1; i <= mapRmb.size(); i++) {
double rate = 6.12;
double rmbPrice = mapRmb.get(i);
double usdPrice = rmbPrice / rate;
map_usd.put(i, usdPrice);
}
return map_usd;
}
}
运行结果:
RMBMap:100.12-16.359477124183005:USDMap
RMBMap:672.87-109.94607843137254:USDMap
RMBMap:358.45-58.57026143790849:USDMap
RMBList:100.12-16.359477124183005:USDList
RMBList:672.87-109.94607843137254:USDList
RMBList:358.45-58.57026143790849:USDList
-------------------------
guavaFinalList:16.359477124183005
guavaFinalList:109.94607843137254
guavaFinalList:58.57026143790849
guavaFinalMap key:1,value:16.359477124183005
guavaFinalMap key:2,value:109.94607843137254
guavaFinalMap key:3,value:58.57026143790849