zoukankan      html  css  js  c++  java
  • Table——高淇JAVA300讲笔记之Guava

    案例一:学生成绩表格的行和列转换

     1 package com.bjsxt.others.guava;
     2 
     3 import java.util.Map;
     4 import java.util.Set;
     5 
     6 import com.google.common.collect.HashBasedTable;
     7 import com.google.common.collect.Table;
     8 import com.google.common.collect.Tables;
     9 import com.google.common.collect.Table.Cell;
    10 
    11 /**
    12  * 双键的Map --> Table -->rowKey+columnKey+value
    13  * 1、方法
    14  *   所有的行数据:cellSet()
    15  *   所有的学生:rowKeySet()
    16  *   所有的课程:columnKeySet()
    17  *   所有的成绩:values()
    18  *   学生对应的课程:rowMap() + get(学生)
    19  *                    row(学生)
    20  *   课程对应的学生:columnMap + get(课程)
    21  *                     column(课程)
    22  *
    23  */
    24 public class Demo08 {
    25     public static void main(String[] args) {
    26         Table<String,String,Integer> tables = HashBasedTable.create();
    27         //测试数据
    28         tables.put("a","javase",80);
    29         tables.put("b","javase",90);
    30         tables.put("a", "oracle", 100);
    31         tables.put("c", "oracle", 95);
    32         
    33         //所有的行数据
    34         Set<Cell<String,String,Integer>> cells = tables.cellSet();
    35         for(Cell<String,String,Integer> temp:cells) {
    36             System.out.println(temp.getRowKey()+"-->"+temp.getColumnKey()+"-->"+temp.getValue());
    37         }
    38         
    39         System.out.println("=============学生查看成绩=====================");
    40         System.out.print("学生	");
    41         //所有的课程
    42         Set<String> cours = tables.columnKeySet();
    43         for(String t:cours) {
    44             System.out.print(t+"	");
    45         }
    46         System.out.println();
    47         //所有的学生
    48         Set<String> stus = tables.rowKeySet();
    49         for(String stu:stus) {
    50             System.out.print(stu+"	");
    51             Map<String,Integer> scores = tables.row(stu);
    52             for(String c:cours) {
    53                 System.out.print(scores.get(c)+"	");
    54             }
    55             System.out.println();
    56         }
    57         
    58         System.out.println("=============课程查看成绩=====================");
    59         System.out.print("课程	");
    60         //所有的学生
    61         Set<String> stuSet = tables.rowKeySet();
    62         for(String t:stuSet) {
    63             System.out.print(t+"	");
    64         }
    65         System.out.println();
    66         //所有的课程
    67         Set<String> courseSet = tables.columnKeySet();
    68         for(String c:courseSet) {
    69             System.out.print(c+"	");
    70             Map<String,Integer> scores = tables.column(c);
    71             for(String s:stuSet) {
    72                 System.out.print(scores.get(s)+"	");
    73             }
    74             System.out.println();
    75         }
    76         System.out.println("===========转换==============");
    77         Table<String,String,Integer> tables2 = Tables.transpose(tables);
    78         //所有的行数据
    79         Set<Cell<String,String,Integer>> cells2 = tables2.cellSet();
    80         for(Cell<String,String,Integer> temp:cells2) {
    81             System.out.println(temp.getRowKey()+"-->"+temp.getColumnKey()+"-->"+temp.getValue());
    82         }
    83         
    84     }
    85 }

    运行结果:

    a-->javase-->80
    a-->oracle-->100
    b-->javase-->90
    c-->oracle-->95
    =============学生查看成绩=====================
    学生    javase    oracle    
    a    80    100    
    b    90    null    
    c    null    95    
    =============课程查看成绩=====================
    课程    a    b    c    
    javase    80    90    null    
    oracle    100    null    95    
    ===========转换==============
    javase-->a-->80
    oracle-->a-->100
    javase-->b-->90
    oracle-->c-->95
  • 相关阅读:
    【HDOJ】2774 Shuffle
    【POJ】2170 Lattice Animals
    【POJ】1084 Square Destroyer
    【POJ】3523 The Morning after Halloween
    【POJ】3134 Power Calculus
    【Latex】如何在Latex中插入伪代码 —— clrscode3e
    【HDOJ】4801 Pocket Cube 的几种解法和优化
    【HDOJ】4080 Stammering Aliens
    【HDOJ】1800 Flying to the Mars
    SQL语法
  • 原文地址:https://www.cnblogs.com/swimminglover/p/8367682.html
Copyright © 2011-2022 走看看