Java之Google Guava使用举例

 简介:

  Guava工程包含了若干被Google的 Java项目广泛依赖 的核心库,例如:集合 [collections] 、缓存 [caching] 、原生类型支持 [primitives support] 、并发库 [concurrency libraries] 、通用注解 [common annotations] 、字符串处理 [string processing] 、I/O 等等。 所有这些工具每天都在被Google的工程师应用在产品服务中。

  中文教程:https://ifeve.com/google-guava/

 

依赖包:

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>29.0-jre</version>
        </dependency>

 

源码:

package tools.guava;

import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import com.google.common.primitives.Chars;
import com.google.common.primitives.Ints;
import org.junit.Test;

import java.awt.*;
import java.util.List;
import java.util.Set;
import java.util.function.Supplier;
import java.util.logging.Logger;

public class MyGuava {
    private final Logger logger = Logger.getLogger(this.getClass().getName());

    /**
     * 字符串拼接
     */
    @Test
    public void testGuavaJoiner() {
        // 快速创建List
        List<String> list = Lists.newArrayList("a", "b", null, "c");

        Joiner joiner = Joiner.on(",").skipNulls();
        String join = joiner.join(list);
        logger.info(join);  // a,b,c

        Joiner joiner2 = Joiner.on(",").useForNull("d");
        String join2 = joiner2.join(list);
        logger.info(join2);  // a,b,d,c

        /**
         * 跳过NULL元素:skipNulls()
         * 对于NULL元素使用其他替代:useForNull(String)
         * */
    }

    /**
     * 字符串分解
     */
    @Test
    public void testGuavaSplitter() {
        // 快速创建List
        String string = "a,b, ,c,";
        Splitter splitter = Splitter.on(",").trimResults().omitEmptyStrings();
        //输出结果: a b c
        splitter.split(string).forEach(x -> logger.info(x));

    }

    @Test
    public void testGuavaOpts() {
        // 快速创建List
        List<Integer> intList = Ints.asList(1, 2, 3, 4, 5);
        logger.info(intList.toString());

        // 数组的组装:1,2,3
        logger.info(Ints.join(",", 1, 2, 3));

//        原生类型数据快速合并
        int[] newIntArray = Ints.concat(new int[]{1, 2}, new int[]{1, 2, 3});
        logger.info(String.valueOf(newIntArray.length));

        // 最大、最小值:3,1
        logger.info(Ints.max(newIntArray) + "," + Ints.min(newIntArray));

        // 是否包含:true
        logger.info(String.valueOf(Ints.contains(newIntArray, 3)));
        logger.info(String.valueOf(Chars.contains(new char[]{'a', 'b', 'c'}, 'c')));

        // 集合转换为数组
        int[] intArray = Ints.toArray(intList);

    }

    /**
     * 一对多键值对:multimap
     */
    @Test
    public void testGuavaMultimap() {
        Multimap<String, String> multimap = ArrayListMultimap.create();
//        Multimap<String, String> multimap = HashMultimap.create();
        multimap.put("a", "1");
        multimap.put("a", "11");
        multimap.put("b", "2");
        multimap.put("b", "22");
        multimap.put("b", "222");
        multimap.put("c", "3");

        // 查询
        System.out.println(multimap);
        System.out.println(multimap.get("b"));
        System.out.println(multimap.get("b").toArray()[0]);
        System.out.println(((List<String>) multimap.get("b")).get(0));

        // 遍历-方法1
        Set<String> keys = multimap.keySet();
        for (String key : keys) {
            System.out.println("遍历-1:" + multimap.get(key));
        }

        // 遍历-方法2
        multimap.keySet().forEach(x -> System.out.println("遍历-2:" + multimap.get(x)));

        // 删除
        multimap.remove("b", "2");
        multimap.removeAll("a");
        System.out.println(multimap);
        System.out.println(Lists.newArrayList(multimap.entries()));

        /**
         * 运行结果:
         * {a=[1, 11], b=[2, 22, 222], c=[3]}
         * [2, 22, 222]
         * 2
         * 遍历-1:[1, 11]
         * 遍历-1:[2, 22, 222]
         * 遍历-1:[3]
         * 遍历-2:[1, 11]
         * 遍历-2:[2, 22, 222]
         * 遍历-2:[3]
         * {b=[22, 222], c=[3]}
         * [b=22, b=222, c=3]
         * */
    }


}

 

上一篇:map/ multimap容器


下一篇:【机器学习】otto案例介绍