com.google.common.collect.Table 存放的数据,以2个键(rowKey+columnKey)一个值的形式,提供了以下方法:
cellSet()
rowKeySet()
columnKeySet()
values()
以下是以学生课程实例代码:
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<String,String,Integer> tables = HashBasedTable.create(); //测试数据 tables.put("a", "javase", 80); tables.put("b", "javase", 90); tables.put("a", "oracle", 100); tables.put("c", "oracle", 95); //所有的行数据 Set<Cell<String,String,Integer>> cells = tables.cellSet(); for(Cell<String,String,Integer> temp:cells){ System.out.println(temp.getRowKey()+":"+temp.getColumnKey()+":"+temp.getValue()); } /** * b:javase:90 c:oracle:95 a:oracle:100 a:javase:80 */ System.out.println("==========学生查看成绩=============="); System.out.print("学生\t"); //所有的课程 Set<String> cours = tables.columnKeySet(); for(String t:cours){ System.out.print(t+"\t"); } System.out.println(); //所有的学生 Set<String> stus = tables.rowKeySet(); for(String stu:stus){ System.out.print(stu+"\t"); Map<String,Integer> scores = tables.row(stu); for(String c:cours){ System.out.print(scores.get(c)+"\t"); } System.out.println(); } /** * 学生 javase oracle b 90 null c null 95 a 80 100 */ System.out.println("==========课程查看成绩=============="); System.out.print("课程\t"); //所有的学生 Set<String> stuSet = tables.rowKeySet(); for(String t:stuSet){ System.out.print(t+"\t"); } System.out.println(); //所有的课程 Set<String> courSet = tables.columnKeySet(); for(String c:courSet){ System.out.print(c+"\t"); Map<String,Integer> scores = tables.column(c); for(String s:stuSet){ System.out.print(scores.get(s)+"\t"); } System.out.println(); } /** * 课程 b c a javase 90 null 80 oracle null 95 100 */ System.out.println("===========转换==========="); Table<String,String,Integer> tables2 = Tables.transpose(tables); //所有的行数据 Set<Cell<String,String,Integer>> cells2 = tables2.cellSet(); for(Cell<String,String,Integer> temp:cells2){ System.out.println(temp.getRowKey()+":"+temp.getColumnKey()+":"+temp.getValue()); } /** * javase:b:90 oracle:c:95 oracle:a:100 */
转自:(1条消息) com.google.common.collect.Table 双键的Map_woshimyc的博客-CSDN博客_com.google.common.collect.table
标签:Map,google,tables,System,collect,println,out From: https://www.cnblogs.com/wwssgg/p/16587152.html