Guava学习笔记:transfer of List<E> and Map<E>

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值