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>>

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

 

 

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

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM