Guide to Guava Table

Java中HashMap是一个key对应一个value,正好在项目中遇到需要两个key的去mapping一个value的情况,发现Guava的Table非常完美的满足了这样一个矩阵的数据结构,我得mark下,so show my code:

   @Test
    public void guavaTableTest() {
        Table<Integer, Integer, String> animalTable = HashBasedTable.create();
        // rowKey和columnKey决定一个value
        animalTable.put(1, 2, "cat");  // rowKey: 1 columnKey: 2 value: cat
        animalTable.put(1, 2, "wolf");  // 两个key一样的话, 覆盖 上一个的值
        System.out.println(animalTable);   // {1={2=wolf}}
        System.out.println(animalTable.get(1, 3));     // null
        System.out.println(animalTable.get(1, 2));     // wolf

        Table<Integer, Integer, String> zooTable = HashBasedTable.create();
        zooTable.put(1, 2, "dog");
        zooTable.put(1, 3, "lion");

        animalTable.putAll(zooTable);   // zooTable会覆盖掉animalTable中相同的rowKey和columnKey指向的值

        System.out.println(animalTable);  // {1={2=dog, 3=lion}}
    }

Output:

{1={2=wolf}}
null
wolf
{1={2=dog, 3=lion}}

参考:
细节可参考Guide to Guava Table
经常看他们家的文章,写得通俗易懂!

Java类库Google Guava扩展项目学习笔记
Guava - EventBus(事件总线)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值