Guava类库学习--Table(双键的Map)

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

主要使用的方法有:

  • 所有行数据:cellSet()
  • 所有第一个key值:rowKeySet()
  • 所有课程:columnKeySet()
  • 所有成绩:values()
  • 课程成绩表:rowMap()+get(stu)/row(stu)
  • 学生成绩表 columnMap()+get(course)/column(course)

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

输入图片说明

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

Table<String,String,Integer> 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<Cell<String,String,Integer>> cells=tables.cellSet();
for(Cell<String,String,Integer> 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<String> students=tables.rowKeySet();
for(String str:students){
    System.out.print(str+"\t");
}
输出结果:
d   b   c   a

4.得到所有课程 columnKeySet()

Set<String> courses=tables.columnKeySet();
for(String str:courses){
    System.out.print(str+"\t");
}
输出结果:
guava   javaee  javame  javase

5.得到所有成绩:values

Collection<Integer> 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<String,Integer> rowMap=tables.row(str);
    Set<Entry<String,Integer>> setEntry=rowMap.entrySet();
    for(Entry<String,Integer> 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<String, Integer> rowMap2 = tables.column(str);
    Set<Entry<String, Integer>> setEntry2 = rowMap2.entrySet();
    for (Entry<String, Integer> 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<String,String,Integer> 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<String> courses=tables.columnKeySet();
        for(String str:courses){
            System.out.print(str+"\t");
        }

        System.out.println();
        //得到所有学生
        Set<String> stus=tables.rowKeySet();
        for(String str:stus){
            System.out.print(str+"\t");
            //课程成绩表
            Map<String,Integer> 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<String> stus2=tables.rowKeySet();
        for(String str:stus2){
            System.out.print(str+"\t");
        }
        System.out.println();

        //得到所有课程
        Set<String> courses2=tables.columnKeySet();
        for(String str:courses2){
            System.out.print(str+"\t");
            //得到学生成绩表
            Map<String,Integer> map=tables.column(str);
            for(String stu:stus2){
                //输出成绩
                System.out.print(map.get(stu)+"\t");
            }
            System.out.println();
        }

        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());
        }   
    }
}

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<String, String, Integer> table = HashBasedTable.create();


        table.put("0010", "i", 1);
        table.put("0010", "e", 2);
        table.put("0020", "c", 2);

        Set<String> columnKeySet = table.columnKeySet();
        Set<Table.Cell<String, String, Integer>> cellSet = table.cellSet();

        Map<String, Integer> column = table.column("e");

        Map<String, Integer> row = table.row("0010");
        Map<String, Map<String, Integer>> rowMap = table.rowMap();

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

        List<Map<String, Integer>> list = new ArrayList();
        int i = 0;

        for (String r : rowKeySet) {

            Map<String, Integer> 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();
        }
    }
}

猜你喜欢

转载自my.oschina.net/112612/blog/1809237