• Guava类库——Table详解


    前言

    Guava 是一套来自 Google 的核心 Java 库,其中包括新的集合类型、不可变的集合、图库,以及并发、I/O、散列、缓存、基元、字符串等实用工具!

    它更细粒度的提供了一些jdk没有的api和优化了一些操作,可以让你的代码更加优雅简洁高效。

    它被广泛用于 Google 内部的大多数 Java 项目,也被许多其他公司广泛使用。它被广泛用于 Google 内部的大多数 Java 项目,也被许多其他公司广泛使用。

    一、使用Guava

    引入maven如下:

    		<dependency>
                <groupId>com.google.guavagroupId>
                <artifactId>guavaartifactId>
                <version>31.1-jreversion>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    二、Table的创建和介绍

    平常我们会遇到创建Map>这样两级嵌套的Map类型的代码,遍历和操作都十分不方便,为了让我们代码看起来更优雅和方便操作,所以Table就出现了。

    1.以前我们的代码可能会是这样的:

    		Map<String, Integer> mapTemp1 = new HashMap<>();
            mapTemp1.put("age", 20);
            mapTemp1.put("score", 98);
            Map<String, Integer> mapTemp2 = new HashMap<>();
            mapTemp2.put("age", 22);
            mapTemp2.put("score", 100);
    		
    		Map<String, Map<String, Integer>> map = new HashMap<>();
            map.put("Tom", mapTemp1);
            map.put("Jery", mapTemp2);
    		//{Jery={score=100, age=22}, Tom={score=98, age=20}}
            System.out.println(map);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2.Table的创建
    Table中的三个参数我们简称为行、列、值,分别直观的对应两层Map中的Map>
    使用Table上面的代码可简化为下面的代码。

    		//创建一个Table
            Table<String, String, Integer> tab = HashBasedTable.create();
            tab.put("Tom", "age", 20);
            tab.put("Jery", "age", 22);
            tab.put("Tom", "score", 98);
            tab.put("Jery", "score", 100);
    		//{Tom={age=20, score=98}, Jery={age=22, score=100}}
            System.out.println(tab);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    3.如果行和列需要按照自然顺序可以使用TreeBasedTable.create()创建

    
            Table<String, String, Integer> tab = TreeBasedTable.create();
    
    • 1
    • 2

    4.如果打算创建一个不可变的Table,其内部数据永远不会改变(初始化后不能增加和删除数据),请使用ImmutableTable创建Table:

    
            Table<String, String, Integer> tab = ImmutableTable.<String, String, Integer>builder()
                    .put("Tom", "age", 20)
                    .put("Jery", "age", 22)
                    .put("Tom", "score", 98)
                    .put("Jery", "score", 100)
                    .build();
    		//{Tom={age=20, score=98}, Jery={age=22, score=100}}
            System.out.println(tab);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    三、通过行和列来获取值

            Table<String, String, Integer> tab = HashBasedTable.create();
            tab.put("Tom", "age", 20);
            tab.put("Jery", "age", 22);
            tab.put("Tom", "score", 98);
            tab.put("Jery", "score", 100);
    		//{Tom={age=20, score=98}, Jery={age=22, score=100}}
            System.out.println(tab);
    
            // 通过行和列来获取值
            Integer age = tab.get("Tom", "age");
    		//20
            System.out.println(age);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    四、通过行和列来删除值

            Table<String, String, Integer> tab = HashBasedTable.create();
            tab.put("Tom", "age", 20);
            tab.put("Jery", "age", 22);
            tab.put("Tom", "score", 98);
            tab.put("Jery", "score", 100);
    		//{Tom={age=20, score=98}, Jery={age=22, score=100}}
            System.out.println(tab);
    
            //通过行和列删除值。返回删除的值
            Integer value = tab.remove("Tom", "score");
    		//98
            System.out.println(value);
    		//{Tom={age=20}, Jery={age=22, score=100}}
            System.out.println(tab);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    五、判断行、列、值是否存在

    		Table<String, String, Integer> tab = HashBasedTable.create();
            tab.put("Tom", "age", 20);
            tab.put("Jery", "age", 22);
            tab.put("Tom", "score", 98);
            tab.put("Jery", "score", 100);
    		//{Tom={age=20, score=98}, Jery={age=22, score=100}}
            System.out.println(tab);
    		
            //根据行和列判断值是否存在
            boolean bo1 = tab.contains("Tom", "score");
            //判断行是否存在
            boolean bo2 = tab.containsRow("Tom");
            //判断列是否存在
            boolean bo3 = tab.containsColumn("age");
            //判断值是否存在
            boolean bo4 = tab.containsValue(100);
    		//true
            System.out.println(bo1);
    		//true
            System.out.println(bo2);
    		//true
            System.out.println(bo3);
    		//true
            System.out.println(bo4);
           
    
    • 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

    六、Table的遍历

            Table<String, String, Integer> tab = HashBasedTable.create();
            tab.put("Tom", "age", 20);
            tab.put("Jery", "age", 22);
            tab.put("Tom", "score", 98);
            tab.put("Jery", "score", 100);
    		//{Tom={age=20, score=98}, Jery={age=22, score=100}}
            System.out.println(tab);
    
            // 遍历表格
            for (Table.Cell c : tab.cellSet()) {
                System.out.println("行:" + c.getRowKey() + ",列:" + c.getColumnKey() + ",值:" + c.getValue());
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    输出:

    {Tom={age=20, score=98}, Jery={age=22, score=100}}
    行:Tom,列:age,值:20
    行:Tom,列:score,值:98
    行:Jery,列:age,值:22
    行:Jery,列:score,值:100
    
    • 1
    • 2
    • 3
    • 4
    • 5

    七、Table转Map

    1.Table转Map>:

            Table<String, String, Integer> tab = HashBasedTable.create();
            tab.put("Tom", "age", 20);
            tab.put("Jery", "age", 22);
            tab.put("Tom", "score", 98);
            tab.put("Jery", "score", 100);
    		//{Tom={age=20, score=98}, Jery={age=22, score=100}}
            System.out.println(tab);
    		
    		Map<String, Map<String, Integer>> rowMap = tab.rowMap();
    		//{age={Tom=20, Jery=22}, score={Tom=98, Jery=100}}
            System.out.println(rowMap);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2.Table转Map>:

            Table<String, String, Integer> tab = HashBasedTable.create();
            tab.put("Tom", "age", 20);
            tab.put("Jery", "age", 22);
            tab.put("Tom", "score", 98);
            tab.put("Jery", "score", 100);
    		//{Tom={age=20, score=98}, Jery={age=22, score=100}}
            System.out.println(tab);
    		
    		Map<String, Map<String, Integer>> columnMap = tab.columnMap();
    		//{age={Tom=20, Jery=22}, score={Tom=98, Jery=100}}
            System.out.println(columnMap);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    3.Table转Map:

            Table<String, String, Integer> tab = HashBasedTable.create();
            tab.put("Tom", "age", 20);
            tab.put("Jery", "age", 22);
            tab.put("Tom", "score", 98);
            tab.put("Jery", "score", 100);
    		//{Tom={age=20, score=98}, Jery={age=22, score=100}}
            System.out.println(tab);
    		
    		Map<String, Integer> ageMap = tab.column("age");
    		//{Tom=20, Jery=22}
            System.out.println(ageMap);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    4.根据指定的R将Table转Map:

            Table<String, String, Integer> tab = HashBasedTable.create();
            tab.put("Tom", "age", 20);
            tab.put("Jery", "age", 22);
            tab.put("Tom", "score", 98);
            tab.put("Jery", "score", 100);
    		//{Tom={age=20, score=98}, Jery={age=22, score=100}}
            System.out.println(tab);
    		
    		//根据指定的行将列和值组成一个Map
            Map<String, Integer> tomMap = tab.row("Tom");
    		//{age=20, score=98}
            System.out.println(tomMap);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    5.根据指定的C将Table转Map:

            Table<String, String, Integer> tab = HashBasedTable.create();
            tab.put("Tom", "age", 20);
            tab.put("Jery", "age", 22);
            tab.put("Tom", "score", 98);
            tab.put("Jery", "score", 100);
    		//{Tom={age=20, score=98}, Jery={age=22, score=100}}
            System.out.println(tab);
    		
    		//根据指定的列将行和值组成一个Map
            Map<String, Integer> ageMap = tab.column("age");
    		//{Tom=20, Jery=22}
            System.out.println(ageMap);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    八、Table翻转行和列

    将Table转为Table:

    		Table<String, String, Integer> tab = HashBasedTable.create();
            tab.put("Tom", "age", 20);
            tab.put("Jery", "age", 22);
            tab.put("Tom", "score", 98);
            tab.put("Jery", "score", 100);
            //{Tom={age=20, score=98}, Jery={age=22, score=100}}
            System.out.println(tab);
    
            Table<String, String, Integer> transposedTab = Tables.transpose(tab);
            //{age={Tom=20, Jery=22}, score={Tom=98, Jery=100}}
            System.out.println(transposedTab);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    九、Table的行列值转为Set

    1.获取所有的行为一个Set

            Table<String, String, Integer> tab = HashBasedTable.create();
            tab.put("Tom", "age", 20);
            tab.put("Jery", "age", 22);
            tab.put("Tom", "score", 98);
            tab.put("Jery", "score", 100);
    		//{Tom={age=20, score=98}, Jery={age=22, score=100}}
            System.out.println(tab);
    		
            //获取所有的行为一个Set
            Set<String> rowSet = tab.rowKeySet();
    		//[Tom, Jery]
            System.out.println(rowSet);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2.获取所有的列为一个Set

            Table<String, String, Integer> tab = HashBasedTable.create();
            tab.put("Tom", "age", 20);
            tab.put("Jery", "age", 22);
            tab.put("Tom", "score", 98);
            tab.put("Jery", "score", 100);
    		//{Tom={age=20, score=98}, Jery={age=22, score=100}}
            System.out.println(tab);
    		
    		//获取所有的列为一个Set
            Set<String> columnSet = tab.columnKeySet();
    		//[age, score]
            System.out.println(columnSet);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    3.获取所有的值为一个集合

            Table<String, String, Integer> tab = HashBasedTable.create();
            tab.put("Tom", "age", 20);
            tab.put("Jery", "age", 22);
            tab.put("Tom", "score", 98);
            tab.put("Jery", "score", 100);
    		//{Tom={age=20, score=98}, Jery={age=22, score=100}}
            System.out.println(tab);
    		
            //获取所有的值为一个集合
            Collection<Integer> values = tab.values();
    		//[20, 98, 22, 100]
            System.out.println(values);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
  • 相关阅读:
    docker实战
    GCP设置Proxy来连接Cloud SQL
    vmware无法ping通主机的解决方法
    ES 查询用法
    编程陷阱:切片追加
    安全厂商安恒信息加入龙蜥社区,完成 与 Anolis OS 兼容适配
    SpringBoot——》更改Redis序列化方式
    docker安装与部署
    如何上传项目到github?
    TCP状态转换
  • 原文地址:https://blog.csdn.net/qq_33697094/article/details/126399185