Google Guava提供的特殊的Table集合
1、Table 是个啥?
是一个特殊的映射,其中两个键可以在组合的方式被指定为单个值。它类似于创建映射的映射。
当你想使用多个键做索引的时候,你可能会用类似 Map<rowKey, Map<columnKye, value>> 的实现;例如Nacos的本地注册表不就是多层Map嵌套嘛
Guava为此提供了新集合类型 Table,它有两个支持所有类型的键:”行”和”列”,类似效果:Map --> Table --> rowKey+columnKye+value
2、Table 怎么引入?
导入guava依赖坐标就好了
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.1-jre</version>
</dependency>
官方文档
3、Table怎么玩?
public class TableTest {
public static void main(String[] args) {
Table<String, String, Integer> tables = HashBasedTable.create();
// 测试数据 学生 --- 课程 --- 分数
tables.put("张三", "javase", 80);
tables.put("李四", "javase", 90);
tables.put("张三", "oracle", 100);
tables.put("王五", "oracle", 95);
tables.put("王五", "Go", 60);
tables.put("秋秋", "Go", 100);
// 所有的行数据
Set<Table.Cell<String, String, Integer>> cells = tables.cellSet();
for (Table.Cell<String, String, Integer> temp : cells) {
System.out.println(temp.getRowKey() + ":" + temp.getColumnKey() + ":" + temp.getValue());
}
// List<Table.Cell<String, String, Integer>> list = cells.stream().sorted((o1, o2) -> o1.getValue() - o2.getValue()).collect(Collectors.toList());
// System.out.println("list = " + list);
/**
* 张三:javase:80
* 张三:oracle:100
* 李四:javase:90
* 王五:oracle:95
* 王五:Go:60
* 秋秋:Go:100
*/
System.out.println("==========学生查看成绩==============");
System.out.print("学生\t");
// 所有的课程
Set<String> cours = tables.columnKeySet();
for (String t : cours) {
System.out.print(t + "\t");
}
System.out.println();
/**
* 学生 javase oracle Go
*/
// 所有的学生
Set<String> stus = tables.rowKeySet();
for (String stu : stus) {
System.out.print(stu + "\t");
Map<String, Integer> scores = tables.row(stu);
for (String c : cours) {
System.out.print(scores.get(c) + "\t");
}
System.out.println();
}
/**
* 学生 javase oracle Go
* 张三 80 100 null
* 李四 90 null null
* 王五 null 95 60
* 秋秋 null null 100
*/
System.out.println("==========课程查看成绩==============");
System.out.print("课程\t");
// 所有的学生
Set<String> stuSet = tables.rowKeySet();
for (String t : stuSet) {
System.out.print(t + "\t");
}
/**
* 张三 李四 王五 秋秋
*/
System.out.println();
// 所有的课程
Set<String> courSet = tables.columnKeySet();
for (String c : courSet) {
System.out.print(c + "\t");
Map<String, Integer> scores = tables.column(c);
for (String s : stuSet) {
System.out.print(scores.get(s) + "\t");
}
System.out.println();
}
/**
* 课程 张三 李四 王五 秋秋
* javase 80 90 null null
* oracle 100 null 95 null
* Go null null 60 100
*/
System.out.println("===========转换===========");
// 相当于把rowKey和columnKey做了一个交换
Table<String, String, Integer> tables2 = Tables.transpose(tables);
// 所有的行数据
Set<Table.Cell<String, String, Integer>> cells2 = tables2.cellSet();
for (Table.Cell<String, String, Integer> temp : cells2) {
System.out.println(temp.getRowKey() + ":" + temp.getColumnKey() + ":" + temp.getValue());
}
/**
* javase:张三:80
* oracle:张三:100
* javase:李四:90
* oracle:王五:95
* Go:王五:60
* Go:秋秋:100
*/
main2();
}
public static void main2() {
Table<String, String, String> table = HashBasedTable.create();
// 使用员工详细信息初始化表
table.put("IBM", "101", "Mahesh");
table.put("IBM", "102", "Ramesh");
table.put("IBM", "103", "Suresh");
table.put("Microsoft", "111", "Sohan");
table.put("Microsoft", "112", "Mohan");
table.put("Microsoft", "113", "Rohan");
table.put("TCS", "121", "Ram");
table.put("TCS", "122", "Shyam");
table.put("TCS", "123", "Sunil");
// 获取与IBM对应的Map
Map<String, String> ibmMap = table.row("IBM");
System.out.println("===========IBM员工名单===========");
for (Map.Entry<String, String> entry : ibmMap.entrySet()) {
System.out.println("Emp Id: " + entry.getKey() + ", Name: " + entry.getValue());
}
System.out.println();
// 获取表格的所有唯一键
System.out.println("===========获取表格的所有唯一键===========");
Set<String> employers = table.rowKeySet();
System.out.print("Employers: ");
employers.forEach(e -> System.out.print(e + " "));
System.out.println("\n");
// 得到一个对应102的Map
System.out.println("===========得到一个对应102的Map===========");
Map<String, String> EmployerMap = table.column("102");
for (Map.Entry<String, String> entry : EmployerMap.entrySet()) {
System.out.println("Employer: " + entry.getKey() + ", Name: " + entry.getValue());
}
main3();
}
public static void main3() {
System.out.println("===========main3()===========");
Table<String, Integer, Integer> table = HashBasedTable.create();
table.put("A", 1, 100);
table.put("A", 2, 101);
table.put("B", 1, 200);
table.put("B", 2, 201);
/**
* contains(Object rowKey, Object columnKey):
* Table中是否存在指定rowKey和columnKey的映射关系
*/
boolean containsA3 = table.contains("A", 3); // false
System.out.println("containsA3 = " + containsA3);
boolean containColumn2 = table.containsColumn(2); // true
System.out.println("containColumn2 = " + containColumn2);
boolean containsRowA = table.containsRow("A"); // true
System.out.println("containsRowA = " + containsRowA);
boolean contains201 = table.containsValue(201); // true
System.out.println("contains201 = " + contains201);
/**
* remove(Object rowKey, Object columnKey):
* 删除Table中指定行列值的映射关系
*/
Integer res = table.remove("A", 2);
System.out.println("res = " + res);
/**
* get(Object rowKey, Object columnKey):
* 获取Table中指定行列值的映射关系
*/
Integer mapping = table.get("B", 2);
System.out.println("mapping = " + mapping);
/**
* column(C columnKey):返回指定columnKey下的所有rowKey与value映射
*/
Map<String, Integer> columnMap = table.column(2);
System.out.println("columnMap = " + columnMap);
/**
* row(R rowKey):返回指定rowKey下的所有columnKey与value映射
*/
Map<Integer, Integer> rowMap = table.row("B");
System.out.println("rowMap = " + rowMap);
/**
* 返回以Table.Cell<R, C, V>为元素的Set集合
* 类似于Map.entrySet
*/
Set<Table.Cell<String, Integer, Integer>> cells = table.cellSet();
for (Table.Cell<String, Integer, Integer> cell : cells) {
// 获取cell的行值rowKey
String rowKey = cell.getRowKey();
System.out.println("rowKey = " + rowKey);
// 获取cell的列值columnKey
Integer columnKey = cell.getColumnKey();
System.out.println("columnKey = " + columnKey);
// 获取cell的值value
Integer value = cell.getValue();
System.out.println("value = " + value);
}
}
}
Google Guava提供的特殊的Table集合的更多相关文章
- 使用Guava提供的filter过滤集合
正常情况下,我们声明一个List需要如下代码 List<String> list = new ArrayList<>(); list.add("AAA"); ...
- 字符串操作 — Google Guava
前言 Java 里字符串表示字符的不可变序列,创建后就不能更改.在我们日常的工作中,字符串的使用非常频繁,熟练的对其操作可以极大的提升我们的工作效率,今天要介绍的主角是 Google 开源的一个核心 ...
- [Google Guava] 2.3-强大的集合工具类:java.util.Collections中未包含的集合工具
原文链接 译文链接 译者:沈义扬,校对:丁一 尚未完成: Queues, Tables工具类 任何对JDK集合框架有经验的程序员都熟悉和喜欢java.util.Collections包含的工具方法.G ...
- [Google Guava] 2.1-不可变集合
范例 01 public static final ImmutableSet<String> COLOR_NAMES = ImmutableSet.of( 02 "red&quo ...
- [Google Guava] 强大的集合工具类:java.util.Collections中未包含的集合工具
转载的,有问题请联系我 原文链接 译文链接 译者:沈义扬,校对:丁一 尚未完成: Queues, Tables工具类 任何对JDK集合框架有经验的程序员都熟悉和喜欢java.util.Collecti ...
- [Google Guava]学习--新集合类型Multiset
Guava提供了一个新集合类型Multiset,它可以多次添加相等的元素,且和元素顺序无关.Multiset继承于JDK的Cllection接口,而不是Set接口. Multiset主要方法介绍: a ...
- [Google Guava] 2.2-新集合类型
转自:并发编程网 原文链接:http://ifeve.com/google-guava-newcollectiontypes/ 链接博客其他文章中还有更多的guava其他功能的描述,有空可慢慢看. G ...
- [Guava官方文档翻译] 7. Guava的Immutable Collection(不可变集合)工具 (Immutable Collections Explained)
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3538666.html ,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体 ...
- [Google Guava] 4-函数式编程
原文链接 译文链接 译者:沈义扬,校对:丁一 注意事项 截至JDK7,Java中也只能通过笨拙冗长的匿名类来达到近似函数式编程的效果.预计JDK8中会有所改变,但Guava现在就想给JDK5以上用户提 ...
- Guava学习笔记:Google Guava 类库简介
http://www.cnblogs.com/peida/tag/Guava/ Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, cachin ...
随机推荐
- 智能AI 的应用场景
小凡智能AI是一款基于人工智能技术开发的助软件,能够帮助用户解决各种各样的问题,提高工作效率和生活质量.它的应用范围广泛,涵盖了工作.学习.健康等多个方面,为用户提供了全方位的服务支持. 在工作方面, ...
- WPF MVVM之点滴分享
(第五点:绑定源有修改) 我并不打算长篇累牍的介绍什么是MVVM.我尽量简洁的介绍,并把自己的经验分享给大家. 一.关于MVVM M:Model,数据模型(后台存储数据的类) V:View,视图(大部 ...
- Elasticsearch之常用术语
一. 数据库和ES简单类比 关系型数据库 表(Table) 行(Row) 列(Cloumn) Schema SQL Elasticsearch 索引(Index) 文档(Document) 字段(Fi ...
- Vue源码学习(六):(支线)渲染函数中with(),call()的使用以及一些思考
好家伙, 昨天,在学习vue源码的过程中,看到了这个玩意 嘶,看不太懂,研究一下 1.上下文 这段出现vue模板编译的虚拟node部分 export function renderMixin( ...
- Solution -「营业」「CF 527C」Glass Carving
Description Link. 有一个块 \(n\times m\) 的矩形,有 \(q\) 次操作,每次把矩形横 / 竖着切一刀,问切完后的最大矩形面积. Solution 一个不同于大多数人. ...
- EtherCAT转Modbus网关用Modbus Slave模拟从站配置案例
EtherCAT转Modbus网关用Modbus Slave模拟从站配置案例 兴达易控EtherCAT到Modbus网关可以用作Modbus从站的配置.EtherCAT到Modbus网关允许Modbu ...
- 有人说SaToken吃相难看,你怎么看。
前言 今天摸鱼逛知乎,偶然看到了一个回答,8月份的,是关于SaToken的,一时好奇就点了进去. 好家伙,因为一个star的问题,提问的人抱怨了许多,我有些意外,就仔细看了下面的评论,想知道一部分人的 ...
- 真·Redis缓存优化—97%的优化率你见过嘛?
本文通过一封618前的R2M(公司内部缓存组件,可以认为等同于Redis)告警,由浅入深的分析了该告警的直接原因与根本原因,并根据原因提出相应的解决方法,希望能够给大家在排查类似问题时提供相应的思路. ...
- pandas(进阶操作)-- 处理非数值型数据 -- 数据分析三剑客(核心)
博客地址:https://www.cnblogs.com/zylyehuo/ 开发环境 anaconda 集成环境:集成好了数据分析和机器学习中所需要的全部环境 安装目录不可以有中文和特殊符号 jup ...
- 可视化-vscode安装pandas
pandas 是基于NumPy 的一种工具,该工具是为解决数据分析任务而创建的.Pandas 纳入了大量库和一些标准的数据模型,提供了高效地操作大型数据集所需的工具.pandas提供了大量能使我们快速 ...