guava 基礎(chǔ)工具類

Optional類

目的: 多用于返回值,明確null的語義,使用或者避免null
關(guān)鍵點: You (and others) are far more likely to forget that other.method(a, b) could return a null value than you're likely to forget that a could be null when you're implementing other.method.

  1. or(default T) 用默認值代替null
  2. isPresent() 判斷方法返回值, 函數(shù)式編程stream流的異常判斷
    ArrayList<String> sList = new ArrayList<String>(16);
    sList.add(null);

        Optional<String> ops = Optional.fromNullable(sList.get(0));
        ops.isPresent();
        ops.of(null);
        String def = "無";
        System.out.println(ops.or(def));

Preconditions 類

應(yīng)用場景: 檢測方法參數(shù)或者運行方法時的狀態(tài)的合理性
方式: 在不滿足條件的情況下,拋出非受檢異,拋出IllegalArgumentException
IllegalStateException,NPE 等,給出對應(yīng)message
局限: 拋出均為包裝后的非受檢異常,無法捕獲處理,程序會終止

  1. Preconditions.checkArgument()
  2. Preconditions.checkState()
  3. Preconditions.checkNotNull()
    private  void arguementCheck(int i, int j){
        Preconditions.checkArgument( i > j, "參數(shù)異常,期望參數(shù): i > j:傳入?yún)?shù) i:%s < j:%s ",i,j);
    }
    @Test
    public void testArguement(){
        arguementCheck(2,5);
    }

Ordering 類(collect包)

應(yīng)用場景: 獲取多個最大最小值,多個比較,有序拷貝,反向,鏈式排序

  1. reverse()
  2. greatestOf()
  3. leastOf()
  4. List<> sortCopy()
  5. onResultOf(Function<T,R>)
private Ordering<People> nameOrder
            = Ordering.natural().onResultOf(new Function<People,Integer>(){
        @Override
        public Integer apply(People p){
            return p.height + p.weight;
        }
    });
    @Test
    public void testHeightWeightOrder(){
        List<People> pl = Lists.newArrayList(
                new People("ren",16,15),
                new People("zhang",15,17),
                new People("wang",17,13)
        );
        System.out.println(nameOrder.sortedCopy(pl));
        System.out.println(nameOrder.reverse().sortedCopy(pl));
        System.out.println(nameOrder.greatestOf(pl,2));
        System.out.println(nameOrder.leastOf(pl,2));
    }

MoreObject.ToStringHelper 類

應(yīng)用場景: key value格式化 toString()函數(shù)

  1. 構(gòu)造器 MoreObject.toStringHelper()
  2. add(String V value)
class People{
    String name;
    int height;
    int weight;
    public People(String name,int height,int weight){
        this.name = name;
        this.height = height;
        this.weight = weight;
    }
    public String toString(){
        return MoreObjects.toStringHelper(this)
                .add("名字",name)
                .add("身高",height)
                .add("體重",weight)
                .toString();
    }
}

Splitter類

應(yīng)用場景: 條件分割,去除空格,去除空值

  1. on() 字符,字符串,Pattern
  2. trimResult()
  3. omitEmptyStrings()
  4. fixedLength()
  5. Iterable<> split()
  6. List<> splitToList()
    @Test
    public void testStringSplit(){
        print(Splitter
                .on("|")
                .split("cat filename   | grep '.*'   ||   wc -l|"));
        print(Splitter
                .on("|")
                .trimResults()
                .split("cat filename   || grep '.*'   |   wc -l|"));
        print(Splitter
                .on("|")
                .trimResults()
                .omitEmptyStrings()
                .split("cat filename   || grep '.*'   |   wc -l|"));
    }
    @Test
    public void testImmutable(){
        Splitter splitter = Splitter.on('/');
        splitter.trimResults(); // does nothing!
        // splitter = splitter.trimResults();
        print(splitter.split("wrong / wrong / wrong"));
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,659評論 19 139
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,754評論 18 399
  • 上一章:職場妖精修煉記(57)初生好感 這一次出差,我好好地玩了二十天才回福州。 回到福州,發(fā)現(xiàn)李振東居然不在公司...
    夏乙之閱讀 345評論 3 6

友情鏈接更多精彩內(nèi)容