GUAVA(三)之Table_成绩表行转列

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40646143/article/details/83890615

双键的Map->Table ,rowKey+coulmnKey+value

常用的方法:

查询全部的数据-->:cellSet();

查询所有的rowKey-->rowKeySet();

查询所有的-->coulmnKeySet();

查询所有的values-->values();

查询rowkey对应的column-->rowMap().get(rowkey)  或者 row(rowkey)

查询columnKey对应的rowKey-->columnMap().get(columnKey)  或者 column(column)

package com.hp.collections;

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

import java.util.Map;
import java.util.Set;

/**
 * cellSet()获取全部学生的课程/成绩 -->注意如果row字段重复  则只显示一个
 * columnKeySet() 查询全部学生的课程
 */
public class Demo6 {

    public static void main(String[] args) {
        Table<String, String, Integer> table = HashBasedTable.create();
        //            row        coulmn     value
        table.put("a同学", "javase", 80);
        table.put("b同学", "C++", 56);
        table.put("c同学", "C#", 89);
        table.put("d同学", "C#", 70);
        Set<Table.Cell<String, String, Integer>> cells = table.cellSet();
        for (Table.Cell<String, String, Integer> tmp : cells) {
            System.out.println("学生是" + tmp.getRowKey() + "-->课程是" + tmp.getColumnKey() + "成绩为" + tmp.getValue());
        }
        System.out.println("######学生查看自己的成绩############");
        //查询全部的课程
        Set<String> stringSet = table.columnKeySet();
        System.out.print("学生"+"\t");
        //stringSet.stream().forEach(e -> System.out.print(e+"\t"));
        for (String s:stringSet) {
            System.out.print(s+"\t");
        }
        System.out.println();
        //查询全部的学生
        Set<String> rowKeySet = table.rowKeySet();
        for (String str:rowKeySet) {
            System.out.print(str+"\t");
            //查询每一门学生的每一门课程
            Map<String, Integer> row = table.row(str);
           stringSet.stream().forEach(e-> System.out.print(row.get(e)+"\t"));
            System.out.println();
        }


        System.out.println("###############课程查看学生的成绩#############");

        //查询全部的学生
        Set<String> rowSet = table.rowKeySet();
        System.out.print("课程"+"\t");
        //stringSet.stream().forEach(e -> System.out.print(e+"\t"));
        for (String s:rowSet) {
            System.out.print(s+"\t");
        }
        System.out.println();
        //查询全部的课程
        Set<String> columnKeySet = table.columnKeySet();
        for (String str:columnKeySet) {
            System.out.print(str+"\t");
            //查询每一门学生的每一门成绩

            Map<String, Integer> column = table.column(str);
            rowSet.stream().forEach(e-> System.out.print(column.get(e)+"\t"));
            System.out.println();
        }

    }
}

猜你喜欢

转载自blog.csdn.net/qq_40646143/article/details/83890615