Guava collections -- Table
发布日期:2021-06-29 12:51:56 浏览次数:2 分类:技术文章

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

Guava全文介绍地址:Google Guava

这次主要介绍是的com.google.common.collect.Table。Table是包含相有序的一对key值,称为行键和一个列键,一个值的集合。Table这种结构可以用于解决以下的的数据结构.

private HashBasedTable
table = HashBasedTable.create();private Map
> table2 = Maps.newHashMap();

给定的row key对应的映射到可能被看作是一个key值为列的Map。同样也可以将一个列做为Key值把行键/值为value的Map。注意,在一些实现,通过列做为key进行数据访问支持操作性能相比用行做为keyw值好。

返回集合或地图的方法总是返回底层表的内容。更新表可以改变这些集合的内容,并且改变集合也会改变这个table。修改表的所有方法都是可选的,和视图返回的表可能会或可能不会被修改。当修改不受支持,这些方法会抛出UnsupportedOperationException。

下面就介绍一个Table的主要用法:

1、prepare Data

public class TableTest {    private HashBasedTable
table = HashBasedTable.create(); @Before public void setUp() { table.put(1, 1, "Rook"); table.put(1, 2, "Knight"); table.put(1, 3, "Bishop"); table.put(2, 1, "Lateral"); table.put(2, 2, "L-Shape"); table.put(2, 3, "Diagonal"); }}

2、行key与列key联合索引

@Test    public void testHashBasedTablePut(){         assertThat(table.get(1,2),is("Knight"));         assertThat(table.get(5,5),is(nullValue()));    }

3、Column View

@Test    public void testGetColumnView(){        Map
columnMap = table.column(1); assertThat(columnMap.get(1),is("Rook")); assertThat(columnMap.get(2),is("Lateral")); columnMap.put(2,"Changed"); assertThat(table.get(2,1),is("Changed")); columnMap.put(3,"Added Value"); assertThat(table.get(3,1),is("Added Value")); }

4、Row View

@Test    public void testGetRowView(){        Map
rowMap = table.row(1); assertThat(rowMap.get(1),is("Rook")); assertThat(rowMap.get(2),is("Knight")); assertThat(rowMap.get(3),is("Bishop")); rowMap.put(4,"King"); assertThat(table.get(1,4),is("King")); assertThat(table.column(4).get(1),is("King")); }

5、Table Operation

@Test    public void testTableOperations(){        table.contains(1,1);        table.containsColumn(2);        table.containsRow(1);        table.containsValue("Rook");    }

更多功能等待你的发现。

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

上一篇:Guava collections -- Range
下一篇:Guava collections -- BiMap

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年04月12日 05时29分30秒