guava的一些用法

  1 package guavaTest;
  2 
  3 import com.google.common.base.CharMatcher;
  4 import com.google.common.base.Joiner;
  5 import com.google.common.base.Splitter;
  6 import com.google.common.collect.ArrayListMultimap;
  7 import com.google.common.collect.Lists;
  8 import com.google.common.primitives.Ints;
  9 import com.google.common.util.concurrent.*;
 10 import org.checkerframework.checker.nullness.compatqual.NullableDecl;
 11 
 12 import java.util.List;
 13 import java.util.Random;
 14 import java.util.concurrent.ExecutorService;
 15 import java.util.concurrent.Executors;
 16 
 17 /**
 18  * Google guava 的使用
 19  */
 20 public class GuavaTest {
 21 
 22     //连接器(对于Joiner,常用的方法是   跳过NULL元素:skipNulls()  /  对于NULL元素使用其他替代:useForNull(String))
 23     // private static final Joiner joiner = Joiner.on(",").skipNulls();
 24     private static final Joiner joiner = Joiner.on(",").useForNull("我是空字符串");
 25 
 26     //分割器(对于Splitter,常用的方法是:trimResults()/omitEmptyStrings()。注意拆分的方式,有字符串,还有正则,还有固定长度分割(太贴心了!))
 27     private static final Splitter splitter = Splitter.on(",").trimResults().omitEmptyStrings();
 28 
 29 
 30     public static void main(String[] args) {
 31 
 32         String join = joiner.join(Lists.newArrayList("a", null, "b"));
 33         System.out.println("join:" + join);
 34 
 35         // a,  ,b,,
 36         for (String tmp : splitter.split("bb,lll,kkk,,,,,,,,")) {
 37             System.out.println("|" + tmp + "|");
 38         }
 39 
 40         List integers = Ints.asList(1, 2, 3, 4, 5, 6);
 41 
 42         System.out.println(Ints.join(",", 1, 2, 3, 4));
 43 
 44         //原生数组类型快速合并
 45         int[] concat = Ints.concat(new int[]{1, 2, 3}, new int[]{7, 8, 9});
 46         System.out.println(concat.length);
 47 
 48         //求最大、最小
 49         System.out.println(Ints.max(concat) + "" + Ints.min(concat));
 50 
 51         //是否包含,不用循环查找了
 52         System.out.println(Ints.contains(concat, 9));
 53 
 54         //集合到数组的转换
 55         System.out.println(Ints.toArray(integers));
 56 
 57         //Map>
 58         ArrayListMultimap listMultimap = ArrayListMultimap.create();
 59 
 60         listMultimap.put("张晓明", "数学");
 61         listMultimap.put("张晓明", "语文");
 62         listMultimap.put("张晓明", "物理");
 63         listMultimap.put("王小二", "英语");
 64 
 65         test();
 66 
 67     }
 68 
 69 
 70     public static void test() {
 71 
 72         ExecutorService service = Executors.newFixedThreadPool(10);
 73         ListeningExecutorService listeningExecutorService = MoreExecutors.listeningDecorator(service);
 74         ListenableFuture listenableFuture = listeningExecutorService.submit(() -> {
 75 
 76             if (new Random().nextInt(3) == 2) {
 77                 throw new NullPointerException();
 78             }
 79             return 1;
 80 
 81         });
 82 
 83         FutureCallback futureCallback = new FutureCallback() {
 84 
 85             @Override
 86             public void onSuccess(@NullableDecl Integer result) {
 87                 System.out.println("---------" + result);
 88             }
 89 
 90             @Override
 91             public void onFailure(Throwable t) {
 92                 System.out.println("------------" + t.getMessage());
 93             }
 94 
 95 
 96         };
 97 
 98         Futures.addCallback(listenableFuture, futureCallback);
 99 
100 
101     }
102 
103 }

 

转载于:https://www.cnblogs.com/java-le/p/11383776.html

你可能感兴趣的:(guava的一些用法)