当前位置:   article > 正文

Guava:google公司开发的一款Java类库扩展工具包_com.google.guava:guava

com.google.guava:guava

Guava是google公司开发的一款Java类库扩展工具包

文档

maven 依赖

<dependency>
  <groupId>com.google.guava</groupId>
  <artifactId>guava</artifactId>
  <version>31.1-jre</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

示例1:连接字符串

package com.example;


import com.google.common.base.Joiner;

import java.util.Arrays;

public class Demo {
    public static void main(String[] args) {

        // JDK 1.8
        String var1 = String.join("-", Arrays.asList("A", null, "C", "D"));
        System.out.println(var1);
        // A-null-C-D

        // guava
        Joiner joiner = Joiner.on("-").skipNulls();
        String var2 = joiner.join("A", null, "C", "D");
        System.out.println(var2);
        // A-C-D
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

示例2:统计元素出现次数

package com.example;


import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multiset;

import java.util.Arrays;

public class Demo {
    public static void main(String[] args) {

        String[] words = {"A", "B", "C", "B"};

        // JDK 1.8
        Map<String, Integer> counts = new HashMap<String, Integer>();
        for (String word : words) {
            Integer count = counts.get(word);
            if (count == null) {
                counts.put(word, 1);
            } else {
                counts.put(word, count + 1);
            }
        }
        
        System.out.println(counts);
        // {A=1, B=2, C=1}

        // guava
        Multiset<String> multiset = HashMultiset.create();
        multiset.addAll(Arrays.asList(words));

        System.out.println(multiset);
        // [A, B x 2, C]
        System.out.println(multiset.count("B"));
        // 2
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

示例3:key对应集合

package com.example;


import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

public class Demo {
    public static void main(String[] args) {
        
        Multimap<String, Integer> multimap = ArrayListMultimap.create();
        multimap.put("A", 1);
        multimap.put("A", 2);
        multimap.put("B", 4);
        multimap.put("C", 5);

        System.out.println(multimap);
        // {A=[1, 2], B=[4], C=[5]}
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

示例4:list分组

5条数据,按照2个元素一组,进行分组

// JDK 1.8
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);

List<List<Integer>> partitionList = new ArrayList<>();


List<Integer> dataList = new ArrayList<>();
int size = 0;

for(Integer data : list) {
    size++;

    dataList.add(data);

    if(size >= 2) {
        partitionList.add(dataList);

        dataList = new ArrayList<>();
        size = 0;
    }
}

if (dataList.size() > 0){
    partitionList.add(dataList);
}

System.out.println(partitionList);
// [[1, 2], [3, 4], [5]]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

使用Lists.partition方法

// guava
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
List<List<Integer>> partition = Lists.partition(list, 2);
System.out.println(partition);
// [[1, 2], [3, 4], [5]]
  • 1
  • 2
  • 3
  • 4
  • 5

参考
Guava中这些Map的骚操作,让我的代码量减少了50%
一篇让你熟练掌握Google Guava包(全网最全)
Java中代码优化的30个小技巧

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/828849
推荐阅读
相关标签
  

闽ICP备14008679号