- Guava 教程
- Guava - 首页
- Guava - 概述
- Guava - 环境设置
- Guava - Optional 类
- Guava - Preconditions 类
- Guava - Ordering 类
- Guava - Objects 类
- Guava - Range 类
- Guava - Throwables 类
- Guava - 集合工具类
- Guava - 缓存工具类
- Guava - 字符串工具类
- Guava - 原生类型工具类
- Guava - 数学工具类
- Guava 有用资源
- Guava - 快速指南
- Guava - 有用资源
- Guava - 讨论
Guava - Table 接口
Table 代表一种特殊的映射,其中可以组合两个键来引用单个值。它类似于创建映射的映射。
接口声明
以下是 com.google.common.collect.Table<R,C,V> 接口的声明:
@GwtCompatible public interface Table<R,C,V>
接口方法
| 序号 | 方法及描述 |
|---|---|
| 1 |
Set<Table.Cell<R,C,V>> cellSet() 返回所有行键/列键/值三元组的集合。 |
| 2 |
void clear() 移除表中的所有映射。 |
| 3 |
Map<R,V> column(C columnKey) 返回具有给定列键的所有映射的视图。 |
| 4 |
Set<C> columnKeySet() 返回表中具有一个或多个值的列键的集合。 |
| 5 |
Map<C,Map<R,V>> columnMap() 返回一个视图,该视图将每个列键与从行键到值的相应映射关联。 |
| 6 |
boolean contains(Object rowKey, Object columnKey) 如果表包含具有指定行键和列键的映射,则返回 true。 |
| 7 |
boolean containsColumn(Object columnKey) 如果表包含具有指定列的映射,则返回 true。 |
| 8 |
boolean containsRow(Object rowKey) 如果表包含具有指定行键的映射,则返回 true。 |
| 9 |
boolean containsValue(Object value) 如果表包含具有指定值的映射,则返回 true。 |
| 10 |
boolean equals(Object obj) 比较指定的对象与此表是否相等。 |
| 11 |
V get(Object rowKey, Object columnKey) 返回对应于给定行键和列键的值,如果不存在此类映射,则返回 null。 |
| 12 |
int hashCode() 返回此表的哈希码。 |
| 13 |
boolean isEmpty() 如果表不包含任何映射,则返回 true。 |
| 14 |
V put(R rowKey, C columnKey, V value) 将指定的值与指定的键关联。 |
| 15 |
void putAll(Table<? extends R,? extends C,? extends V> table) 将指定表中的所有映射复制到此表。 |
| 16 | V remove(Object rowKey, Object columnKey) 移除与给定键关联的映射(如果存在)。 |
| 17 |
Map<C,V> row(R rowKey) 返回具有给定行键的所有映射的视图。 |
| 18 |
Set<R> rowKeySet() 返回表中具有一个或多个值的行键的集合。 |
| 19 |
Map<R,Map<C,V>> rowMap() 返回一个视图,该视图将每个行键与从列键到值的相应映射关联。 |
| 20 |
int size() 返回表中行键/列键/值映射的数量。 |
| 21 |
Collection<V> values() 返回所有值的集合,其中可能包含重复项。 |
Table 接口示例
使用您选择的任何编辑器创建以下 Java 程序,例如在 C:/> Guava 目录下。
GuavaTester.java
import java.util.Map;
import java.util.Set;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
public class GuavaTester {
public static void main(String args[]) {
//Table<R,C,V> == Map<R,Map<C,V>>
/*
* Company: IBM, Microsoft, TCS
* IBM -> {101:Mahesh, 102:Ramesh, 103:Suresh}
* Microsoft -> {101:Sohan, 102:Mohan, 103:Rohan }
* TCS -> {101:Ram, 102: Shyam, 103: Sunil }
*
* */
//create a table
Table<String, String, String> employeeTable = HashBasedTable.create();
//initialize the table with employee details
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");
//get Map corresponding to IBM
Map<String,String> ibmEmployees = employeeTable.row("IBM");
System.out.println("List of IBM Employees");
for(Map.Entry<String, String> entry : ibmEmployees.entrySet()) {
System.out.println("Emp Id: " + entry.getKey() + ", Name: " + entry.getValue());
}
//get all the unique keys of the table
Set<String> employers = employeeTable.rowKeySet();
System.out.print("Employers: ");
for(String employer: employers) {
System.out.print(employer + " ");
}
System.out.println();
//get a Map corresponding to 102
Map<String,String> EmployerMap = employeeTable.column("102");
for(Map.Entry<String, String> entry : EmployerMap.entrySet()) {
System.out.println("Employer: " + entry.getKey() + ", Name: " + entry.getValue());
}
}
}
验证结果
使用 javac 编译器编译类,如下所示:
C:\Guava>javac GuavaTester.java
现在运行 GuavaTester 以查看结果。
C:\Guava>java GuavaTester
查看结果。
List of IBM Employees Emp Id: 102, Name: Ramesh Emp Id: 101, Name: Mahesh Emp Id: 103, Name: Suresh Employers: IBM TCS Microsoft Employer: IBM, Name: Ramesh