Google工具库guava中集合类型Table

399 篇文章 12 订阅
143 篇文章 1 订阅

一、简介

Table:它具有两个key[行, 列],对应一个值

Table是Guava提供的一个接口 Interface Table<R,C,V>

Table可以看成:Table<R,C,V> == Map<R,Map<C,V>>

Table接口有以下实现:

HashBasedTable:基于HashMap<R,HashMap<C,V>>HashMap<R,HashMap<C,V>>的实现。
TreeBasedTable:基于TreeMap<R,TreeMap<C,V>>TreeMap<R,TreeMap<C,V>>的实现。
ImmutableTable:基于ImmutableMap<R,ImmutableMap<C,V>>ImmutableMap<R,ImmutableMap<C,V>>的实现

ImmutableTable:不可变table,创建后不能修改
TreeBasedTable:对行或列排序的table

二、它的主要方法:

S.N.方法 & 描述
1Set<Table.Cell<R,C,V>> cellSet()
返回集合中的所有行键/列键/值三元组。
2void clear()
从表中删除所有映射。
3Map<R,V> column(C columnKey)
返回在给定列键的所有映射的视图。
4Set<C> columnKeySet()
返回一组具有表中的一个或多个值的列键。
5Map<C,Map<R,V>> columnMap()
返回关联的每一列键与行键对应的映射值的视图。
6boolean contains(Object rowKey, Object columnKey)
返回true,如果表中包含与指定的行和列键的映射。
7boolean containsColumn(Object columnKey)
返回true,如果表中包含与指定列的映射。
8boolean containsRow(Object rowKey)
返回true,如果表中包含与指定的行键的映射关系。
9boolean containsValue(Object value)
返回true,如果表中包含具有指定值的映射。
10boolean equals(Object obj)
比较指定对象与此表是否相等。
11V get(Object rowKey, Object columnKey)
返回对应于给定的行和列键,如果没有这样的映射存在值,返回null。
12int hashCode()
返回此表中的哈希码。
13boolean isEmpty()
返回true,如果表中没有映射。
14V put(R rowKey, C columnKey, V value)
关联指定值与指定键。
15void putAll(Table<? extends R,? extends C,? extends V> table)
复制从指定的表中的所有映射到这个表。
16V remove(Object rowKey, Object columnKey)
如果有的话,使用给定键相关联删除的映射。
17Map<C,V> row(R rowKey)
返回包含给定行键的所有映射的视图。
18Set<R> rowKeySet()
返回一组行键具有在表中的一个或多个值。
19Map<R,Map<C,V>> rowMap()
返回关联的每一行按键与键列对应的映射值的视图。
20int size()
返回行键/列键/表中的值映射关系的数量。
21Collection<V> values()
返回所有值,其中可能包含重复的集合。

三、HashBasedTable方法的使用1

1、把数据存储到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");
}

输出结果:

70  90  100 80

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

四、HashBasedTable方法的使用2

1、允许row和column确定的二维点重复

table.put(1, 2, 3);
table.put(1, 6, 3);

2、判断row和column确定的二维点是否存在

if(table.contains(1, 2)) {
    table.put(1, 4, 4);
    table.put(2, 5, 4);
}

3、获取某行或某列对应的数据集

获取column为5的数据集:

Map<Integer, Integer> column = table.column(5);
System.out.println(column);

获取rowkey为1的数据集:

Map<Integer, Integer> row = table.row(1);
System.out.println(row);

获取所有的rowKey值的集合:

Set<Integer> keySet = table.rowKeySet();
System.out.println(keySet);

获取所有的columnKey值的集合:

Set<String> columnSet = tables.columnKeySet();
System.out.println(columnSet);

5、获取某行某列的结果

获取rowKey为1,columnKey为2的的结果

Integer value = table.get(1, 2);
System.out.println(value);

6、判断是否包含某个值或 某行或某列的值

判断是否包含columnKey的值:

System.out.println(table.containsColumn(3));

判断是否包含rowKey为1的视图:

System.out.println(table.containsRow(1));

判断是否包含值为2的集合:

System.out.println(table.containsValue(2));

7、将table转换为Map套Map格式

Map<Integer, Map<Integer, Integer>> rowMap = table.rowMap();
Map<Integer, Map<Integer, Integer>> columnMap = table.columnMap();

8、删除元素或清空集合

删除rowKey为1,columnKey为2的元素,返回删除元素的值:

Integer res = table.remove(1, 2);
System.out.println(res);

清空集合:

table.clear();
System.out.println(table);

最后:TreeBasedTable的使用方式跟HashBasedTable基本相同

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值