Guava
是一种基于开源的Java
库,其中包含「谷歌」正在由他们很多项目使用的很多核心库。这个库是为了方便编码,并减少编码错误。这个库提供用于集合,缓存,支持原语,并发性,常见注解,字符串处理,I/O
和验证的实用方法。
有人会说,我为什么要要用Guava
,用这个有什么用。下面我们来介绍下Guava
,介绍来自:https://www.oschina.net/p/guava?hmsr=aladdin1e1。
导入Maven
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>27.0-jre</version>
<!-- or, for Android: -->
<version>27.0-android</version>
</dependency>
@Test
public void testJoiner() {
Joiner joiner = Joiner.on(",").skipNulls();
String join = joiner.join("Harry", null, "Ron");
System.out.println("join:" + join);
Joiner joiner1 = Joiner.on(",").useForNull("");
String content1 = joiner1.join("Harry", null, "Ron", "Hermione");
System.out.println("content1:" + content1);
System.out.println(Joiner.on("-").join("a", "b", "c", "d"));
System.out.println(Joiner.on("-").join(Lists.newArrayList("a", "b", "c", "d")));
}
「运行结果」
Joiner.on(",").skipNulls()
会将为null
的值过滤掉。Joiner.on(",").useForNull("")
不会过滤掉为null
。
Map
集合Key/Value
拼接@Test
public void testMapJoiner() {
LinkedHashMap<String, String> map = Maps.newLinkedHashMap();
map.put("name", "doctor");
map.put("sex", "man");
Joiner.MapJoiner mapJoiner = Joiner.on("&").withKeyValueSeparator("~");
String join = mapJoiner.join(map);
System.out.println(join);
}
withKeyValueSeparator("~")
可以将Map的key和value连接起来。如上图所示。
@Test
public void testSplitter() {
String content = ",a,b,,,c,";
//String content = "a,b";
// java内置的会忽略[尾部]空内容
String[] split = content.split(",");
System.out.println(Arrays.toString(split));
// guava默认不会忽略空内容
List<String> splitToList = Splitter.on(",").splitToList(content);
System.out.println(splitToList.toString());
// 可以自由选择的忽略,像java一样,忽略空字符串
List<String> splitToList2 = Splitter.on(",").omitEmptyStrings().trimResults().splitToList(content);
System.out.println(splitToList2.toString());
}
运行结果如下:
Java
j内置的会忽略【尾部】空内容,而Guava
不会。Guava
可以根据实际业务场景做定制化的开发。
Guava
可以对字符作匹配。功能非常全面
@Test
public void testCharMatcher() {
// 1.获取字符串中数字
String str1 = "1mahesh123ff87f";
String retainFrom = CharMatcher.DIGIT.retainFrom(str1);
System.out.println("1:" +retainFrom);
// 2.把多个空格替换,并去掉首位的空格
String str2 = " Mahesh Parashar ";
String collapseFrom = CharMatcher.WHITESPACE.trimAndCollapseFrom(str2, ' ');
System.out.println("2:" +collapseFrom);
// 3.去掉转义字符(\t,\n,\b...)
String str3 = " ab\tcd\nef\bg a";
String removeFrom = CharMatcher.JAVA_ISO_CONTROL.removeFrom(str3);
String collapseFrom1 = CharMatcher.WHITESPACE.trimAndCollapseFrom(removeFrom, ' ');
System.out.println("3:" +collapseFrom1);
// ★4.把所有的数字用"*"代替
String str4 = "124abc85dds";
String replaceFrom = CharMatcher.JAVA_DIGIT.replaceFrom(str4, "*");
System.out.println("4:" +replaceFrom);
// 5.获取所有的数字和小写字母
String str5 = "124abc85ddsAF1HNsd";
String retainFrom1 = CharMatcher.JAVA_DIGIT.or(CharMatcher.JAVA_LOWER_CASE).retainFrom(str5);
System.out.println("5:" +retainFrom1);
// 7.获取所有单字节长度的符号
String str7 = ",dg,123AH中国";
String retainFrom2 = CharMatcher.SINGLE_WIDTH.retainFrom(str7);
System.out.println("7:" +retainFrom2);
// 8.获取字母
String str8 = "FirstName LastName +1 123 456 789 !@#$%^&*()_+|}{:\"?><";
String retainFrom3 = CharMatcher.JAVA_LETTER.retainFrom(str8);
System.out.println("8:" +retainFrom3);
// 9.获取字母和数字
String retainFrom4 = CharMatcher.JAVA_LETTER_OR_DIGIT.retainFrom(str8);
System.out.println("9:" +retainFrom4);
// 10.出现次数统计
int count = CharMatcher.ANY.countIn(str8);
System.out.println("10:" +count);
// 11.数字出现次数
int count1 = CharMatcher.DIGIT.countIn(str8);
System.out.println("11:" +count1);
// 12.获得除小写字母外其他所有字符
String retainFrom5 = CharMatcher.JAVA_LOWER_CASE.negate().retainFrom(str8);
System.out.println("12:" +retainFrom5);
}
运行结果如下:
equal()
比较不用担心空指针异常@Test
public void test1(){
//Objects.equal,不用担心空指针异常
System.out.println(Objects.equal("a", "a"));
System.out.println(Objects.equal(null, "a"));
System.out.println(Objects.equal("a", null));
System.out.println(Objects.equal(null, null));
}
运行结果:
null
/**
* 计算所有员工总年龄
*
* 不在担心对象为null
*/
@Test
public void test3() {
final List<Employee> list = Lists.newArrayList(new Employee("emp1", 30),
new Employee("emp2", 40),
null,
new Employee("emp4", 18));
int sum = 0;
for (Employee employee : list) {
sum += Optional.fromNullable(employee).or(new Employee("empx", 0)).getAge();
}
System.out.println(sum);
}
如上,遍历时能过滤null
对象。
@Test
public void testMap() {
Map<Integer, String> logfileMap = Maps.newHashMap();
logfileMap.put(1, "a.log");
logfileMap.put(2, "b.log");
logfileMap.put(3, "c.log");
System.out.println("logfileMap:" + logfileMap);
Map<String, Integer> integerMap = BimapDemo.reverseMap(logfileMap);
System.out.println(integerMap);
}
显示效果如下:
如果key
值相同怎么处理呢,Guava对于key
值相同的处理方法:
@Test
public void testMap1() {
Map<Integer, String> logfileMap = Maps.newHashMap();
logfileMap.put(1, "a.log");
logfileMap.put(2, "b.log");
logfileMap.put(3, "c.log");
logfileMap.put(4, "c.log");
System.out.println("logfileMap:" + logfileMap);
Map<String, Integer> inverseMap = BimapDemo.reverseMap(logfileMap);
System.out.println("reverseMap:" + inverseMap);
}
显示结果如下:
logfileMap:{1=a.log, 2=b.log, 3=c.log, 4=c.log}
reverseMap:{c.log=4, b.log=2, a.log=1}
当然Guava
还有很多处理Map
的数据结构和方法。有兴趣的可以自行研究。
还有个有趣的方法,如果value
相同,不让Map
存入值。
@Test
public void testBimap1() {
BiMap<Integer, String> logfileMap = HashBiMap.create();
logfileMap.put(1, "a.log");
logfileMap.put(2, "b.log");
logfileMap.put(3, "c.log");
logfileMap.put(4, "c.log");
}
如上图,c.long
禁止存入。
好了,关于Guava
的介绍就到这里了。
本文分享自 Lvshen的技术小屋 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与?腾讯云自媒体同步曝光计划? ,欢迎热爱写作的你一起参与!