【Java常用工具类汇总 2】Google核心库GUAVA(附代码示例)

/**

  • on :map item之间分隔符

withKeyValueSeparator:定义map项的分隔符

*/

//转为指定格式字符串

System.out.println(Joiner.on(“;”).withKeyValueSeparator(“=”).join(ImmutableMap.of(“id”, “1”, “name”, “zs”)));//id=1;name=zs

//指定格式字符串转为map

System.out.println(Splitter.on(“;”).withKeyValueSeparator(“=”).split(“id=1;name=zs”));//{id=1, name=zs}

}

2、连接器 Joiner 小测试

小练习:

需求:

①有一个[1, , 2, null, 3]

②按逗号分隔,并去掉null和""

private static List makeList() {

List list = new ArrayList();

list.add(“1”);

list.add(“”);

list.add(“2”);

list.add(null);

list.add(“3”);

return list;//[1, , 2, null, 3]

}

static String delimiter = “,”;

//循环方式

private static void test01() {

List list = makeList();

StringBuilder builder = new StringBuilder();

for(String str : list) {

if(StringUtils.isNotEmpty(str)) {

builder.append(str+delimiter);

}

}

System.out.println(builder);//1,2,3,

builder.setLength(builder.length() - delimiter.length());

System.out.println(builder);//1,2,3

}

//Java8 stream方式

private static void test02() {

List list = makeList();

System.out.println(list.stream().filter(x -> StringUtils.isNotEmpty(x)).collect(Collectors.joining(“,”)));//1,2,3

System.out.println(list.stream().filter(StringUtils::isNotEmpty).collect(Collectors.joining(“,”)));//1,2,3

}

//Guava Joiner

private static void test03() {

List list = makeList();

System.out.println(Joiner.on(“,”).join(list));//java.lang.NullPointerException

System.out.println(Joiner.on(“,”).join(list.stream().filter(StringUtils::isNotEmpty).collect(Collectors.toList())));//1,2,3

}

// java.lang

private static void test04() {

List list = makeList();

System.out.println(String.join(“,”, list.stream().filter(StringUtils::isNotEmpty).collect(Collectors.toList())));//1,2,3

}

四、CharMatcher


常用方法如下:

  1. javaDigit:数字

  2. javaLowerCase:小写

  3. javaUpperCase:大写

  4. whitespace:空格

  5. matches:匹配,返回Boolean

  6. removeFrom:移除指定类型

  7. retainFrom:除指定字符外全部移除

private static void test06() {

//判断是否是数字类型

System.out.println(CharMatcher.javaDigit().matches(‘1’));//true

System.out.println(CharMatcher.javaDigit().matches(‘z’));//false

//将某字符串的数字部分替换为某字符

System.out.println(CharMatcher.javaDigit().replaceFrom(“hello world666”, “1”));//hello world111

//javaLowerCase:小写

System.out.println(CharMatcher.javaLowerCase().matches(‘a’));//true

System.out.println(CharMatcher.javaLowerCase().matches(‘A’));//false

//javaUpperCase:大写

System.out.println(CharMatcher.javaUpperCase().matches(‘a’));//false

System.out.println(CharMatcher.javaUpperCase().matches(‘A’));//true

//移除空格

System.out.println(CharMatcher.whitespace().removeFrom(“hello world666”));//helloworld666

//移除数字

System.out.println(CharMatcher.javaDigit().removeFrom(“hello world666”));//hello world

//移除小写字母

System.out.println(CharMatcher.javaLowerCase().removeFrom(“hello worldAAA666”));// AAA666

//移除小写字母或数字

System.out.println(CharMatcher.javaLowerCase().or(CharMatcher.javaDigit()).removeFrom(“hello worldAAA666”));// AAA

//除指定字符外全部移除

System.out.println(CharMatcher.javaUpperCase().retainFrom(“hello worldAAA666”));//AAA

//判断某字符串包含几个某字符

System.out.println(CharMatcher.is(‘o’).countIn(“hello world”));//2

//去掉连续空格,并用指定字符替换空格

System.out.println(CharMatcher.whitespace().trimAndCollapseFrom(“hello world AAA666”, ’ '));//hello world AAA666

System.out.println(CharMatcher.whitespace().trimAndCollapseFrom(“hello world AAA666”, ''));//helloworld*AAA666

}

五、指定字符串格式转换


private static void test07() {

//hyphen 连字符;camel 骆驼

System.out.println(CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, “user-name”)); //userName

//underscore 下划线

System.out.println(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, “user_name”)); //userName

System.out.println(CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, “user_name”)); //UserName

System.out.println(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, “USER_name_zz”)); //UserNameZz

/**

  • 感觉前面的LOWER_UNDERSCORE或者UPPER_UNDERSCORE,,都是一样的,,暂时不懂为什么

*/

System.out.println(CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, “USER_name_ZZ-DD”)); //UserNameZz-dd

System.out.println(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, “USER_name_ZZ-DD”)); //UserNameZz-dd

}

往期精彩内容:

Java知识体系总结

Spring框架总结

超详细的springBoot学习笔记

常见数据结构与算法整理总结

img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Go语言开发知识点,真正体系化!

加入社区》https://bbs.csdn.net/forums/4304bb5a486d4c3ab8389e65ecb71ac0

超详细的springBoot学习笔记

常见数据结构与算法整理总结

[外链图片转存中…(img-WY57flaE-1725738049774)]
[外链图片转存中…(img-bgP13OoS-1725738049775)]
[外链图片转存中…(img-6dh06aT0-1725738049776)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Go语言开发知识点,真正体系化!

加入社区》https://bbs.csdn.net/forums/4304bb5a486d4c3ab8389e65ecb71ac0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值