guava字符串工具--------Joiner 根据给定的分隔符把字符串连接到一起
public class JoinerTest {
public static void main(String args[]){
//1、将list字符串集合,以,形式转为字符串
List<String> list =new ArrayList<String>();
list.add("xx");
list.add("zz");
list.add("dd");
//Joiner.on(",")获得Joiner实例对象
Joiner joiner = Joiner.on(",");
//joiner.join(list)传入操作的集合,并转成字符串格式
System.out.println(joiner.join(list));
//输出结果:xx,zz,dd
//2、将Iterator<T>列表,转为字符串
Iterator<String> it=list.iterator();
String str=Joiner.on("|").join(it);
System.out.println(str);
//输出结果:xx|zz|dd
//3、连接多个字符串
String str1=Joiner.on(",").join("小小","爸爸","妈妈","爷爷","奶奶");
System.out.println(str1);
//输出:小小,爸爸,妈妈,爷爷,奶奶
//4、连接字符串与列表
StringBuilder builder=new StringBuilder("小小最乖");
//返回StringBuilder类型
StringBuilder str2=Joiner.on(",").appendTo(builder,list);
System.out.println(str2);
//输出:小小最乖xx,zz,dd
//5、跳过null值连接
list.add(null);
list.add("小小");
//skipNulls()代表去除null
String str3=Joiner.on(",").skipNulls().join(list);
System.out.println(str3);
//输出:xx,zz,dd,小小
//6、替换null值进行连接
String str4=Joiner.on(",").useForNull("空").join(list);
System.out.println(str4);
//输出:xx,zz,dd,空,小小
//7、Map的键值对打印出来
Map<String, String> map = new HashMap<>();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
Joiner.MapJoiner mapJoiner = Joiner.on(",").withKeyValueSeparator("=");
System.out.println(mapJoiner.join(map));
//输出:key1=value1,key2=value2,key3=value3
//8、一步将字符串转为List<Long>集合(前面先转为list<String>,后面用java8特性将List<String>转为List<Long>)
List<Long> list=Splitter.on("#").splitToList("111#222#333").stream().mapToLong(str->Long.parseLong(str)).boxed().distinct().collect(Collectors.toList());
} }
guava字符串工具--------Joiner 根据给定的分隔符把字符串连接到一起的更多相关文章
- java代码之美(8)---guava字符串工具
guava字符串工具 在java开发过程中对字符串的处理是非常频繁的,google的guava工具对字符串的一些处理进行优化,使我们开发过程中让自己的代码看去更加美观,清爽. 一.Joiner 根据给 ...
- java代码(8) ---guava字符串工具
guava字符串工具 一.Joiner 根据指定的分隔符把字符串连接在一起,MapJoiner执行相同的操作,但是针对Map的key和value 分析源码可知:该类构造方法被private修饰,无法直 ...
- Java 将指定字符串连接到此字符串的结尾 concat()
Java 手册 concat public String concat(String str) 将指定字符串连接到此字符串的结尾. 如果参数字符串的长度为 0,则返回此 String 对象.否则,创建 ...
- guava字符串工具 Strings 校验补全 转换null和""
public class StringsTest { public static void main(String args[]){ //1.补右全(Strings.padEnd方法) String ...
- guava字符串工具 Splitter 主要功能是拆分字符串为集合 Map
public class SplitterTest { public static void main(String args[]){ //1.拆分字符串为List集合 String str=&quo ...
- [Google Guava]字符串处理:连接器、拆分器、字符匹配器
一.连接器[Joiner] 二.拆分器[Splitter] 三.字符匹配器[CharMatcher] 四.字符集[Charsets] Charsets:针对所有Java平台都要保证支持的六种字符集提供 ...
- * 类描写叙述:字符串工具类 类名称:String_U
/****************************************** * 类描写叙述:字符串工具类 类名称:String_U * ************************** ...
- Guava 开源工具的简单介绍
Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, caching, primitives support, concurrency libra ...
- StringUtils 字符串工具类
package com.thinkgem.jeesite.common.utils; import java.io.File; import java.io.IOException; import j ...
随机推荐
- Linux基础-04-权限
1. 查看文件的权限 1) 使用ls –l命令查看文件上所设定的权限. -rw-r--r-- 1 root root 605 Mar 18 20:28 .jp1.tar.gz 权限信息 属主 属组 文 ...
- UnknownError: session deleted because of page crash from tab crashed
一.问题 在docker上跑Selenium+ChromeDriver+Chrome无头模式报错: UnknownError: unknown error: session deleted becau ...
- Inline Hook 钩子编写技巧
Hook 技术通常被称为钩子技术,Hook技术是Windows系统用于替代中断机制的具体实现,钩子的含义就是在程序还没有调用系统函数之前,钩子捕获调用消息并获得控制权,在执行系统调用之前执行自身程序, ...
- hdu 6625 three array (01-trie)
大意: 给两个数组$a,b$, 要求重排使得$c_i=a_i\oplus b_i$字典序最小. 字典树上贪心取$n$次, 然后排序, 还不知道怎么证. #include <iostream> ...
- idea下spring boot jpa写原生sql的时候,报Cannot resolve table错误
错误如图 打开View→Tool Windows→Persistence选项 在弹出的Persistence窗口的项目上右键,选择Generate Persistence Mapping→By Dat ...
- VsCode开发Angular的必备插件
1 概述 一般个人开发或者小公司开发都会使用破解版软件,除非比较尊重正版且不太缺钱的人才会用正版,但是大型公司有严格的规定,不允许员工使用盗版软件. 这时候我就不得不从WebStorm转向VsCode ...
- win10环境下,让所有程序都以管理员身份运行的办法
记录下,备查! 打开gpedit.msc组策略编辑. 左侧依次找到:计算机配置->Window设置->安全设置->本地策略->安全选项 然后再右侧找到:用户账户控制:以管理员批 ...
- 起始路由改成分区(Areas)的RouteConfig.cs配置方法
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/ ...
- MySQL库的相关操作
再熟悉一下Mysql库.表.记录的基本操作. 库 增 create database userinfo1 charset utf8; 查 show databases; show create dat ...
- Arc Engine二次开发——弹窗进行属性查询
在Arcmap中使用Sapefile格式的矢量数据时,经常会用到其属性查询的功能,弹出窗体然后用户鼠标点击或手动输入查询条件,进而查询到感兴趣的要素.在AE二次开发中也经常需要这个功能,于是在此记录整 ...