【guava】集合

public class Solution {

    public static void main(String... arg) {
        //可变集合创建
        List<String> list = Lists.newArrayList();
        Set<String> set = Sets.newHashSet();
        Map<String, String> map = Maps.newHashMap();

        //不可变集合
        ImmutableList iList = ImmutableList.of(1, 2, 3);
        ImmutableList iList2 = ImmutableList.copyOf(list);//不受list改变影响
        ImmutableList immutableList3 = ImmutableList.builder()
                .add("1")
                .addAll(list)
                .build();

        //BiMap
        HashBiMap<String, Integer> hashBiMap = HashBiMap.create();
        hashBiMap.put("tome", 170);
        hashBiMap.get("tome");
        hashBiMap.inverse().get(170);
        hashBiMap.forcePut("jane", 170);//移出了k-v tome-170

        //Multimap
        HashMultimap<String, Integer> multimap = HashMultimap.create();
        multimap.put("key1", 1);
        multimap.put("key1", 2);
        multimap.put("key1", 3);
        Set<Integer> sets = multimap.get("key1");

        //Multiset
        HashMultiset<Integer> hashMultiset = HashMultiset.create(Lists.newArrayList(1, 1, 1, 2, 3));
        int count = hashMultiset.count(1);
        System.out.println(count);

        //交集、并集、差集
        Set<Integer> aSet = Sets.newHashSet(1, 2, 3, 4);
        Set<Integer> bSet = Sets.newHashSet( 3, 4, 5,6);
        Sets.intersection(aSet, bSet);
        Sets.union(aSet, bSet);
        Sets.difference(aSet, bSet);

    }
}

 

posted @ 2020-11-30 20:12  正义的五毛  阅读(53)  评论(0)    收藏  举报