java hashbasedtable_Guava类库学习--Table(双键的Map)
发布日期:2021-06-24 01:38:37 浏览次数:11 分类:技术文章

本文共 4088 字,大约阅读时间需要 13 分钟。

Table是Guava提供的一个接口 Interface Table,由rowKey+columnKey+value组成 它有两个键,一个值,和一个n行三列的数据表类似,n行取决于Table对对象中存储了多少个数据

主要使用的方法有:

所有行数据:cellSet()

所有第一个key值:rowKeySet()

所有课程:columnKeySet()

所有成绩:values()

课程成绩表:rowMap()+get(stu)/row(stu)

学生成绩表 columnMap()+get(course)/column(course)

给出一个学生-课程-成绩表,测试上面提到的方法,表如下 :

d3f780478de0249577a91469de01da8f.png

1.把数据存储到Table中,通过HashBasedTable.create()新建一个Table对象

Table tables=HashBasedTable.create();

tables.put("a", "javase", 80);

tables.put("b", "javaee", 90);

tables.put("c", "javame", 100);

tables.put("d", "guava", 70);

2.得到所有行数据 tables.cellSet()

Set> cells=tables.cellSet();

for(Cell temp:cells){

System.out.println(temp.getRowKey()+" "+temp.getColumnKey()+" "+temp.getValue());

}

输出结果:

d guava 70

b javaee 90

c javame 100

a javase 80

3.得到所有学生 rowKeySet()

Set students=tables.rowKeySet();

for(String str:students){

System.out.print(str+"\t");

}

输出结果:

d b c a

4.得到所有课程 columnKeySet()

Set courses=tables.columnKeySet();

for(String str:courses){

System.out.print(str+"\t");

}

输出结果:

guava javaee javame javase

5.得到所有成绩:values

Collection scores=tables.values();

for(Integer in:scores){

System.out.print(in+"\t");

}

输出结果:

70 90 100 80

6.得到学生的课程成绩表 rowMap+get(stu)/row(stu)

for(String str:students){

Map rowMap=tables.row(str);

Set> setEntry=rowMap.entrySet();

for(Entry entry:setEntry){

System.out.println(entry.getKey()+" "+entry.getValue());

}

}

输出结果:

guava 70

javaee 90

javame 100

javase 80

7.得到学生的姓名成绩表 columnMap+get(course)/column(course)

for (String str : courses) {

Map rowMap2 = tables.column(str);

Set> setEntry2 = rowMap2.entrySet();

for (Entry entry : setEntry2) {

System.out.println(entry.getKey() + " " + entry.getValue());

}

}

输出结果为:

d 70

b 90

c 100

a 80

Demo1

public class Demo08 {

public static void main(String[] args) {

//新建一个table对象,并存入数据

Table tables=HashBasedTable.create();

tables.put("a", "javase", 80);

tables.put("b", "javaee", 90);

tables.put("c", "javame", 100);

tables.put("d", "guava", 70);

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

System.out.print("学生\t");

//得到所有课程

Set courses=tables.columnKeySet();

for(String str:courses){

System.out.print(str+"\t");

}

System.out.println();

//得到所有学生

Set stus=tables.rowKeySet();

for(String str:stus){

System.out.print(str+"\t");

//课程成绩表

Map scores=tables.row(str);

for(String c:courses){

//输出成绩

System.out.print(scores.get(c)+"\t");

}

System.out.println();

}

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

System.out.print("课程\t");

//得到所有学生

Set stus2=tables.rowKeySet();

for(String str:stus2){

System.out.print(str+"\t");

}

System.out.println();

//得到所有课程

Set courses2=tables.columnKeySet();

for(String str:courses2){

System.out.print(str+"\t");

//得到学生成绩表

Map map=tables.column(str);

for(String stu:stus2){

//输出成绩

System.out.print(map.get(stu)+"\t");

}

System.out.println();

}

System.out.println("---------------------------转换---------------------------");

//将列调换,由学生-课程-成绩表变为 课程-学生-成绩

Table tables2=Tables.transpose(tables);

// 得到所有行数据

Set> cells2 = tables2.cellSet();

for (Cell temp : cells2) {

System.out.println(temp.getRowKey() + " " + temp.getColumnKey()+ " " + temp.getValue());

}

}

}

Demo2

package HashBasedTable;

import java.util.*;

import com.google.common.collect.HashBasedTable;

import com.google.common.collect.Lists;

import com.google.common.collect.Maps;

import com.google.common.collect.Table;

/**

* Created by Administrator on 2018/5/9.

*/

public class Demo01 {

public static void main(String[] args) {

Table table = HashBasedTable.create();

table.put("0010", "i", 1);

table.put("0010", "e", 2);

table.put("0020", "c", 2);

Set columnKeySet = table.columnKeySet();

Set

> cellSet = table.cellSet();

Map column = table.column("e");

Map row = table.row("0010");

Map> rowMap = table.rowMap();

Set rowKeySet = table.rowKeySet();//获取行的值到一个set集合中

List> list = new ArrayList();

int i = 0;

for (String r : rowKeySet) {

Map map = new HashMap();

map = table.row(r);

list.add(i, map);

i++;

}

for (int j = 0; j < list.size(); j++) {

for (String m : list.get(j).keySet()) {

System.out.print(m + " " + list.get(j).get(m));

}

System.out.println();

}

}

}

转载地址:https://blog.csdn.net/weixin_29306261/article/details/114362082 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:python中组合数据类型、函数和代码复用的难点_完成第5章 函数和代码复用 程序练习题P151-152...
下一篇:java runnable main_【BUG】”main” prio=5 tid=1 RUNNABLE

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2024年03月28日 04时36分08秒