【Guava 】Collections – Join and Split
Convert Collections to String Using Joiner
Convert List into String Using Joiner
@Test
public void whenConvertListToString_thenConverted() {
List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");
String result = Joiner.on(",").join(names);
assertEquals(result, "John,Jane,Adam,Tom");
}
Convert Map to String Using Joiner
@Test
public void whenConvertMapToString_thenConverted() {
Map<String, Integer> salary = Maps.newHashMap();
salary.put("John", 1000);
salary.put("Jane", 1500);
String result = Joiner.on(" , ").withKeyValueSeparator(" = ")
.join(salary);
assertThat(result, containsString("John = 1000"));
assertThat(result, containsString("Jane = 1500"));
}
Join Nested Collections
@Test
public void whenJoinNestedCollections_thenJoined() {
List<ArrayList<String>> nested = Lists.newArrayList(
Lists.newArrayList("apple", "banana", "orange"),
Lists.newArrayList("cat", "dog", "bird"),
Lists.newArrayList("John", "Jane", "Adam"));
String result = Joiner.on(";").join(Iterables.transform(nested,
new Function<List<String>, String>() {
@Override
public String apply(List<String> input) {
return Joiner.on("-").join(input);
}
}));
assertThat(result, containsString("apple-banana-orange"));
assertThat(result, containsString("cat-dog-bird"));
assertThat(result, containsString("apple-banana-orange"));
}
Handle null values while using Joiner
skip null values
@Test
public void whenConvertListToStringAndSkipNull_thenConverted() {
List<String> names = Lists.newArrayList("John", null, "Jane", "Adam", "Tom");
String result = Joiner.on(",").skipNulls().join(names);
assertEquals(result, "John,Jane,Adam,Tom");
}
replace them instead
@Test
public void whenUseForNull_thenUsed() {
List<String> names = Lists.newArrayList("John", null, "Jane", "Adam", "Tom");
String result = Joiner.on(",").useForNull("nameless").join(names);
assertEquals(result, "John,nameless,Jane,Adam,Tom");
}
Create Collections from String using Splitter
Create List from String using Splitter
@Test
public void whenCreateListFromString_thenCreated() {
String input = "apple - banana - orange";
List<String> result = Splitter.on("-").trimResults()
.splitToList(input);
assertThat(result, contains("apple", "banana", "orange"));
}
Create Map from String using Splitter
@Test
public void whenCreateMapFromString_thenCreated() {
String input = "John=first,Adam=second";
Map<String, String> result = Splitter.on(",")
.withKeyValueSeparator("=")
.split(input);
assertEquals("first", result.get("John"));
assertEquals("second", result.get("Adam"));
}
Split String
Split String with multiple separators
@Test
public void whenSplitStringOnMultipleSeparator_thenSplit() {
String input = "apple.banana,,orange,,.";
List<String> result = Splitter.onPattern("[.|,]")
.omitEmptyStrings()
.splitToList(input);
assertThat(result, contains("apple", "banana", "orange"));
}
Split a String at specific length
@Test
public void whenSplitStringOnSpecificLength_thenSplit() {
String input = "Hello world";
List<String> result = Splitter.fixedLength(3).splitToList(input);
assertThat(result, contains("Hel", "lo ", "wor", "ld"));
}
Limit the split result
@Test
public void whenLimitSplitting_thenLimited() {
String input = "a,b,c,d,e";
List<String> result = Splitter.on(",")
.limit(4)
.splitToList(input);
assertEquals(4, result.size());
assertThat(result, contains("a", "b", "c", "d,e"));
}
参考
Guava – Join and Split Collections
【Guava 】Collections – Join and Split的更多相关文章
- 【python3】collections系列介绍
文章来源:http://www.jb51.net/article/48771.htm (http://www.cnblogs.com/wushank/p/5122786.html) 修改人:天马流行拳 ...
- 【python】collections模块(有序字典,计数器,双向队列)
collections模块基本介绍 我们都知道,Python拥有一些内置的数据类型,比如str, int, list, tuple, dict等, collections模块在这些内置数据类型的基础上 ...
- 【java】【guava】Google Guava的splitter用法
Google Guava的splitter,分割字符串的用法 package com.sxd.swapping.guava; import com.google.common.base.CharMat ...
- Java集合【5】-- Collections源码分析
目录 一.Collections接口是做什么的? 二.Collections源码之大类方法 1.提供不可变集合 2.提供同步的集合 3.类型检查 4.提供空集合或者迭代器 5.提供singleton的 ...
- 【Guava】使用Guava的RateLimiter做限流
一.常见的限流算法 目前常用的限流算法有两个:漏桶算法和令牌桶算法. 1.漏桶算法 漏桶算法的原理比较简单,请求进入到漏桶中,漏桶以一定的速率漏水.当请求过多时,水直接溢出.可以看出,漏桶算法可以强制 ...
- java中级——集合框架【4】-Collections
Collections 首先我们要知道Collections是一个类,容器的工具类,就如同Arrays是数组的工具类 反转 reverse 使List中的数据发生发转 package cn.jse.c ...
- 【转】线程join()方法的含义
在很多情况下,主线程生成并启动了子线程,如果子线程里要进行大量的耗时运算,主线程往往将于子线程之前结束,但是如果主线程处理完其它事务后,需要用到子线程的处理结果,也就是主线程需要等待子线程执行完成之后 ...
- 【数据库】left join(左关联)、right join(右关联)、inner join(自关联)的区别
left join(左关联).right join(右关联).inner join(自关联)的区别 用一张图说明三者的区别: 总结: left join(左联接) 返回包括左表中的所有记录和右表中关联 ...
- 【Guava】PreConditions来校验参数
前置条件:让方法调用的前置条件判断更简单. 在我们的日常开发中,经常要对入参进行一定的参数校验,比如是否为空,参数的取值范围是否符合要求等等.这种参数校验如果我们单独进行校验的话,代码的重复率比较高, ...
随机推荐
- ORB(oriented FAST and rotated BRIEF)特征提取与检测
ORB采取FAST算法检测特征点,采取BRIEF算法计算特征点描述子. 1.检测特征点 检测候选特征点周围一圈的像素值,若有足够多的像素值与候选特征点的差异都较大,则认为该候选特征点是特征点. 对于上 ...
- 关于js 异步回调的一些方法
http://www.ruanyifeng.com/blog/2012/12/asynchronous%EF%BC%BFjavascript.html
- python笔记-1(import导入、time/datetime/random/os/sys模块)
python笔记-6(import导入.time/datetime/random/os/sys模块) 一.了解模块导入的基本知识 此部分此处不展开细说import导入,仅写几个点目前的认知即可.其 ...
- 实验吧—密码学——WP之 困在栅栏里的凯撒
首先我们研究题目.栅栏:凯撒 还发现一个数字“6”,那么我们很容易就有一个解题思路 对这段字符进行栅栏解密再进行凯撒解密 我们都知道,栅栏解密的关键就是栏数,根据题目中我们发现的信息,这段字符串是12 ...
- 在Java中,以下关于方法重载和方法重写描述正确的是?
public class TTTTT extends SuperC{ public String get(){ return null; } } class SuperC{ Object get(){ ...
- 异构环境oracle数据库迁移dmp文件之exp和imp以及expdp和impdp
exp/imp可在以下情况下使用 两个数据库之间传送数据 1.同一个oracle数据库的版本之间 2.不同oracle数据库的版本之间 3.相同或不相同的操作系统之间的oracle数据库 用于数据库的 ...
- Xenserver之HA实现-NFS的实现
环境: 在vm上安装好一台Xenserver服务器,一台centos7虚拟机(用来做NFS存储,因为实现HA需要共享存储),网络连接方式为桥接模式 echo '- - -'>> /sys/ ...
- linux kernel笔记
文章目录 关于linux内核中的__attribute__关键字 Linux kernel启动参数 gdt / ldt PCB 关于linux内核中的__attribute__关键字 part I: ...
- hermes 试用
hermes 是一个不错的基于kafaka 的event broker,基于push模型(webhook) 测试环境使用docker-compose 运行 环境准备 docker-compose ...
- kafka 学习资料
kafka 学习资料 kafka 学习资料 网址 kafka 中文教程 http://orchome.com/kafka/index