- 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