博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
GUAVA的常用方法汇总
阅读量:3962 次
发布时间:2019-05-24

本文共 2531 字,大约阅读时间需要 8 分钟。

代码笔记

@RunWith(JUnit4.class)public class SimpleTest {
/** *
*
com.google.guava
*
guava
*
19.0
*
*/ @Test public void testGuava() {
// basicOperate(); CollectionOperate(); } private void CollectionOperate() {
/* * set集合交集, 并集, 差集 * */ HashSet
set1 = Sets.newHashSet(1, 2, 3, 4, 5); HashSet
set2 = Sets.newHashSet(4, 5, 6, 7, 8); //交集 Sets.SetView
intersection = Sets.intersection(set1, set2);//45 //并集 Sets.SetView
union = Sets.union(set1, set2);//12345678 //差集,set1比set2多出的部分 Sets.SetView
difference1 = Sets.difference(set1, set2);//123 Sets.SetView
difference2 = Sets.difference(set2, set1);//678 } private void typeTrans() { /* * 集合类型转换 * */ List
integers = Lists.newArrayList(1, 2, 3, 4); //注意包路径 List
strings = com.google.common.collect.Lists.transform(integers, Functions.toStringFunction()); } private void basicOperate() { /* * 一般的可变集合直接使用Maps./Lists./Sets.等方法直接调用 * 不可变集合如下所示: * (1)在多线程操作下,是线程安全的 * (2)所有不可变集合会比可变集合更有效的利用资源 * (3)中途不可改变,使用add、remove等方法直接抛异常 * */ ImmutableList
list = ImmutableList.of("a", "b"); ImmutableMap
map = ImmutableMap.of(1, "a", 2, "b", 3, "c"); ImmutableSet
set = ImmutableSet.of("a", "a");//排重 /* * 将list作为value,任意不重复主键作为key形成一个map
> * */ ArrayListMultimap
multimap = ArrayListMultimap.create(); multimap.put(1, "a"); multimap.put(1, "a"); multimap.put(1, "c"); List
strings = multimap.get(1); //返回的就是list集合 System.out.println(JacksonUtil.obj2json(strings)); /* * 双主键table,根据行和列进行查询,可以查询行得到列map,或者根据列得到行map进行遍历 */ Table
tables = HashBasedTable.create(); tables.put(1, "property", "rj"); tables.put(2, "property", "kk"); tables.put(2, "value", "airline"); String value = tables.get(2, "value"); /* * 把集合转换为特定规则的字符串 * */ String join = Joiner.on(",").withKeyValueSeparator("=").join(map); //1=a,2=b,3=c String join1 = Joiner.on(",").join(list); //a,b 如果集合初始化并且没数据,则直接为空字符串;guava使用快速失败操作,在编写时告警未初始化集合 /* * 将字符串转为集合 * */ String str1="1-2-3-4- 5- 6 "; String str2 = "1=A, 2=B, 3=C "; List
split1 = Splitter.on("-").omitEmptyStrings().trimResults().splitToList(str1);//去除-,去除空串,去除空格 Map
split2 = Splitter.on(",").omitEmptyStrings().trimResults().withKeyValueSeparator("=").split(str2); /* * 正则分隔字符串 */ String input = "aa.dd,,ff,,."; List
s = Splitter.onPattern("[.|,]").omitEmptyStrings().trimResults().splitToList(input); }}

转载地址:http://utgzi.baihongyu.com/

你可能感兴趣的文章
解决Linux CentOS中cp -f 复制强制覆盖的命令无效的方法
查看>>
wdcpv3升级到v3.2后,多PHP版本共存的安装方法
查看>>
PHP统计当前网站的访问人数,访问信息,被多少次访问。
查看>>
Windows10远程报错CredSSP加密oracle修正
查看>>
Windows server 2016 设置多用户登陆
查看>>
偶然发现的面包屑
查看>>
CentOS 7 下挂载NTFS文件系统磁盘并设置开机自动挂载
查看>>
非插件实现Typecho语法高亮
查看>>
windows 下 netsh 实现 端口映射(端口转发)
查看>>
两个好用的命令行工具 watch 和 rsync
查看>>
信安入门神级书单
查看>>
【IPFS指南】IPFS的竞争对手们(一)
查看>>
docker更换国内镜像
查看>>
CentOS 下 tree命令用法详解
查看>>
docker上传镜像至Registry时https报错解决方法
查看>>
docker下删除none的images
查看>>
Linux提权获取敏感信息方法
查看>>
Ubuntu 16.04开机A start job is running for Raise network interface(5min 4s)解决方法
查看>>
Ubuntu 16.04开机隐藏菜单缩短时间
查看>>
Ubuntu 更换国内源
查看>>