Guava學習(八):集合幫助類

guava中的集合幫助類主要有:Collections2,Lists,Sets,Maps,Ints,Strings等基本集合類型對應的幫助類及guava的新集合類型對應的幫助類,類名基本是對應類後面加個s。
其中Lists,Sets的生產集合的方法可以再創建的時候就傳入元素,使用很方便,Sets可以對對兩個Set求交集、並集、差集,在某些場景下很有用,其他很多方法有興趣可以看看。
在這裏插入圖片描述

使用例子:

package com.bluedragon.guavalearning.collectiontool;

import com.google.common.collect.*;
import com.google.common.primitives.Ints;

import java.util.*;

/**
 * @author CodeRush
 * @date 2019/8/7 0:02
 */
public class CollectionToolTest {

    public static void main(String[] args) {
        /*
         * Coolection2
         */
        ArrayList<Integer> integers = Lists.newArrayList(10, 2, 5, 7, 11, 6, 3, 2, 8, 1, 0);
        Collection<List<Integer>> permutations = Collections2.permutations(integers);
        System.out.println(permutations);
        Collection<List<Integer>> orderedPermutations = Collections2.orderedPermutations(integers);
        System.out.println(orderedPermutations);
        //轉換 轉換成其他類型的集合
        Collection<Double> transform = Collections2.transform(integers, Integer::doubleValue);
        System.out.println(transform);
        //java8後這個作用不大
        Collection<Integer> filter = Collections2.filter(integers, input -> input < 9);
        System.out.println(filter);

        //把一個字符串分割成字符List
        ImmutableList<Character> characters = Lists.charactersOf("hello world");
        System.out.println(characters);
        //反轉元素順序
        List<Character> reverseCharacters = Lists.reverse(characters);
        System.out.println(reverseCharacters);
        List<Integer> integers1 = Lists.asList(1, new Integer[]{5, 4, 3});
        System.out.println(integers1);
        List<String> transform1 = Lists.transform(reverseCharacters, c -> c + c.toString());
        System.out.println(transform1);
        //更加語義化的方法
        ArrayList<Object> objects = Lists.newArrayListWithCapacity(5);
        List<List<String>> partition = Lists.partition(transform1, 3);
        System.out.println(partition); //[[dd, ll, rr], [oo, ww,   ], [oo, ll, ll], [ee, hh]

        HashSet<Integer> set1 = Sets.newHashSet(1, 3, 5, 3, 7, 9, 6, 8);
        HashSet<Integer> set2 = Sets.newHashSet(2, 4, 6, 8, 10);
        Set<List<Integer>> cartesianProduct = Sets.cartesianProduct(set1);
        System.out.println(cartesianProduct);
        //交集
        Sets.SetView<Integer> intersection = Sets.intersection(set1, set2);
        System.out.println(intersection);
        //並集
        Sets.SetView<Integer> union = Sets.union(set1, set2);
        System.out.println(union);
        //差集
        Sets.SetView<Integer> difference = Sets.difference(set1, set2);
        System.out.println(difference);
        HashMap<String, Boolean> map = Maps.newHashMap();
        Set<String> setFromMap = Sets.newSetFromMap(map);
        setFromMap.add("aa");
        setFromMap.add("bb");
        setFromMap.add("cc");
        System.out.println(setFromMap);
        System.out.println(map);

        int[] ints = Ints.toArray(integers);
        System.out.println(Ints.contains(ints, 3));
        String join = Ints.join("-", ints);
        System.out.println(join);
        System.out.println(Ints.max(ints));
        System.out.println(Ints.min(ints));

        //根據Iterable子類及一個函數轉換成ImmutableMap
        ImmutableMap<Integer, String> integerStringImmutableMap = Maps.toMap(set1, input -> "value" + input);
        System.out.println(integerStringImmutableMap);
    }
}

輸出結果:

permutations([10, 2, 5, 7, 11, 6, 3, 2, 8, 1, 0])
orderedPermutationCollection([0, 1, 2, 2, 3, 5, 6, 7, 8, 10, 11])
[10.0, 2.0, 5.0, 7.0, 11.0, 6.0, 3.0, 2.0, 8.0, 1.0, 0.0]
[2, 5, 7, 6, 3, 2, 8, 1, 0]
[h, e, l, l, o,  , w, o, r, l, d]
[d, l, r, o, w,  , o, l, l, e, h]
[1, 5, 4, 3]
[dd, ll, rr, oo, ww,   , oo, ll, ll, ee, hh]
[[dd, ll, rr], [oo, ww,   ], [oo, ll, ll], [ee, hh]]
[[1], [3], [5], [6], [7], [8], [9]]
[6, 8]
[1, 3, 5, 6, 7, 8, 9, 2, 10, 4]
[1, 3, 5, 7, 9]
[aa, bb, cc]
{aa=true, bb=true, cc=true}
true
10-2-5-7-11-6-3-2-8-1-0
11
0
{1=value1, 3=value3, 5=value5, 6=value6, 7=value7, 8=value8, 9=value9}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章