標籤:rri ges 容器 exception str 運行 建立對象 als cep
一、目錄
1、唯讀設定
2、函數式編程+組合式編程
3、約束條件
4、集合操作(並集、差集、交集)
二、代碼實現
1、唯讀設定
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"); 運行報錯
}
2、函數式編程
(1)函數一:找出集合眾的迴文字串,迴文又稱 mirror word ,backword,是指字串從前面或者後面讀都是一樣的,比如moom
//結果: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)函數二:日期轉換
//結果: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)函數三:組合式編程
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); } }
3、約束條件
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 }
4、交集、並集、差集
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+" "); } }
Java容器-引入Guava類庫