代码编织梦想

在guava库中还提供了一种二维表结构:Table。使用Table可以实现二维矩阵的数据结构,可以是稀溜矩阵。通常来说,当你想使用多个键做索引的时候,你可能会用类似Map(FirstName, Map(LastName, Person))的实现,这种方式很丑陋,使用上也不友好。Guava为此提供了新集合类型Table,Table是Guava提供的一个接口 Interface Table(R,C,V),由rowKey(行)+columnKey(列)+value组成 ,它有两个支持所有类型的键:”行”和”列”。Table提供多种视图,以便你从各种角度使用它:
这里写图片描述

主要使用的方法有: 
    * 返回一个Set集合,包含了所有数据:cellSet() 
    * 返回一个Set集合,集合由第一列键数据也就是rowKey所有数据:rowKeySet() 
    * 返回一个Set集合,集合由第二列键数据也就是columnKey所有数据:columnKeySet() 
    * 返回一个Collection集合,集合由所有value数据组成:values() 
    * 根据行rowKey(第一列,行),返回这一行对应的列和值组成的集合:[rowMap()+get(rows数据)]/row(row数据) 
    * 根据行columnKey(第一列,行),返回这一行对应的列和值组成的集合[columnMap()+get(column数据)]/column(column数据)
Table<String,String,Integer> tables=HashBasedTable.create();
    tables.put("a", "javase", 80);
    tables.put("b", "javaee", 90);
    tables.put("c", "javame", 100);
    tables.put("d", "guava", 70);
for(String str:students){
    Map<String,Integer> rowMap=tables.row(str);
    Set<Entry<String,Integer>> setEntry=rowMap.entrySet();
    for(Entry<String,Integer> entry:setEntry){
        System.out.println(entry.getKey()+" "+entry.getValue());
    }
}
输出结果:
guava 70
javaee 90
javame 100
javase 80
for (String str : courses) {
    Map<String, Integer> rowMap2 = tables.column(str);
    Set<Entry<String, Integer>> setEntry2 = rowMap2.entrySet();
    for (Entry<String, Integer> entry : setEntry2) {
        System.out.println(entry.getKey() + " " + entry.getValue());
    }
}
输出结果为:
d 70
b 90
c 100
a 80
import java.util.Collection;
import java.util.Map;
import java.util.Set;

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import com.google.common.collect.Table.Cell;
import com.google.common.collect.Tables;

/**
 * 测试Table :Table就是有了双键的Map
 * 学生(rowkey)--课程(columkey)--成绩(value)
 *     lf     --     a      -- 80
 *     dn     --     b      -- 90
 *     cf     --     a      -- 88
 *  
 * @author Administrator
 *
 */
public class Demo06 {
    public static void main(String[] args) {
        Table<String,String,Integer> table = HashBasedTable.create();
        table.put("a", "javase", 80);
        table.put("b", "javase", 90);
        table.put("a", "javame", 100);
        table.put("d", "guava", 70);

        //得到所有的行数据
        Set<Cell<String,String,Integer>> cellset = table.cellSet();

        for(Cell<String,String,Integer> temp:cellset){
            System.out.println(temp.getRowKey()+"--"+temp.getColumnKey()+"--"+temp.getValue());
        }

        System.out.println("-------rowKey和columnKey转换---------");
        Table<String,String,Integer> table1 = Tables.transpose(table);
        Set<Cell<String,String,Integer>> cellset1 = table1.cellSet();

        for(Cell<String,String,Integer> temp:cellset1){
            System.out.println(temp.getRowKey()+"--"+temp.getColumnKey()+"--"+temp.getValue());
        }

        System.out.println("-------按学生查看成绩---------");

        System.out.print("学生\t");
        Set<String> cours = table.columnKeySet();
        for(String temp:cours){
            System.out.print(temp+"\t");
        }

        System.out.println();

        Set<String> stu = table.rowKeySet();
        for(String temp:stu){
            System.out.print(temp);
            Map<String,Integer> map = table.row(temp);
            for(String temp1:cours){
                System.out.print("\t"+map.get(temp1));
            }
            System.out.println();
        }

        System.out.println("-------按课程查看成绩---------");

        System.out.print("课程\t");
        Set<String> stu1 = table.rowKeySet();
        for(String temp:stu1){
            System.out.print(temp+"\t");
        }

        System.out.println();

        Set<String> cours1 = table.columnKeySet();
        for(String temp:cours1){
            System.out.print(temp);
            Map<String,Integer> map1 = table.column(temp);
            for(String temp1:stu1){
                System.out.print("\t"+map1.get(temp1));
            }
            System.out.println();
        }
    }

}
输出结果:
        d--guava--70
        b--javase--90
        a--javase--80
        a--javame--100
        -------转换---------
        guava--d--70
        javase--b--90
        javase--a--80
        javame--a--100
        -------按学生查看成绩---------
        学生  guava   javase  javame  
        d   70  null    null
        b   null    90  null
        a   null    80  100
        -------按课程查看成绩---------
        课程  d   b   a   
        guava   70  null    null
        javase  null    90  80
        javame  null    null    100
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/quniMdejiangyou/article/details/78106611

guava使用之table_其实我系个好人的博客-爱代码爱编程_guava table

当我们需要多个索引的数据结构的时候,通常情况下,我们只能用这种丑陋的Map<FirstName, Map<LastName, Person>>来实现。 为此Guava提供了一个新的集合类型-Tabl

guava一键多值的 map_kanpiaoxue的博客-爱代码爱编程

  public static void main(String[] args) { Multimap<Integer, String> map = LinkedListMultimap.create(); map.put(1, "xue"); map.put(1, "wang"); map.put(1

guava table 示例教程-爱代码爱编程

Guava Table 示例教程 本文带你学习Guava Table 接口及其实现。Guava Table 是类似表格结构的集合,包含行、列以及相应的单元格值。行和列作为有序的键。 概述 如果把Guava Table和

guava table_陌上花开ft的博客-爱代码爱编程_guava table

1、guava的table类,可代替map<string,map<string,string>>,使用非常方便。 import com.google.common.collect.HashBased

python-10.菜鸟教程-5-总纲实例演示-爱代码爱编程

Python 数字求和 以下实例为通过用户输入两个数字,并计算两个数字之和: # -*- coding: UTF-8 -*- # Filename : test.py # author by : www.runoob.com # 用户输入数字 num1 = input('输入第一个数字:') num2 = input('输入第二个数字:')

Flink 使用 Guava cache来实现维表数据热存储之资料整理-爱代码爱编程

需求分析:   Flink实时流处理过程中,需要使用维表数据,我们需要加载维表数据进行join,一般分3种方式   1,使用预加载,在open方法里面读取数据   也可以定时读写redis的数据,同步更新内存数据   2,使用分布式缓存--不推荐   3,使用Guava cache 异步IO缓存 1)导入依赖: <depend

Guava 心得:Guava 新集合 —— Table 等-爱代码爱编程

Table 当我们需要多个索引的数据结构的时候,通常情况下,我们只能用这种丑陋的Map<FirstName, Map<LastName, Person>>来实现。为此Guava提供了一个新的集合类型-Table集合类型,来支持这种数据结构的使用场景。Table支持“row”和“column”,而且提供多种视图。 @Test

java hashbasedtable_实用的开源工具库Guava之Table-爱代码爱编程

Table接口 Table是Guava在jdk的基础上新增的一种新集合类型,是一个有序的键值对集合。可能通过行和列取出对应的值。而Table有可能是稀疏的,不是每个行列对都会有值。可以通过行(列)键值或取出一行(列)的数据,也可以同时使用,取出唯一的映射值。返回集合的方法是返回Tabale的中对象的引用,修改集合会修改表,修改表也会修改集合中的对象。

java guava table_guava之Table-爱代码爱编程

Table 双键的 map rowKey+columnKey+value 以学车成绩表来举例 成绩表 方法: 所有的行数据:cellSet(); 所有的学生:rowKeySet(); 所有的课程:columnKeySet(); 所有的成绩:values(); 学生对应的课程和成绩:rowMap();课程为键 或者row(学生)

java guava table_Guava Table接口-爱代码爱编程

Guava Table接口 Table代表一个特殊的映射,其中两个键可以在组合的方式被指定为单个值。它类似于创建映射的映射。 接口声明 以下是 com.google.common.collect.Table 接口的声明: @GwtCompatible public interface Table 接口方法 Table 例子 选择

java guava table_java – 使用Guava的Tables.toTable-爱代码爱编程

我有一个持有两个atrributes的东西:状态(一个枚举)和所有者(另一个对象). 我想获得一个Guava Table< owner,status,Long>通过遍历ArrayList并计算对象,如果某些状态不在List中,则计数为0,如下所示: [owner1, status1, 2], [owner1, status2, 0],

java二维数组序列化,GitHub - flym/array-tree-table: 基于二维数据,支持排序,支持序列化/反序列化的guava table实现...-爱代码爱编程

array-tree-table 使用二维数组实现的并且可用于排序的表格(Table), 支持json的序列化和反序列化 参考Table定义:Table源码 实现目标 查找 按行,按列,快速定位(guava中的table实现不能满足此要求) 快速迭代,按行,按列,支持稀疏迭代 泛型支持(通用化) 支持排序(guava中的TreeTable

Table 类 - Google Guava-爱代码爱编程

一、Table简介 Table 代表一个特殊的映射,其中两个键可以在组合的方式被指定为单个值。它类似于创建映射的映射。 通常来说,当你想使用多个键做索引的时候,你可能会用类似 Map<FirstName, Map<LastName, Person>> 的实现,这种方式很丑陋,使用上也不友好。 Guava为此提供了新集合类型 T

Guava-Table-爱代码爱编程

import com.google.common.collect.Table; import com.google.common.collect.TreeBasedTable; import lombok.extern.slf4j.Slf4j; import java.util.Collection; import java.util.Map; impo

guava中的Table-爱代码爱编程

提供两层映射关系(row, col)->value 1、Table接口 方法有 boolean contains(Object rowKey, Object columnKey)boolean containsRow(Object rowKey)boolean containsColumn(Object columnKey)boolean

guava类库——table详解_西凉的悲伤的博客-爱代码爱编程

目录 前言一、使用Guava二、Table的创建和介绍三、通过行和列来获取值四、通过行和列来删除值五、判断行、列、值是否存在六、Table的遍历七、Table转Map八、Table翻转行和列九、Table的行列值转为

python列表中字符串转数字的方法_python把列表中的字符串变为数字-爱代码爱编程

比如我们有个列表: number = ['1', '2', '3', '4']; 如果我们需要将列表里的元素转换为数字呢?最常用的大家可能会想到使用列表推导式: ''' 学习中遇到问题没人解答?小编创建了一个Pyth

google guava之table简介说明_google table-爱代码爱编程

转自:   Google guava之Table简介说明 下文笔者讲述guava中Table集合的简介说明,如下所示 guava之Table集合简介 Table集合: 用于存储数据表,类似于Map<String,Map<String,String>>这种结构 Table集合实现了指定接口 Arra