Google Guava库

Google guava通了强大的工具类来简化代码。列举常用的集中类:

原文:https://ifeve.com/google-guava/

        //不可变集合
        ImmutableList<Integer> list = ImmutableList.of(1, 2, 3, 4);
        ImmutableMap<String, String> map = ImmutableMap.of("a", "b", "a1", "b1");

        //基本类型的比较
        int intc = Ints.compare(1, 2);
        int longc = Longs.compare(1L, 2L);

        //字符串拼接 skipNulls:过滤null,useForNull:null的替换
        int[] numbers = {1, 2, 3, 4, 5};
        String join = Joiner.on(";").skipNulls().useForNull("==").join(Ints.asList(numbers));
        String join1 = Joiner.on(";").skipNulls().join(Ints.asList(numbers));
        String join2 = Joiner.on(";").useForNull("==").join(Ints.asList(numbers));
        Ints.join(";", numbers);

        //分隔符拼接 omitEmptyStrings:去除分隔结果为空格的数据,trimResults去除分隔结果的前后空格
        String spiltString = "join,,tws,test,,";
        List<String> splitList = Splitter.on(",").omitEmptyStrings().trimResults().splitToList(spiltString);

        //Ints
        int[] array = {1, 2, 3, 4, 5};
        int a = 3;
        boolean contains = Ints.contains(array, a);
        int indexOf = Ints.indexOf(array, a);
        int max = Ints.max(array);
        int min = Ints.min(array);
        int[] concat = Ints.concat(array, array);

        //集合
        HashSet<Integer> setA = Sets.newHashSet(1, 2, 3);
        HashSet<Integer> setB = Sets.newHashSet(4, 5, 3);
        Sets.SetView<Integer> union = Sets.union(setA, setB); //交集
        Sets.SetView<Integer> difference = Sets.difference(setA, setB); //并集
        Sets.SetView<Integer> intersection = Sets.intersection(setA, setB); //差集

        Preconditions.checkArgument(1 > 2, "error");

        //Lists
        ArrayList<Object> listC = Lists.newArrayList();
        //集合分割
        List<List<Integer>> partition = Lists.partition(list, 200);
        //集合翻转
        List<Integer> reverse = Lists.reverse(list);

猜你喜欢

转载自blog.csdn.net/qq_28126793/article/details/83541183