天天看點

guava學習:guava集合類型-table

最近學習了下guava的使用,這裡簡單記錄下一些常用并且使用的工具類把。

看到table的使用時候真的是眼前一亮,之前的代碼中寫過很多的Map<String,Map<String,String>> 這種格式的代碼,這種閱讀起來非常的不友好,甚至都不知道map中的key到底是什麼還要聯系上下文聯想才可以,而table類型的出現徹底解決掉了這個麻煩。

Table支援 row、column、value  我們把上面定義的map結構想象成一張資料表就可以了:

Table<R,C,V> == Map<R,Map<C,V>>      

下面先讓我們來看一張資料表吧,結合資料表來編寫我們的代碼:

guava學習:guava集合類型-table
S.N. 方法 & 描述
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()

傳回所有值,其中可能包含重複的集合。

下面是根據上面的表格寫的Demo

/*
         *  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", "102","Shyam");
        employeeTable.put("TCS", "123","Sunil");

        //所有行資料
        System.out.println(employeeTable.cellSet());
        //所有公司
        System.out.println(employeeTable.rowKeySet());
        //所有員工編号
        System.out.println(employeeTable.columnKeySet());
        //所有員工名稱
        System.out.println(employeeTable.values());
        //公司中的所有員工和員工編号
        System.out.println(employeeTable.rowMap());
        //員工編号對應的公司和員工名稱
        System.out.println(employeeTable.columnMap());
        //row+column對應的value
        System.out.println(employeeTable.get("IBM","101"));
        //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());
        }

        //table中所有的不重複的key
        Set<String> employers = employeeTable.rowKeySet();
        System.out.print("Employers: ");
        for(String employer: employers){
            System.out.print(employer + " ");
        }
        System.out.println();

        //得到員工編号為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());
        }      

運作結果

[(IBM,101)=Mahesh, (IBM,102)=Ramesh, (IBM,103)=Suresh, (Microsoft,111)=Sohan, (Microsoft,112)=Mohan, (Microsoft,113)=Rohan, (TCS,121)=Ram, (TCS,102)=Shyam, (TCS,123)=Sunil]
[IBM, Microsoft, TCS]
[101, 102, 103, 111, 112, 113, 121, 123]
[Mahesh, Ramesh, Suresh, Sohan, Mohan, Rohan, Ram, Shyam, Sunil]
{IBM={101=Mahesh, 102=Ramesh, 103=Suresh}, Microsoft={111=Sohan, 112=Mohan, 113=Rohan}, TCS={121=Ram, 102=Shyam, 123=Sunil}}
{101={IBM=Mahesh}, 102={IBM=Ramesh, TCS=Shyam}, 103={IBM=Suresh}, 111={Microsoft=Sohan}, 112={Microsoft=Mohan}, 113={Microsoft=Rohan}, 121={TCS=Ram}, 123={TCS=Sunil}}
Mahesh
List of IBM Employees
Emp Id: 101, Name: Mahesh
Emp Id: 102, Name: Ramesh
Emp Id: 103, Name: Suresh
Employers: IBM Microsoft TCS 
Employer: IBM, Name: Ramesh
Employer: TCS, Name: Shyam
      

  

開開心心編碼,快快樂樂生活。

繼續閱讀