guava之Table
来源:互联网 发布:数据分析职业发展 知乎 编辑:程序博客网 时间:2024/05/20 13:39
在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 70javaee 90javame 100javase 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 70b 90c 100a 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
阅读全文
0 0
- guava之Table
- Guava使用之Table
- guava之Table
- Google Guava学习之Table
- Guava之二维矩阵Table
- Guava ---- Table矩阵
- Java-类库-Guava-Table
- Guava collections -- Table
- Guava Table接口
- Guava Table<R,C,V>
- JAVA学习笔记47——其他容器简介4:guava之MulitiSet、MultiMap、BiMap、Table
- Guava学习笔记:Guava新集合-Table等
- Guava学习笔记【10】:Guava新集合-Table等
- Google Guava学习(8)-Guava集合工具 Table接口
- guava学习之——guava简介
- Guava学习之Multimap
- Guava学习之BiMap
- Guava学习之Multisets
- UVA 12538 Version Controlled IDE 可持久化平衡树 || rope
- 数据库的数据类型和数据表
- linux第一章作业
- HDU1166:敌兵布阵(线段树以及单点更新)
- jdbc连接数据库
- guava之Table
- 菜鸟学大型分布式网站心得(一)
- MDK+JLINK环境下LPC1857外扩SDRAM在线调试实现
- C++字符型指针无法输出的问题
- HTML转义字符大全 (换行,enter,所有特殊字符)
- hadoop2.7.3在集群中配置多个namenode(federation cluster)
- 17-9-29Unsupported major.minor version 52.0 报错处理方式
- Codeforces Round #436 (Div. 2)D. Make a Permutation! codeforces-864D. Make a Permutation!
- Project Euler 36-40题