阅读背景:

Google Guava学习(8)-Guava集合工具 Table接口

来源:互联网 

1. 功能:简化表格操作,形似Excel的单元格操作;

2. 代码:

package com.example.google.guava.demo.collection;

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * <code>TableTest</code>
 * </p>
 * Description: Table<R, C, V>接口类似Map<R,Map<C,V>>,R-row,C-column,V-value
 *
 * @author Mcchu
 * @date 2017/10/20 8:25
 */
public class TableTest {

    public static void main(String[] args) {
        // 1.创建表格Table
        Table<String, String, String> employeeTable = HashBasedTable.create();

        // 2.初始化表格Table,参数分别对应表格的行、列、值
        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");

        // 3.获取表格中某个单元格的值
        String IBM103name = employeeTable.get("IBM","103");
        System.out.println("3.行key为IBM列key为103的员工值:"+IBM103name);

        // 4.转换为Map,将Table<R,C,V>转换成Map<C,V>形势
        Map<String,String> ibmEmployees =  employeeTable.row("IBM");
        System.out.println("4.IBM员工信息:");
        for (Map.Entry<String,String> entry:ibmEmployees.entrySet()){
            String id = entry.getKey();
            String name = entry.getValue();
            System.out.println("  员工id:"+ id +",员工姓名:"+name);
        }

        // 5.键去重(即获取唯一)
        Set<String> employeeRowKeys = employeeTable.rowKeySet();
        System.out.println("5.获取表中所有唯一键:");
        for (String rowKey: employeeRowKeys){
            System.out.println("  行号:"+rowKey);
        }

        // 6.转换为Map,将Table<R,C,V>转换成Map<R,V>形式
        Map<String,String> employerMap1 =  employeeTable.column("102");
        System.out.println("6.列102的数据:");
        for ( Map.Entry<String,String> entry:employerMap1.entrySet() ){
            String company = entry.getKey();
            String name = entry.getValue();
            System.out.println("  公司名字:"+company+",员工姓名"+name);
        }

        // 7.遍历表格
        Set<Table.Cell<String,String,String>> tableSet = employeeTable.cellSet();
        Iterator<Table.Cell<String,String,String>> it = tableSet.iterator();
        System.out.println("7.遍历Table:");
        while (it.hasNext()){
            Table.Cell<String,String,String> data = it.next();
            String rowCompany = data.getRowKey();
            String columnId = data.getColumnKey();
            String valueName = data.getValue();
            System.out.println("  行号:"+rowCompany+",列号:"+columnId+",值:"+valueName);
        }

        // 8.获取列键
        System.out.println("8.获取所有列键:");
        for (String columnKey : employeeTable.columnKeySet()){
            Map<String,String> rowAndValue = employeeTable.column(columnKey);
            System.out.println("  列:"+columnKey+"对应的数据Map是:"+rowAndValue);
        }

        // 9.
        Collection<String> values = employeeTable.values();
        System.out.println("9.获取所有值:"+values.toString());

        // 10.其他简单方法,不做尝试
        //boolean contains(Object rowKey, Object columnKey)
        //boolean containsColumn(Object columnKey)
        //boolean containsRow(Object rowKey)
        //boolean containsValue(Object value)
        //boolean equals(Object obj)
        //int hashCode()
        //boolean isEmpty()
        //void putAll(Table<? extends R,? extends C,? extends V> table)
        //V remove(Object rowKey, Object columnKey)
        //int size()
    }
}
packa



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: