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 ...
随机推荐
- SQL Server 数据库清空ldf日志文件
USE [master] ALTER DATABASE [DB_Develop] SET RECOVERY SIMPLE WITH NO_WAIT ALTER DATABASE [DB_Develop ...
- python技巧 — Chrome浏览器中的 XPath Helper
用于XPath 爬取网页结构的时候使用, 安装后 快捷键调用 左边 ctrl+ shift+x 启动 安装流程: 1. 打开chrome浏览器,扩展程序 .搜索 XPath Helper 下载安装(前 ...
- 解决webpack4.x使用autoprefixer 无效
安装 npm i webpck webpack-cli style-loader postcss-loader -D 配置 webpack.config.js module: { rules: [{ ...
- Codeforces Round #421 (Div. 1) (BC)
1. 819B Mister B and PR Shifts 大意: 给定排列$p$, 定义排列$p$的特征值为$\sum |p_i-i|$, 可以循环右移任意位, 求最小特征值和对应移动次数. 右移 ...
- 禁止服务向 eureka 上注册配置文件
### 禁止向注册中心注册服务eureka.client.register-with-eureka=false
- linux之rename和mv的区别
rename 命令格式 rename [ -v ] [ -n ] [ -f ] perlexpr [ files ] 参数介绍 -v:被替换掉的字符串 -n:替换成的字符串 -f:匹配要替换的文件模式 ...
- Ubuntu install android studio
Ubuntu install android studio 1. 安装 openjdk8,并在配置文件 /etc/profile 中,追加如下内容: sudo aptitude install ope ...
- NodeList和HTMLCollection区别
关于DOM集合接口,主要不同在于HTMLCollection是元素集合而NodeList是节点集合(既包括元素,也包括节点). 规定一下结果是: . node.childNodes 结果返回类型是 N ...
- active port
2510099 - SSL Port XXXXX Not Active - message on NWA even though SSL works Resolution Open the defau ...
- aspx反射调用方法
string name = base.Request["action"]; ]); if (obj2 != null) { s = obj2.ToString(); } 传入方法名 ...