-
功能:简化表格操作,形似Excel的单元格操作;
-
代码:
package com.example.google.guava.demo.collection;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* <p>
* <code>TableTest</code>
* </p>
* Description: Table<R, C, V>接口类似Map<R,Map<C,V>>,R-row,C-column,V-value
*
* @author Mcchu
* @date 2017/10/20 8:25
*/
public class TableTest {
public static void main(String[] args) {
// 1.创建表格Table
Table<String, String, String> employeeTable = HashBasedTable.create();
// 2.初始化表格Table,参数分别对应表格的行、列、值
employeeTable.put("IBM", "101","Mahesh");
employeeTable.put("IBM", "102","Ramesh");
employeeTable.put("IBM", "103","Suresh");
employeeTable.put("Microsoft", "111","Sohan");
employeeTable.put("Microsoft", "112","Mohan");
employeeTable.put("Microsoft", "113","Rohan");
employeeTable.put("TCS", "121","Ram");
employeeTable.put("TCS", "122","Shyam");
employeeTable.put("TCS", "123","Sunil");
// 3.获取表格中某个单元格的值
String IBM103name = employeeTable.get("IBM","103");
System.out.println("3.行key为IBM列key为103的员工值:"+IBM103name);
// 4.转换为Map,将Table<R,C,V>转换成Map<C,V>形势
Map<String,String> ibmEmployees = employeeTable.row("IBM");
System.out.println("4.IBM员工信息:");
for (Map.Entry<String,String> entry:ibmEmployees.entrySet()){
String id = entry.getKey();
String name = entry.getValue();
System.out.println(" 员工id:"+ id +",员工姓名:"+name);
}
// 5.键去重(即获取唯一)
Set<String> employeeRowKeys = employeeTable.rowKeySet();
System.out.println("5.获取表中所有唯一键:");
for (String rowKey: employeeRowKeys){
System.out.println(" 行号:"+rowKey);
}
// 6.转换为Map,将Table<R,C,V>转换成Map<R,V>形式
Map<String,String> employerMap1 = employeeTable.column("102");
System.out.println("6.列102的数据:");
for ( Map.Entry<String,String> entry:employerMap1.entrySet() ){
String company = entry.getKey();
String name = entry.getValue();
System.out.println(" 公司名字:"+company+",员工姓名"+name);
}
// 7.遍历表格
Set<Table.Cell<String,String,String>> tableSet = employeeTable.cellSet();
Iterator<Table.Cell<String,String,String>> it = tableSet.iterator();
System.out.println("7.遍历Table:");
while (it.hasNext()){
Table.Cell<String,String,String> data = it.next();
String rowCompany = data.getRowKey();
String columnId = data.getColumnKey();
String valueName = data.getValue();
System.out.println(" 行号:"+rowCompany+",列号:"+columnId+",值:"+valueName);
}
// 8.获取列键
System.out.println("8.获取所有列键:");
for (String columnKey : employeeTable.columnKeySet()){
Map<String,String> rowAndValue = employeeTable.column(columnKey);
System.out.println(" 列:"+columnKey+"对应的数据Map是:"+rowAndValue);
}
// 9.
Collection<String> values = employeeTable.values();
System.out.println("9.获取所有值:"+values.toString());
// 10.其他简单方法,不做尝试
//boolean contains(Object rowKey, Object columnKey)
//boolean containsColumn(Object columnKey)
//boolean containsRow(Object rowKey)
//boolean containsValue(Object value)
//boolean equals(Object obj)
//int hashCode()
//boolean isEmpty()
//void putAll(Table<? extends R,? extends C,? extends V> table)
//V remove(Object rowKey, Object columnKey)
//int size()
}
}
- 输出结果:
3.行key为IBM列key为103的员工值:Suresh
4.IBM员工信息:
员工id:101,员工姓名:Mahesh
员工id:102,员工姓名:Ramesh
员工id:103,员工姓名:Suresh
5.获取表中所有唯一键:
行号:IBM
行号:Microsoft
行号:TCS
6.列102的数据:
公司名字:IBM,员工姓名Ramesh
7.遍历Table:
行号:IBM,列号:101,值:Mahesh
行号:IBM,列号:102,值:Ramesh
行号:IBM,列号:103,值:Suresh
行号:Microsoft,列号:111,值:Sohan
行号:Microsoft,列号:112,值:Mohan
行号:Microsoft,列号:113,值:Rohan
行号:TCS,列号:121,值:Ram
行号:TCS,列号:122,值:Shyam
行号:TCS,列号:123,值:Sunil
8.获取所有列键:
列:101对应的数据Map是:{IBM=Mahesh}
列:102对应的数据Map是:{IBM=Ramesh}
列:103对应的数据Map是:{IBM=Suresh}
列:111对应的数据Map是:{Microsoft=Sohan}
列:112对应的数据Map是:{Microsoft=Mohan}
列:113对应的数据Map是:{Microsoft=Rohan}
列:121对应的数据Map是:{TCS=Ram}
列:122对应的数据Map是:{TCS=Shyam}
列:123对应的数据Map是:{TCS=Sunil}
9.获取所有值:[Mahesh, Ramesh, Suresh, Sohan, Mohan, Rohan, Ram, Shyam, Sunil]