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打包,指定入口类的jar包,具体的pom配置为:<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.1.0</version> <executions> <execution> ...
现代化Maven插件 Modernizer Maven插件可以检测对旧版API的使用,这些旧版API可以替代现代Java版本。 这些现代的API通常比传统的API具有更高的性能,安全性和惯用性... < groupId>org.gaul</ groupId> < artifactId>
Maven坐标:com.google.guava:guava:11.0.2; 标签:google、guava、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构...
下载:<dependency> <groupId>com.squareup</groupId> <artifactId>otto</artifactId> <version>(insert latest version)</version> </dependency>用法:Bus bus = new Bus();发布:bus.post(new ...
<artifactId>guava</artifactId> <version>20.0</version> </dependency> <!-- ...其他依赖... --> </dependencies> ``` 完成SpringMVC项目的基本搭建后,我们开始将其转换为Spring Boot项目。Spring Boot简化...
<artifactId>guava</artifactId> <version>24.0-jre</version> </dependency> 然后,我们需要创建一个配置类,用于配置 Guava 的缓存机制: ```java @Configuration @EnableCaching public class ...
在`<head>`中,你可以设置元信息,如页面标题(`<title>`),而在`<body>`中则包含实际的网页内容。 HTML标记有多种类型,分为单标签(例如`<br>`用于换行)和双标签(例如`<p>`用于段落)。常见的标记包括: 1. `...
<artifactId>guava</artifactId> <version>30.1-jre</version> </dependency> </dependencies> ``` Maven会自动下载并管理这些依赖,包括它们的传递性依赖。 ### 六、Maven插件 Maven插件扩展了Maven的功能,...
<artifactId>guava</artifactId> <version>***</version> </dependency> ``` # Gradle依赖: ``` Gradle: implementation group: 'com.google.guava', name: 'guava', version: '***' Gradle (Short): ...
<artifactId>guava</artifactId> <version>***</version> </dependency> ``` # Gradle依赖: ``` Gradle: implementation group: 'com.google.guava', name: 'guava', version: '***' Gradle (Short): ...
<artifactId>guava</artifactId> <version>***</version> </dependency> ``` # Gradle依赖: ``` Gradle: implementation group: 'com.google.guava', name: 'guava', version: '***' Gradle (Short): ...
<artifactId>guava</artifactId> <version>***</version> </dependency> ``` # Gradle依赖: ``` Gradle: implementation group: 'com.google.guava', name: 'guava', version: '***' Gradle (Short): ...
<artifactId>guava</artifactId> <version>***</version> </dependency> ``` # Gradle依赖: ``` Gradle: implementation group: 'com.google.guava', name: 'guava', version: '***' Gradle (Short): ...
<artifactId>guava</artifactId> <version>***</version> </dependency> ``` # Gradle依赖: ``` Gradle: implementation group: 'com.google.guava', name: 'guava', version: '***' Gradle (Short): ...
Map<String, Map<String, List<Person>>> peopleByGenderAndCity = people.stream() .collect(Collectors.groupingBy(Person::getGender, groupingBy(Person::getCity))); // 这将创建一个Map,外层键是性别,内...
<artifactId>guava</artifactId> <version>30.1-jre</version> </dependency> ``` 2. **静态导入(Static Imports)** Guava鼓励使用静态导入,以减少代码中的冗余。例如,你可以导入`...
Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<>(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect....
赠送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...
Map<String, Integer> mutableMap = new HashMap<>(); mutableMap.put("One", 1); mutableMap.put("Two", 2); ImmutableMap<String, Integer> immutableMap = ImmutableMap.copyOf(mutableMap); ``` 对于空...
<artifactId>spring-boot-starter-cache</artifactId> </dependency> ``` Gradle: ```gradle implementation 'com.google.guava:guava:版本号' implementation 'org.springframework.boot:spring-boot-...