集合框架的擴展 guava(Guava對JDK集合的擴展)

一,對集合設置只讀java

List<String> list 
            = new ArrayList<String>();
       list.add("a");
       list.add("b");
       list.add("c");
       //對比查看初始化list grava 對只讀設置安全可靠,而且相對簡單
       List<String> immutableList = ImmutableList.of("A","B","C");
       immutableList.add("c");
      System.out.println(immutableList);//java.lang.UnsupportedOperationException
    }

二,過濾器,更好的與集合類解耦
1,查看集合中的迴文單詞,字符
注意:
若是一個類只使用一次,而且這個類 的對象也只是用一次,那麼咱們就是用匿名內部類
工具:Collections2.filter 過濾器編程

函數式編程
//List的靜態初始化 
     List<String> list 
          = Lists.newArrayList("ab","bcb","cd","son","mom");
     //找出迴文 mirror words
     //匿名內部類的對象:匿名內部類,同時建立類對象
     Collection<String> parlidromeList = Collections2.filter(list, new Predicate<String>(){
        @Override
        public boolean apply(String input) {
            //業務邏輯
            return new StringBuilder(input).reverse().toString().equals(input);
        }
     });
     for(String tem:parlidromeList){
         System.out.println(tem);
     }
    }

2,確保容器中字符串的長度不超過5,超過進行截取,而且所有大寫安全

//組合式函數編程
       //確保容器中字符串的長度不超過5,超過進行截取,而且所有大寫
        List<String> lists = Lists.newArrayList("good","happy","wonderful");
        //確保容器中字符串的長度不超過5,超過進行截取
        Function<String,String> f1 
              = new Function<String,String>(){
                @Override
                public String apply(String input) {
                    return input.length()>5?input.substring(0,5):input;
                }
        };
        //轉成大寫
        Function<String,String> f2 
          = new Function<String,String>(){
            @Override
            public String apply(String input) {
                return input.toUpperCase();
         }
    };
    //如今須要將f1和f2組合在一塊兒 String combinedStr = f2(f1(String))
    Function<String,String> f
           = Functions.compose(f1, f2);
    Collection<String> resultCol 
           = Collections2.transform(lists, f);
    for(String tem:resultCol){
        System.out.println(tem);
    }        
}

3,添加約束條件(非空,長度驗證)app

Set<String> st = Sets.newHashSet();
      //建立約束
      Constraint<String> constraint 
             = new Constraint<String>(){

        @Override
        public String checkElement(String element) {
            //非空驗證
            Preconditions.checkNotNull(element);
            //長度驗證
            Preconditions.checkArgument(element.length()>=5 && element.length()<=20);
            return element;
        }
      };
      Set<String> cs 
          = Constraints.constrainedSet(st, constraint);
      //cs.add("good");//java.lang.IllegalArgumentException
      //cs.add("");//java.lang.IllegalArgumentException
      //cs.add(null);//java.lang.NullPointerException
      cs.add("wonderful");//添加成功
      System.out.println(cs);
      
    }

4,集合的操做:交集,差集,並集ide

Set<Integer> set1 = Sets.newHashSet(1,2,3,4,6);
    Set<Integer> set2 = Sets.newHashSet(2,4,6,7);
    //交集
    System.out.println("交集爲:");
    SetView<Integer> intersections 
         = Sets.intersection(set1, set2);
    System.out.println(intersections);
    //差集
    System.out.println("差集爲:");
    SetView<Integer> diff 
        = Sets.difference(set1, set2);
    System.out.println(diff);
    //並集
    System.out.println("並集爲:");
    SetView<Integer> union 
        = Sets.union(set1, set2);
    System.out.println(union);
   }
相關文章
相關標籤/搜索