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

  

posted @ 2018-10-19 14:38  mickey007  阅读(6703)  评论(0编辑  收藏  举报