`
hejiajunsh
  • 浏览: 410067 次
  • 性别: Icon_minigender_1
  • 来自: 天津
社区版块
存档分类
最新评论

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

 

分享到:
评论

相关推荐

    maven-shade-plugin-3.1.0.jar

    maven打包,指定入口类的jar包,具体的pom配置为:&lt;plugin&gt; &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt; &lt;artifactId&gt;maven-shade-plugin&lt;/artifactId&gt; &lt;version&gt;3.1.0&lt;/version&gt; &lt;executions&gt; &lt;execution&gt; ...

    modernizer-maven-plugin:检测旧版Java API的使用

    现代化Maven插件 Modernizer Maven插件可以检测对旧版API的使用,这些旧版API可以替代现代Java版本。 这些现代的API通常比传统的API具有更高的性能,安全性和惯用性... &lt; groupId&gt;org.gaul&lt;/ groupId&gt; &lt; artifactId&gt;

    guava-11.0.2-API文档-中文版.zip

    Maven坐标:com.google.guava:guava:11.0.2; 标签:google、guava、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构...

    基于Guava的事件总线Otto.zip

    下载:&lt;dependency&gt;  &lt;groupId&gt;com.squareup&lt;/groupId&gt;  &lt;artifactId&gt;otto&lt;/artifactId&gt;  &lt;version&gt;(insert latest version)&lt;/version&gt; &lt;/dependency&gt;用法:Bus bus = new Bus();发布:bus.post(new ...

    SpringMVC-SpringBoot项目搭建.docx

    &lt;artifactId&gt;guava&lt;/artifactId&gt; &lt;version&gt;20.0&lt;/version&gt; &lt;/dependency&gt; &lt;!-- ...其他依赖... --&gt; &lt;/dependencies&gt; ``` 完成SpringMVC项目的基本搭建后,我们开始将其转换为Spring Boot项目。Spring Boot简化...

    springboot本地缓存(guava与caffeine).docx

    &lt;artifactId&gt;guava&lt;/artifactId&gt; &lt;version&gt;24.0-jre&lt;/version&gt; &lt;/dependency&gt; 然后,我们需要创建一个配置类,用于配置 Guava 的缓存机制: ```java @Configuration @EnableCaching public class ...

    html 标记大全.rar

    在`&lt;head&gt;`中,你可以设置元信息,如页面标题(`&lt;title&gt;`),而在`&lt;body&gt;`中则包含实际的网页内容。 HTML标记有多种类型,分为单标签(例如`&lt;br&gt;`用于换行)和双标签(例如`&lt;p&gt;`用于段落)。常见的标记包括: 1. `...

    热-maven安装与配置实践

    &lt;artifactId&gt;guava&lt;/artifactId&gt; &lt;version&gt;30.1-jre&lt;/version&gt; &lt;/dependency&gt; &lt;/dependencies&gt; ``` Maven会自动下载并管理这些依赖,包括它们的传递性依赖。 ### 六、Maven插件 Maven插件扩展了Maven的功能,...

    guava-r06.jar中文文档.zip

    &lt;artifactId&gt;guava&lt;/artifactId&gt; &lt;version&gt;***&lt;/version&gt; &lt;/dependency&gt; ``` # Gradle依赖: ``` Gradle: implementation group: 'com.google.guava', name: 'guava', version: '***' Gradle (Short): ...

    guava-r03.jar中文文档.zip

    &lt;artifactId&gt;guava&lt;/artifactId&gt; &lt;version&gt;***&lt;/version&gt; &lt;/dependency&gt; ``` # Gradle依赖: ``` Gradle: implementation group: 'com.google.guava', name: 'guava', version: '***' Gradle (Short): ...

    guava-r05.jar中文文档.zip

    &lt;artifactId&gt;guava&lt;/artifactId&gt; &lt;version&gt;***&lt;/version&gt; &lt;/dependency&gt; ``` # Gradle依赖: ``` Gradle: implementation group: 'com.google.guava', name: 'guava', version: '***' Gradle (Short): ...

    guava-r08.jar中文文档.zip

    &lt;artifactId&gt;guava&lt;/artifactId&gt; &lt;version&gt;***&lt;/version&gt; &lt;/dependency&gt; ``` # Gradle依赖: ``` Gradle: implementation group: 'com.google.guava', name: 'guava', version: '***' Gradle (Short): ...

    guava-r07.jar中文文档.zip

    &lt;artifactId&gt;guava&lt;/artifactId&gt; &lt;version&gt;***&lt;/version&gt; &lt;/dependency&gt; ``` # Gradle依赖: ``` Gradle: implementation group: 'com.google.guava', name: 'guava', version: '***' Gradle (Short): ...

    guava-r09.jar中文文档.zip

    &lt;artifactId&gt;guava&lt;/artifactId&gt; &lt;version&gt;***&lt;/version&gt; &lt;/dependency&gt; ``` # Gradle依赖: ``` Gradle: implementation group: 'com.google.guava', name: 'guava', version: '***' Gradle (Short): ...

    java集合分组(Guava)

    Map&lt;String, Map&lt;String, List&lt;Person&gt;&gt;&gt; peopleByGenderAndCity = people.stream() .collect(Collectors.groupingBy(Person::getGender, groupingBy(Person::getCity))); // 这将创建一个Map,外层键是性别,内...

    Guava教程1使用GoogleCollections,G

    &lt;artifactId&gt;guava&lt;/artifactId&gt; &lt;version&gt;30.1-jre&lt;/version&gt; &lt;/dependency&gt; ``` 2. **静态导入(Static Imports)** Guava鼓励使用静态导入,以减少代码中的冗余。例如,你可以导入`...

    springboot 使用 redis guava caffeine 缓存示例.zip

    Jackson2JsonRedisSerializer&lt;Object&gt; serializer = new Jackson2JsonRedisSerializer&lt;&gt;(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect....

    guava-18.0-API文档-中英对照版.zip

    赠送jar包:guava-18.0.jar 赠送原API文档:guava-18.0-javadoc.jar 赠送源代码:guava-18.0-sources.jar 包含翻译后的API文档:guava-18.0-javadoc-API文档-中文(简体)-英语-对照版.zip 对应Maven信息:groupId...

    不可变对象ImmutableXXX:Collection、List、Set、Map…

    Map&lt;String, Integer&gt; mutableMap = new HashMap&lt;&gt;(); mutableMap.put("One", 1); mutableMap.put("Two", 2); ImmutableMap&lt;String, Integer&gt; immutableMap = ImmutableMap.copyOf(mutableMap); ``` 对于空...

    springbt_guava_cache.7z

    &lt;artifactId&gt;spring-boot-starter-cache&lt;/artifactId&gt; &lt;/dependency&gt; ``` Gradle: ```gradle implementation 'com.google.guava:guava:版本号' implementation 'org.springframework.boot:spring-boot-...

Global site tag (gtag.js) - Google Analytics