guava之Table

在guava库中还提供了一种二维表结构:Table。使用Table可以实现二维矩阵的数据结构,可以是稀溜矩阵。通常来说,当你想使用多个键做索引的时候,你可能会用类似Map(FirstName, Map(LastName, Person))的实现,这种方式很丑陋,使用上也不友好。Guava为此提供了新集合类型Table,Table是Guava提供的一个接口 Interface Table(R,C,V),由rowKey(行)+columnKey(列)+value组成 ,它有两个支持所有类型的键:”行”和”列”。Table提供多种视图,以便你从各种角度使用它:
guava之Table_第1张图片

主要使用的方法有: 
    * 返回一个Set集合,包含了所有数据:cellSet() 
    * 返回一个Set集合,集合由第一列键数据也就是rowKey所有数据:rowKeySet() 
    * 返回一个Set集合,集合由第二列键数据也就是columnKey所有数据:columnKeySet() 
    * 返回一个Collection集合,集合由所有value数据组成:values() 
    * 根据行rowKey(第一列,行),返回这一行对应的列和值组成的集合:[rowMap()+get(rows数据)]/row(row数据) 
    * 根据行columnKey(第一列,行),返回这一行对应的列和值组成的集合[columnMap()+get(column数据)]/column(column数据)
Table tables=HashBasedTable.create();
    tables.put("a", "javase", 80);
    tables.put("b", "javaee", 90);
    tables.put("c", "javame", 100);
    tables.put("d", "guava", 70);
for(String str:students){
    Map<String,Integer> rowMap=tables.row(str);
    Set<Entry<String,Integer>> setEntry=rowMap.entrySet();
    for(Entry<String,Integer> entry:setEntry){
        System.out.println(entry.getKey()+" "+entry.getValue());
    }
}
输出结果:
guava 70
javaee 90
javame 100
javase 80
for (String str : courses) {
    Map<String, Integer> rowMap2 = tables.column(str);
    Set<Entry<String, Integer>> setEntry2 = rowMap2.entrySet();
    for (Entry<String, Integer> entry : setEntry2) {
        System.out.println(entry.getKey() + " " + entry.getValue());
    }
}
输出结果为:
d 70
b 90
c 100
a 80
import java.util.Collection;
import java.util.Map;
import java.util.Set;

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

/**
 * 测试Table :Table就是有了双键的Map
 * 学生(rowkey)--课程(columkey)--成绩(value)
 *     lf     --     a      -- 80
 *     dn     --     b      -- 90
 *     cf     --     a      -- 88
 *  
 * @author Administrator
 *
 */
public class Demo06 {
    public static void main(String[] args) {
        Table table = HashBasedTable.create();
        table.put("a", "javase", 80);
        table.put("b", "javase", 90);
        table.put("a", "javame", 100);
        table.put("d", "guava", 70);

        //得到所有的行数据
        Set> cellset = table.cellSet();

        for(Cell temp:cellset){
            System.out.println(temp.getRowKey()+"--"+temp.getColumnKey()+"--"+temp.getValue());
        }

        System.out.println("-------rowKey和columnKey转换---------");
        Table table1 = Tables.transpose(table);
        Set> cellset1 = table1.cellSet();

        for(Cell temp:cellset1){
            System.out.println(temp.getRowKey()+"--"+temp.getColumnKey()+"--"+temp.getValue());
        }

        System.out.println("-------按学生查看成绩---------");

        System.out.print("学生\t");
        Set cours = table.columnKeySet();
        for(String temp:cours){
            System.out.print(temp+"\t");
        }

        System.out.println();

        Set stu = table.rowKeySet();
        for(String temp:stu){
            System.out.print(temp);
            Map map = table.row(temp);
            for(String temp1:cours){
                System.out.print("\t"+map.get(temp1));
            }
            System.out.println();
        }

        System.out.println("-------按课程查看成绩---------");

        System.out.print("课程\t");
        Set stu1 = table.rowKeySet();
        for(String temp:stu1){
            System.out.print(temp+"\t");
        }

        System.out.println();

        Set cours1 = table.columnKeySet();
        for(String temp:cours1){
            System.out.print(temp);
            Map map1 = table.column(temp);
            for(String temp1:stu1){
                System.out.print("\t"+map1.get(temp1));
            }
            System.out.println();
        }
    }

}
输出结果:
        d--guava--70
        b--javase--90
        a--javase--80
        a--javame--100
        -------转换---------
        guava--d--70
        javase--b--90
        javase--a--80
        javame--a--100
        -------按学生查看成绩---------
        学生  guava   javase  javame  
        d   70  null    null
        b   null    90  null
        a   null    80  100
        -------按课程查看成绩---------
        课程  d   b   a   
        guava   70  null    null
        javase  null    90  80
        javame  null    null    100

你可能感兴趣的:(java)