Guava系列之超实用的集合工具类

x33g5p2x  于2021-12-18 转载在 其他  
字(9.5k)|赞(0)|评价(0)|浏览(329)

List、Set、Map是我们开发过程中使用频次最高的三种集合类型,今天我们来看一下Guava中对这三种类型的集合提供的工具类

Lists

主要方法有

  • 各种创建list的方法
  • asList()将数据组转成list
  • newArrayList()
  • newArrayListWithCapacity(10) 指定容量的创建
  • newArrayListWithExpectedSize(20) 初始化指定容量
  • newCopyOnWriteArrayList()
  • newLinkedList()
  • partition(List list, int size) 将list按指定大小分隔成多个list
  • cartesianProduct(List<? extends B>… lists) 获取多个list的笛卡尔集
  • charactersOf(String str) 将字符串转成字符集合
  • reverse(List list) 反转list
  • transform(List fromList, Function<? super F, ? extends T> function) 数据转换

各种创建list的方法

@Test
    public void ListCreateTest(){
        //将数组转成list,并在开头位置插入元素
        List<String> list = Lists.asList("a",new String[]{"b","c","d"});
        List<String> list1 = Lists.asList("a","b",new String[]{"c","d","e"});

        //直接创建ArrayList
        ArrayList<String> arrayList = Lists.newArrayList();
        //创建ArrayList,并初始化
        ArrayList<String> arrayList1 = Lists.newArrayList("a","b","c");
        //基于现有的arrayList,创建一个arrayList
        ArrayList<String> arrayList2 = Lists.newArrayList(arrayList1);
        //初始化指定容量大小的ArrayList,其中容量指ArrayList底层依赖的数组的length属性值,常用于提前知道ArrayList大小的情况的初始化
        ArrayList<String> arrayList3 = Lists.newArrayListWithCapacity(10);
        //初始化预定容量大小的ArrayList,返回的list的实际容量为5L + estimatedSize + (estimatedSize / 10),常用于不确定ArrayList大小的情况的初始化
        ArrayList<String> arrayList4 =Lists.newArrayListWithExpectedSize(20);
        //创建CopyOnWriteArrayList
        CopyOnWriteArrayList<String> copyOnWriteArrayList = Lists.newCopyOnWriteArrayList();
        //创建linkedList
        LinkedList<String> linkedList = Lists.newLinkedList();
    }

按指定大小分隔list

@Test
    public void partitionTest(){
        List<String> list = Lists.newArrayList("a","b","c","d","e");
        //将list按大小为2分隔成多个list
        List<List<String>> splitList = Lists.partition(list,2);
        System.out.println(splitList);

    }

笛卡尔集

@Test
    public void cartesianProcustTest(){
        List<String> list1 = Lists.newArrayList("a","b","c");
        List<String> list2 = Lists.newArrayList("d","e","f");
        List<String> list3 = Lists.newArrayList("1","2","3");
        //获取多个list的笛卡尔集
        List<List<String>> list = Lists.cartesianProduct(list1,list2,list3);
        System.out.println(list);
    }

字符串转成字符集合

@Test
    public void charactersOfTest(){
        //将字符串转成字符集合
        ImmutableList<Character> list = Lists.charactersOf("ababcdfb");
    }

list反转

@Test
    public void reverseTest(){
        List<String> list = Lists.newArrayList("a","b","c","1","2","3");
        //反转list
        List<String> reverseList = Lists.reverse(list);
        System.out.println(reverseList);
    }

数据转换

@Test
    public void transFormTest(){
        List<String> list = Lists.newArrayList("a","b","c");
        //把list中的每个元素拼接一个1
        List<String> list1 = Lists.transform(list,str -> str + "1");
        System.out.println(list1);
    }
Sets

主要方法有:

  • 各种创建set的方法

  • newHashSet()

  • newLinkedHashSet()

  • newTreeSet()

  • newConcurrentHashSet()

  • cartesianProduct(Set<? extends B>… sets) 笛卡尔集

  • combinations(Set set, final int size) 按指定大小进行排列组合

  • difference(final Set set1, final Set<?> set2) 两个集合的差集

  • intersection(final Set set1, final Set<?> set2) 交集

  • filter(Set unfiltered, Predicate<? super E> predicate) 过滤

  • powerSet(Set set) 获取set可分隔成的所有子集

  • union(final Set<? extends E> set1, final Set<? extends E> set2) 并集

创建各种set的方法

@Test
    public void setsCreate(){
        HashSet<String> set = Sets.newHashSet();
        Sets.newLinkedHashSet();
        Sets.newHashSetWithExpectedSize(10);
        Sets.newTreeSet();
        Sets.newConcurrentHashSet();
    }

笛卡尔集

@Test
    public void cartesianProduct(){
        Set<String> set1 = Sets.newHashSet("a","b","c");
        Set<String> set2 = Sets.newHashSet("1","2","3");
        Set<String> set3 = Sets.newHashSet("@","#","&");
        //多个Set的笛卡尔集,参数接收多个set集合
        Set<List<String>> sets = Sets.cartesianProduct(set1,set2,set3);
        System.out.println(sets);

        List<Set<String>> list = Lists.newArrayList(set1,set2,set3);
        //也可以把多个Set集合,放到一个list中,再计算笛卡尔集
        Set<List<String>> sets1 = Sets.cartesianProduct(list);
        System.out.println(sets1);
        //Sets.combinations()
        //Sets.difference()
    }

按指定大小进行排列组合

@Test
    public void combinationsTest(){
        //Set<String> set = Sets.new("a","b","c","d");
        //
        //ImmutableSet immutableSet = ImmutableSet.of("a","b","c","d");
        //将集合中的元素按指定的大小分隔,指定大小的所有组合
        Set<String> set1 = Sets.newHashSet("a","b","c","d");
        Set<Set<String>> sets = Sets.combinations(set1,3);
        for(Set<String> set : sets){
            System.out.println(set);
        }
    }

差集

@Test
    public void differenceTest(){
        Set<String> set1 = Sets.newHashSet("a","b","d");
        Set<String> set2 = Sets.newHashSet("d","e","f");
        //difference返回:从set1中剔除两个set公共的元素
        System.out.println(Sets.difference(set1,set2));
        //symmetricDifference返回:剔除两个set公共的元素,再取两个集合的并集
        System.out.println(Sets.symmetricDifference(set1,set2));
    }

交集

@Test
    public void intersectionTest(){
        Set<String> set1 = Sets.newHashSet("a","b","c");
        Set<String> set2 = Sets.newHashSet("a","b","f");
        //取两个集合的交集
        System.out.println(Sets.intersection(set1,set2));
    }

过滤

@Test
    public void filterTest(){
        Set<String> set1 = Sets.newHashSet("a","b","c");
        //建议可以直接使用java8的过滤,比较方便
        Set<String> set2 = Sets.filter(set1,str -> str.equalsIgnoreCase("b"));
        System.out.println(set2);
    }

所有的排列组合

@Test
    public void powerSetTest(){
        Set<String> set1 = Sets.newHashSet("a","b","c");
        //获取set可分隔成的所有子集
        Set<Set<String>> allSet = Sets.powerSet(set1);
        for(Set<String> set : allSet){
            System.out.println(set);
        }
    }

并集

@Test
    public void unionTest(){
        Set<String> set1 = Sets.newHashSet("a","b","c");
        Set<String> set2 = Sets.newHashSet("1","2","3");
        //取两个集合的并集
        System.out.println(Sets.union(set1,set2));
    }
Maps

主要方法有:

  • 创建各种Map的方法

  • Maps.newHashMap();

  • Maps.newConcurrentMap();

  • Maps.newIdentityHashMap();

  • Maps.newLinkedHashMap();

  • Maps.newTreeMap();

  • asMap(Set set, Function<? super K, V> function) set转map

  • difference(Map<? extends K, ? extends V> left, Map<? extends K, ? extends V> right) 计算map的差值

  • filterEntries(Map<K, V> unfiltered, Predicate<? super Entry<K, V>> entryPredicate) 通过Entry过滤

  • filterKeys(Map<K, V> unfiltered, final Predicate<? super K> keyPredicate) 通过Key过滤

  • filterValues(Map<K, V> unfiltered, final Predicate<? super V> valuePredicate) 通过value过滤

  • transformEntries(Map<K, V1> fromMap, EntryTransformer<? super K, ? super V1, V2> transformer) 转换Entry

  • transformValues(Map<K, V1> fromMap, Function<? super V1, V2> function) 转换value

创建各种Map的方法

@Test
    public void createDemo(){
        Maps.newHashMap();
        Maps.newHashMapWithExpectedSize(10);
        //Maps.newEnumMap();
        Maps.newConcurrentMap();
        Maps.newIdentityHashMap();

        Maps.newLinkedHashMap();
        Maps.newLinkedHashMapWithExpectedSize(10);

        Maps.newTreeMap();
    }

set转map

@Test
    public void asMapTest(){
        Set<String> set = Sets.newHashSet("a","b","c");
        //将set转成Map,key为set元素,value为每个元素的长度
        Map<String,Integer> map = Maps.asMap(set,String::length);
        System.out.println(map);
    }

计算map的差值

@Test
    public void differenceTest(){
        Map<String,String> map1 = Maps.newHashMap();
        map1.put("a","1");
        map1.put("b","2");
        map1.put("c","3");
        Map<String,String> map2 = Maps.newHashMap();
        map2.put("a","1");
        map2.put("e","5");
        map2.put("f","6");
        //mapDifference是将两个map相同的部分剔除
        MapDifference<String,String> mapDifference = Maps.difference(map1,map2);
        //两个Map相同的部分
        System.out.println(mapDifference.entriesInCommon());
        //左边集合剔除相同部分后的剩余
        System.out.println(mapDifference.entriesOnlyOnLeft());
        //右边集合剔除相同部分后的剩余
        System.out.println(mapDifference.entriesOnlyOnRight());
    }

通过Entry过滤

@Test
    public void filterEntriesTest(){
        Map<String,String> map1 = Maps.newHashMap();
        map1.put("a","1");
        map1.put("b","2");
        map1.put("c","3");
        Map<String,String> result = Maps.filterEntries(map1,item -> !item.getValue().equalsIgnoreCase("2"));
        System.out.println(result);

    }

通过Key过滤

@Test
    public void filterKeysTest(){
        Map<String,String> map1 = Maps.newHashMap();
        map1.put("a","1");
        map1.put("b","2");
        map1.put("c","3");
        Map<String,String> result = Maps.filterKeys(map1, item -> !item.equalsIgnoreCase("b"));
        System.out.println(result);
    }

通过value过滤

@Test
    public void filterValuesTest(){
        Map<String,String> map1 = Maps.newHashMap();
        map1.put("a","1");
        map1.put("b","2");
        map1.put("c","3");
        Map<String,String> result =  Maps.filterValues(map1,item -> !item.equalsIgnoreCase("3"));
        System.out.println(result);
    }

转换Entry

@Test
    public void transFormEntriesTest(){
        Map<String,String> map1 = Maps.newHashMap();
        map1.put("a","1");
        map1.put("b","2");
        map1.put("c","3");
        Map<String,String> result = Maps.transformEntries(map1,(k,v) -> k + v);
        System.out.println(result);
    }

转换value

@Test
    public void transformValuesTest(){
        Map<String,String> map1 = Maps.newHashMap();
        map1.put("a","1");
        map1.put("b","2");
        map1.put("c","3");
        Map<String,String> result = Maps.transformValues(map1, value -> value + 10);
        System.out.println(result);
    }

以上是Guava中提供的集合工具类,可以看到工具很丰富,包含了集合的各种常规操作,让我们在使用集合的时候更得心就手,熟练掌握Guava的各种集合工具类,势必能提升编码效率

相关文章