Google-Guava(Utilites)
Posted 肖帆咪
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Google-Guava(Utilites)相关的知识,希望对你有一定的参考价值。
由 Google 公司开源,Guava是一种基于开源的Java库,Google Guava源于2007年的"Google Collections Library"。这个库是为了方便编码,并减少编码错误。这个库用于提供集合,缓存,支持原语句,并发性,常见注解,字符串处理,I/O和验证的实用方法。
Guava Utilites常用API
1. Joiner
String res = Joiner.on("#").join(要拼接的集合);
assertThat(res,equalTo(拼接后的结果));
//跳过NUll拼接
Joiner.on("#"),skipNUlls().join(要拼接的集合);
//碰到null都被替换成别的
Joiner.on("#"),useForNull("Defult").join(要拼接的集合);
//返回的值和要拼接的是同一个对象
StringBuilder resultBuilder = Joiner.on("->").useForNull("*").appendTo(new StringBuilder("E"), stringListWithNullValue);
//写入文档中
private final String targetFileName = "D:\\\\Users\\\\Desktop\\\\guava\\\\guava-test.txt";
try (FileWriter writer = new FileWriter(new File(targetFileName)))
Joiner.on("->").useForNull("*").appendTo(writer, stringListWithNullValue);
assertThat(Files.isFile().test(new File(targetFileName)), equalTo(true));
catch (IOException e)
fail("append to the writer occur fetal error");
//stream流 拼接
stringListWithNullValue.stream().filter(item -> item != null && !item.isEmpty()).collect(Collectors.joining("->"));
//stream流拼接使用默认替代null值
stringListWithNullValue.stream()
.map(item -> item == null || item.isEmpty() ? "*" : item).collect(Collectors.joining("->"));
//拼接map集合
private final Map<String, String> stringMap = ImmutableMap.of("I", "am", "you", "is");
Joiner.on("->").withKeyValueSeparator("=").join(stringMap);
//拼接map集合并写入txt文件
try (FileWriter writer = new FileWriter(new File(targetFileNameToMap)))
Joiner.on("->").withKeyValueSeparator("=").appendTo(writer, stringMap);
assertThat(Files.isFile().test(new File(targetFileNameToMap)), equalTo(true));
catch (IOException e)
fail("append to the writer occur fetal error");
2. Splitter
public void testslitSplit()
List<String> result = Splitter.on("|").splitToList("A|B|C");
assertThat(result, notNullValue());
assertThat(result.size(), equalTo(3));
assertThat(result.get(0), equalTo("A"));
assertThat(result.get(1), equalTo("B"));
assertThat(result.get(2), equalTo("C"));
public void testsplit_On_Split_OmitEmpty()
List<String> result = Splitter.on("|").splitToList("A|B|C||");
assertThat(result, notNullValue());
assertThat(result.size(), equalTo(5));
//忽略空值
result = Splitter.on("|").omitEmptyStrings().splitToList("A|B|C||");
assertThat(result.size(), equalTo(3));
public void testsplit_On_Split_OmitEmpty_TrimResult()
List<String> result = Splitter.on("|").omitEmptyStrings().splitToList("A | B|C||");
assertThat(result.size(), equalTo(3));
assertThat(result.get(0), equalTo("A "));
assertThat(result.get(1), equalTo(" B"));
assertThat(result.get(2), equalTo("C"));
//trimResults() 去掉空格
result = Splitter.on("|").trimResults().omitEmptyStrings().splitToList("A | B|C||");
assertThat(result.size(), equalTo(3));
assertThat(result.get(0), equalTo("A"));
assertThat(result.get(1), equalTo("B"));
assertThat(result.get(2), equalTo("C"));
public void testsplitFixLength()
List<String> result = Splitter.fixedLength(2).splitToList("aabc");
assertThat(result, notNullValue());
assertThat(result.size(), equalTo(2));
assertThat(result.get(0), equalTo("aa"));
assertThat(result.get(1), equalTo("bc"));
public void testsplitOnSplitLimit()
List<String> result = Splitter.on("#").limit(3).splitToList("A#B#C#D");
assertThat(result, notNullValue());
assertThat(result.size(), equalTo(3));
assertThat(result.get(0), equalTo("A"));
assertThat(result.get(1), equalTo("B"));
assertThat(result.get(2), equalTo("C#D"));
public void testsplitOnPatternString()
//支持正则表达式分割(传递一个字符串)
List<String> result = Splitter.onPattern("\\\\|").trimResults().omitEmptyStrings().splitToList("A|B|C||");
assertThat(result, notNullValue());
assertThat(result.size(), equalTo(3));
assertThat(result.get(0), equalTo("A"));
assertThat(result.get(1), equalTo("B"));
assertThat(result.get(2), equalTo("C"));
public void testsplitOnPattern()
//支持正则表达式分割(传递一个Pattern对象)
List<String> result = Splitter.on(Pattern.compile("\\\\|")).trimResults().omitEmptyStrings().splitToList("A|B|C||");
assertThat(result, notNullValue());
assertThat(result.size(), equalTo(3));
assertThat(result.get(0), equalTo("A"));
assertThat(result.get(1), equalTo("B"));
assertThat(result.get(2), equalTo("C"));
public void testsplitOnSplitToMap()
//分割装进Map中withKeyValueSeparator("=")
Map<String,String> result = Splitter.on(Pattern.compile("\\\\|")).trimResults()
.omitEmptyStrings().withKeyValueSeparator("=").split("A=a| B=b|C=c||");
assertThat(result, notNullValue());
assertThat(result.size(), equalTo(3));
assertThat(result.get("A"), equalTo("a"));
assertThat(result.get("B"), equalTo("b"));
assertThat(result.get("C"), equalTo("c"));
3. Preconditions
//检查是否为null
Preconditions.checkNotNull(list);
//检查不为null,并返回message
Preconditions.checkNotNull(list, "The list should not be null");
//返回带数字的message
Preconditions.checkNotNull(list, "The list should not be null and size must be %s", 2);
//判断参数类型
Preconditions.checkArgument();
//判断线程状态 IllegalStateException
Preconditions.checkState();
//判断索引
List<String> list = ImmutebleList.of("张三","李四");
Preconditions.checkElementIndex(10,list.size());
//请求的对象不能为null
Object.requireNonNull(null);
4. Obejects
import com.google.common.base.MoreObjects;
import com.google.common.collect.ComparisonChain;
import java.util.Calendar;
public class ObjectsTest
static class Guava implements Comparable<Guava>
private final String manufacturer;
private final String version;
private final Calendar releaseDate;
public Guava(String manufacturer, String version, Calendar releaseDate)
this.manufacturer = manufacturer;
this.version = version;
this.releaseDate = releaseDate;
@Override
public String toString()
return MoreObjects.toStringHelper(this).omitNullValues()
.add("manufacturer", this.manufacturer)
.add("version", this.version)
.add("releaseDate", this.releaseDate).toString();
@Override
public boolean equals(Object o)
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Guava guava = (Guava) o;
return Objects.equals(this.manufacturer, guava.manufacturer)
&& Objects.equals(this.version, guava.version)
&& Objects.equals(this.releaseDate, guava.releaseDate);
@Override
public int hashCode()
return com.google.common.base.Objects.hashCode(manufacturer, version, releaseDate);
@Override
public int compareTo(Guava o)
return ComparisonChain.start().compare(this.manufacturer, o.manufacturer)
.compare(this.version, o.version)
.compare(this.releaseDate, o.releaseDate).result();
public static void main(String[] args)
Guava guava1 = new Guava("Google", "23.0", Calendar.Google-Guava(Utilites)