Java容器-引入Guava類庫

目錄

一、只讀設置java

二、函數式編程+組合式編程編程

三、約束條件app

四、集合操做(並集、差集、交集)ide

代碼實現

一、只讀設置函數式編程

public static void main(String [] args){ //只讀設置
        List ls=new ArrayList(); ls.add("a"); ls.add("b"); ls.add("c"); //不使用guava的類庫
        List <String > readList= Collections.unmodifiableList(ls); //readList.add("d"); 報錯 //使用guava的類庫
        List<String> imutableList= ImmutableList.of("a","b","c"); //imutableList.add("a"); 運行報錯
}

二、函數式編程函數

(1)函數一:找出集合衆的迴文字符串,迴文又稱 mirror word ,backword,是指字符串從前面或者後面讀都是同樣的,好比moom ui

//結果:moom 由於moon逆序之後仍是moom
    public static void main(String[] args) { List<String> list = Lists.newArrayList("dog", "cat", "pig", "moom"); Collection<String> pList=Collections2.filter(list, new Predicate<String>() { public boolean apply(String s) { return new StringBuilder(s).reverse().toString().equals(s); } }); // 匿名內部類,同時建立對象,Collections2.filter相似過濾器
        for(Object o:pList){ System.out.println(o); } }

(2)函數二:日期轉換spa

//結果:1970-01-01    1970-01-24   1970-01-02
 public static void main(String [] args){ Set<Long> timeSet= Sets.newHashSet(); timeSet.add(1000L); timeSet.add(2000L*1000000); timeSet.add(3000L*20000); Collection<String> transList= Collections2.transform(timeSet, new Function<Long, String>() { public String apply(Long input) { return new SimpleDateFormat("yyyy-MM-dd").format(input); } }); for(String s:transList){ System.out.println(s); } }

(3)函數三:組合式編程code

public static void main(String [] args){ List<String> list = Lists.newArrayList("happy", "sad", "wahaha"); //方法一
        Function<String,String>f1=new Function<String, String>() { public String apply(String s) { return s.length()>5&&s.length()<20?s:"error"; } }; //方法二:字母所有大寫
        Function<String, String> f2 = new Function<String, String>() { public String apply(String input) { return input.toUpperCase(); } }; //組合方法
        Function<String, String> f = Functions.compose(f1, f2); Collection resultCol=Collections2.transform(list,f); for(Object s:resultCol){ System.out.println(s); } }

 三、約束條件orm

 public static void main(String[] args) { Set<String> sets = Sets.newHashSet(); // 建立約束
        Constraint<String> constraint = new Constraint<String>() { @Override public String checkElement(String element) { // 非空驗證
 Preconditions.checkNotNull(element); // 長度限制 5-20,不然報錯
 Preconditions.checkArgument( element.length() >= 5 && element.length() <= 20, element); return element; } }; Set<String> cs = Constraints.constrainedSet(sets, constraint); // cs.add(null); 報錯java.lang.NullPointerException //cs.add("qaz"); 報錯java.lang.IllegalArgumentException: qaz
    }

 四、交集、並集、差集

 public static void main(String [] args){ Set<Integer> sets=Sets.newHashSet(1,2,3,4,5,6); Set<Integer> set2=Sets.newHashSet(3,4,5,6,7,8,9); Sets.SetView<Integer> intersection =Sets.intersection(sets, set2); for(Integer in:intersection){ System.out.print(in+"  "); } System.out.println(""); //差集
        Sets.SetView<Integer> intersection2=Sets.difference(sets,set2); for(Integer in:intersection2){ System.out.print(in+"  "); } System.out.println(""); //並集
        Sets.SetView<Integer> intersection3=Sets.union(sets,set2); for(Integer in:intersection3){ System.out.print(in+"  "); } }

相關文章
相關標籤/搜索