分享

guava之Table

 jasonbetter 2019-03-27

guava之Table

Table

双键的 map
rowKey+columnKey+value
以学车成绩表来举例

成绩表

方法:
所有的行数据:cellSet();
所有的学生:rowKeySet();
所有的课程:columnKeySet();
所有的成绩:values();

学生对应的课程和成绩:rowMap();课程为键 或者row(学生)
课程对应的学生和成绩:columnMap();学生为键 或者column(课程)

示例程序

public static void main(String[] args) {
        Table<String, String, Integer> table=HashBasedTable.create();
        table.put("黎明", "javase", 80);
        table.put("黎明", "oracle", 100);
        table.put("郭富城", "javase", 90);
        table.put("刘德华", "oracle", 95);        
        Set<Cell<String, String, Integer>> cells=table.cellSet();        //每行来输出
        for(Cell<String, String, Integer> c:cells)
            System.out.println(c.getRowKey()+"----->"+c.getColumnKey()+"----->"+c.getValue());
        
        
        System.out.println("=================学生对应的课程成绩====================");
        System.out.print("学生\t");        //得到装着学生的set再输出
        Set<String> stus=table.rowKeySet();        for(String temp:stus)
            System.out.print(temp+"\t");
        System.out.println();        //得到课程的set
        Set<String> courses=table.columnKeySet();        for(String c:courses){
            System.out.print(c+"\t");            //获得学生和成绩得map,学生为键
            Map<String, Integer> scores=table.column(c);            for(String stu:stus)
                System.out.print(scores.get(stu)+"\t");
            System.out.println();
        }        //原理同上
        System.out.println("==================课程对应的学生成绩===================");
        System.out.print("课程\t");        Set<String> coures2=table.columnKeySet();        for(String temp:coures2)
            System.out.print(temp+"\t");
        System.out.println();        
        Set<String> stus2=table.rowKeySet();        for(String s:stus2){
            System.out.print(s+"\t");            Map<String, Integer> scores=table.row(s);            for(String c:coures2)
                System.out.print(scores.get(c)+"\t");
            System.out.println();
        }
    }

作者:乙木真人
链接:https://www.jianshu.com/p/fefb8d842d3b
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多