【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来校验参数
前置条件:让方法调用的前置条件判断更简单. 在我们的日常开发中,经常要对入参进行一定的参数校验,比如是否为空,参数的取值范围是否符合要求等等.这种参数校验如果我们单独进行校验的话,代码的重复率比较高, ...
随机推荐
- s21day01 python笔记
s21day01 python笔记 一.计算机基础 计算机的初步认识 用户:人 软件:QQ.浏览器等 解释器/编译器/虚拟机:java解释器.python解释器等 操作系统 硬件:CPU.内存.硬盘. ...
- Java异常处理的方法
1.异常概述在程序中,错误可能产生于程序员没有预料到的各种情况,或者是超出了程序员可控制范围的环境因素,如用户的坏数据.试图打开一个根本不存在的文件等.在Java中这种在程序运行是可能出现的一些错误称 ...
- easyui表单校验
痛苦使人清醒,痛苦使人警惕.生于忧患,死于安乐.付出总会有回报. 1.下面跟大家分享使用easyui时表单中的值如何校验. 1.1 首先,在你的jsp/html页面引入JQuery和easyui的js ...
- Go Example--关闭通道
package main import ( "fmt" ) func main() { jobs := make(chan int, 5) done := make(chan bo ...
- LeetCode–Flip Game
You are playing the following Flip Game with your friend: Given a string that contains only these tw ...
- UVA10341 Solve It
题意 PDF 分析 在\(0\le x\le 1\)时,\(f(x)=pe^{-x}+q\sin x+r\cos x+s\tan x+tx^2+u\)是减函数,所以当\(f(0)\ge 0 \wedg ...
- spring事务中出现oracle游标溢出的解决方案
本例事务中大量查询SQL语句,会导致oracle游标溢出:对于数据库游标出现解决方案:1.大量查询SQL语句取消事务,只针对插入/更新 做事务处理2.用临时表代替大量查询SQL语句推荐使用第二种方案
- url和资源的再理解
元数据管理系统中, 确实是所有的静态资源都放在WebContent 不在dgs这个主项目中,通过url访问了 下面的这个项目在dgs中
- Android.bp学习笔记
1.Android.bp简介 Android 7.0之后希望用Android.bp替换Android.mk,bp简单的配置更方便Ninja 文件的产生,而Blueprint和Soong 就此产生.An ...
- elasticsearch技术解析与实战(一) 入门和索引
GET _cat/nodes GET _cat/health GET _cat/shards GET http://10.37.84.124:9200/secisland?pretty { " ...