Guava---collection集合用法

目录

    • List
    • Set
    • Map
    • table

List

  • 创建
	//初始化
	    ImmutableList of = ImmutableList.of(1, 2, 3);
        LinkedList linkedList = Lists.newLinkedList();
        ArrayList list2 = Lists.newArrayList();
        //处始化大小
        ArrayList list3 = Lists.newArrayListWithCapacity(10);
        //处始化,并添加元素
        String[] ss = {"2"};
        List list = Lists.asList("1", ss);
        ArrayList list4 = Lists.newArrayList("A","B");
 
  
  • 常用方法介绍

        //笛卡儿积
        List> list5 = Lists.cartesianProduct(Lists.newArrayList("A","B"), Lists.newArrayList("1","2"));
        System.out.println(list5);//[[A, 1], [A, 2], [B, 1], [B, 2]]

        //元素转换
        List transform = Lists.transform(Lists.newArrayList("A","B"), new Function() {
            @Nullable
            @Override
            public String apply(@Nullable String input) {
                return input.toLowerCase();
            }
        });
        System.out.println(transform);//[a, b]

        //字符串转化为字符,返回ImmutableList,Immutable是不可变对象
        ImmutableList list6 = Lists.charactersOf("mnbvcxz");
        System.out.println(list6);//[m, n, b, v, c, x, z]

        //reversed
        List reverse = Lists.reverse(list6);
        System.out.println(reverse);//[z, x, c, v, b, n, m]

        //按个数分组
        List> partition = Lists.partition(reverse, 3);
        System.out.println(partition);//[[z, x, c], [v, b, n], [m]]

上述方法,其实实际并未将元素改变,是在正在需要拿元素的时候,才将相关元素按照指定变化。这是一个很重要思想,避免了一开始就遍历元素一个个将元素变化

  • list存在null排序问题
        /**
         * jdk 排序
         *
         * 但是存在一个问题,不能为null,否则将抛出异常
         */
        List list = Arrays.asList(1, 3, 2, 4,null);
        System.out.println(list);
        Collections.sort(list);
        System.out.println(list);

上述代码会抛错,因为存在null无法排序,这时需要传递一个Comparator进去、
guava为我们提供了Ordering类

        List list = Arrays.asList(1, 3, 2, 4,null);
        System.out.println(list);
        //将null放到第一个
        Collections.sort(list, Ordering.natural().nullsFirst());
        System.out.println(list);
        //判断list是否是有序
         System.out.println(Ordering.natural().nullsFirst().isOrdered(list));
        //倒序
		//Ordering.natural().reverse().nullsFirst()

Set

  • 创建
HashSet set = Sets.newHashSet(1, 2, 3);
HashSet set1 = Sets.newHashSet(Lists.newArrayList(1, 1, 2, 3, 4));
  • 常用方法
 	//子集,size表示每一个子集的个数
        Set> set2 = Sets.combinations(Sets.newHashSet(1, 2, 3), 2);
        set2.forEach(System.out::print);//[1, 2][1, 3][2, 3]
        //差集,交集,并集

Map

  • map
	 //map构建
        HashMap hashMap = Maps.newHashMap();
        Maps.newHashMapWithExpectedSize(5);
        ConcurrentMap concurrentMap = Maps.newConcurrentMap();//线程安全map
        ImmutableMap immutableMap = ImmutableMap.builder()
                .put(1, "文文是猪").build();

        //map初始化并赋值
        Map map = Maps.asMap(Sets.newHashSet(1, 2, 3), k -> k + "_value");
        System.out.println(map);//{1=1_value, 2=2_value, 3=3_value}
        ImmutableMap map1 = Maps.uniqueIndex(Lists.newArrayList(1, 2, 3), v -> v + "_key");
        System.out.println(map1);//{1_key=1, 2_key=2, 3_key=3}

        //map修改value
        Map map2 = Maps.transformValues(map, v -> v + "transformValues");
        System.out.println(map2);//{1=1_valuetransformValues, 2=2_valuetransformValues, 3=3_valuetransformValues}

        //按key过滤
        Map map3 = Maps.filterKeys(map, key -> key == 1);
        System.out.println(map3);//{1=1_value}
  • Multimap
        /**
         * LinkedListMultimap
         * 

* 非线程安全 * 可以存储相同key * get方法返回的是list *

*

* 线程安全使用: * Multimap multimap= Multimaps.synchronizedMultimap(LinkedListMultimap.create()); *

* * */ LinkedListMultimap multimap = LinkedListMultimap.create(); multimap.put("1", "2"); multimap.put("1", "2"); multimap.put("1", "3"); multimap.put("1", "4"); System.out.println(multimap);//{1=[2, 2, 3, 4]} /** * HashMultimap *

* 非线程安全 * 可以存储相同key * 相同key value覆盖 * get方法返回的是set *

*

* 线程安全使用: * Multimap hashMultimap= Multimaps.synchronizedMultimap(HashMultimap.create()); *

*/ HashMultimap hashMultimap = HashMultimap.create(); hashMultimap.put("1", "2"); hashMultimap.put("1", "2"); hashMultimap.put("1", "3"); hashMultimap.put("1", "4"); System.out.println(hashMultimap);//{1=[4, 2, 3]}
  • BiMap
        /**
         * 非线程安全
         * 不能存在相同key,也不能存在相同value
         * 
         * 线程安全使用: Maps.synchronizedBiMap(biMap)
         */
        //相同key覆盖,相同value抛错
        HashBiMap biMap = HashBiMap.create();
        biMap.put("11","2");
        biMap.put("1","3");
        System.out.println(biMap);//{11=2, 1=3}
        try {
            biMap.put("2","3");
        } catch (Exception e) {
            e.printStackTrace();//java.lang.IllegalArgumentException: value already present: 3
        }
        //强行加元素,若value和之前的相同,则覆盖之前的entry
        biMap.forcePut("3","3");
        System.out.println(biMap);//{11=2, 3=3}
  • NavigableMap和TreeRangeMap
        //[]
        Range closed = Range.closed(1, 5);
        //()
        Range open = Range.open(1, 5);
        //(]
        Range openClosed = Range.openClosed(1, 5);
        //[)
        Range closedOpen = Range.closedOpen(1, 5);

        TreeMap treeMap = Maps.newTreeMap();
        treeMap.put(1,"111");
        treeMap.put(3,"333");
        treeMap.put(2,"222");
        treeMap.put(4,"444");
        System.out.println(treeMap);//{1=111, 2=222, 3=333, 4=444}

        NavigableMap subMap = Maps.subMap(treeMap, Range.closed(1, 3));
        System.out.println(subMap);//{1=111, 2=222, 3=333}


        TreeRangeMap rangeMap = TreeRangeMap.create();
        rangeMap.put(Range.closed(90,100),"A");
        rangeMap.put(Range.closed(60,89),"B");
        System.out.println(rangeMap);//[[60..89]=B, [90..100]=A]
        System.out.println(rangeMap.get(99));//A

table

        //Map>
        HashBasedTable table = HashBasedTable.create();
        table.put(1,2,3);
        System.out.println(table);
        table.put(1,2,4);
        System.out.println(table);
        Object o = table.get(1, 2);
        Map row = table.row(1);
        Map column = table.column(2);

guava的collection实在是太多了,这里介绍一部分用法。后续学习数据结构把实现原理及用到的算法详细介绍一遍,以及整个的设计的设计模式和UML图,待续。。。。。

你可能感兴趣的:(Guava)