当前位置: 首页 > news >正文

Table和HashBasedTable的使用案例

-------------------

1.普通使用

package org.example.testhashbasedtable;import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;import java.util.Map;public class TestHashBasedTable {public static void main(String[] args) {Table<String, Integer, Float> table = HashBasedTable.create();table.put("a", 1, 1.1f);table.put("a", 2, 2.2f);System.out.println(table.row("a"));System.out.println(table.column(2));System.out.println(table.row("b"));System.out.println(table.column(1));Map<String, Float> column = table.column(1);}
}/*
{1=1.1, 2=2.2}
{a=2.2}
{}
{a=1.1}感悟:通过r或者c进行查找,得到的是一个包含其它2个字段的map*/

2.putAll 和 遍历

package org.example.testhashbasedtable;import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;public class TestHashBasedTable {public static void main(String[] args) {// 数据源1Table<String, Integer, Float> table = HashBasedTable.create();table.put("a", 1, 1.1f);table.put("a", 2, 2.2f);// 数据源2Table<String, Integer, Float> table2 = HashBasedTable.create();table2.put("a", 11, 1.1f);table2.put("aa", 2, 2.2f);// 数据源1添加到数据源2中table2.putAll(table);System.out.println(table2);// 遍历所有的条目for (Table.Cell<String, Integer, Float> cell : table2.cellSet()) {System.out.println(cell.getRowKey() + "  " + cell.getColumnKey() + " " + cell.getValue());}}
}/*
{a={11=1.1, 1=1.1, 2=2.2}, aa={2=2.2}}
a  11 1.1
a  1 1.1
a  2 2.2
aa  2 2.2*/

行、列、值 就是excel中的表格的抽象。

3.putAll的话,相同行列的是会被覆盖。相当于2个Excel表的融合。

package org.example.testhashbasedtable;import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Sets;
import com.google.common.collect.Table;import java.util.Set;public class Test2 {public static void main(String[] args) {// table1Table<Integer, Integer, Set<Integer>> table = HashBasedTable.create();table.put(1, 2, Sets.newHashSet(4, 5, 6));// table2Table<Integer, Integer, Set<Integer>> table2 = HashBasedTable.create();table2.put(1, 2, Sets.newHashSet(6, 7, 8));// 把table2加到table1中table.putAll(table2);// 发现相同行列的肯定是被覆盖了System.out.println(table);}
}/*
{1={2=[8, 6, 7]}}*/

http://www.lryc.cn/news/248291.html

相关文章:

  • 【执行批处理后 executeBatch() 没反应,一个参数相信就能搞定】
  • 【LeetCode】每日一题 2023_11_25 二叉树中的伪回文路径(dfs,数组/位运算)
  • 什么是海外私人IP代理?是纯净独享的代理吗?
  • Vue组件库推荐:Element UI深度解析
  • Mysql 8.0主从复制模式安装(兼容Mysql 5.7)
  • 基于Django+Tensorflow卷积神经网络鸟类识别系统
  • 史上最全前端知识点+高频面试题合集,十二大专题,命中率高达95%
  • 我叫:基数排序【JAVA】
  • ArkUI开发进阶—@Builder函数@BuilderParam装饰器的妙用与场景应用【鸿蒙专栏-05】
  • 智慧城市内涝积水监测仪功能,提升城市预防功能
  • ISCTF2023 部分wp
  • springboot(ssm毕业生学历证明系统Java(codeLW)
  • 分布式锁3: zk实现分布式锁
  • 每日博客Day8
  • Redis-主从与哨兵架构
  • PTA 7-3 将数组中的数逆序存放
  • JavaScript 如何拷贝对像(Object)或者数组(Array)
  • nodejs669在线图书借阅管理系统vue前端
  • 计算机网络之概述
  • git stash save untracked not staged
  • spring-boot集成mybatis-generator
  • C++中用于动态内存的new和delete操作符
  • 什么是美颜sdk?集成第三方美颜sdk的步骤
  • Gogs服务搭建及软件的使用
  • Python基础语法之学习运算符
  • freertos任务调度机制深度分析(以RISC-V架构为例)
  • 深入了解Spring Boot中@Async注解的8大坑点
  • C语言——深入理解指针(3)
  • 图书管理系统源码,图书管理系统开发,图书借阅系统源码配置和运行图解源码已附加
  • FFmpeg介绍