guava 的集合相关

本文探讨了Guava库中的集合特性,重点在于Table和Map的使用。介绍了HashBasedTable和TreeBasedTable的实现,强调了它们在存储数据时的特性,如Table基于HashMap或TreeMap的实现,以及ImmutableTable的不可变性。此外,还提到了ArrayListMultimap在处理多值映射时如何处理重复值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • guava集合相关

1.table相关

    public static void main(String[] args) {

		//HashBasedTable.<Integer, Integer, Integer> 表示为 HashBasedTable.<RowKey, ColumnKey, val>
        Table<Integer, Integer, Integer> table = HashBasedTable.<Integer, Integer, Integer>create();
        table.put(1, 2, 3);
        //允许row和column确定的二维点重复
        table.put(1, 6, 3);
        //table={1={2=3, 6=3}}
        //判断row和column确定的二维点是否存在
        if(table.contains(1, 2)) {
            table.put(1, 4, 4);
            table.put(2, 5, 4);
        }
        System.out.println(table);
        //获取column为5的数据集
        Map<Integer, Integer> column = table.column(5);
        //返回结果是 Map<RowKey, val> 的值
        System.out.println(column);
        //获取rowkey为1的数据集
        Map<Integer, Integer> row = table.row(1);
        System.out.println(row);
        //获取rowKey为1,columnKey为2的的结果
        Integer value = table.get(1, 2);
        System.out.println(value);
        //判断是否包含columnKey的值
        System.out.println(table.containsColumn(3));
        //判断是否包含rowKey为1的视图
        System.out.println(table.containsRow(1));
        //判断是否包含值为2的集合
        System.out.println(table.containsValue(2));
        //将table转换为Map套Map格式
        Map<Integer, Map<Integer, Integer>> rowMap = table.rowMap();
        System.out.println(rowMap);
        //获取所有的rowKey值的集合
        Set<Integer> keySet = table.rowKeySet();
        System.out.println(keySet);
        //删除rowKey为1,columnKey为2的元素,返回删除元素的值
        Integer res = table.remove(1, 2);
        //清空集合
        table.clear();
        System.out.println(res);
        System.out.println(table);
    }

结果

{1={4=4, 2=3, 6=3}, 2={5=4}}
{2=4}
{4=4, 2=3, 6=3}
3
false
true
false
{1={4=4, 2=3, 6=3}, 2={5=4}}
[1, 2]
3
{1={4=4, 6=3}, 2={5=4}}

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>>的实现。
TreeBasedTable的使用方式跟HashBasedTable基本相同,在这里就不在复述了。。。

2.map相关

Multimap<Integer, Integer> map = HashMultimap.<Integer, Integer>create();
map.put(1, 2);
map.put(1, 2);
map.put(1, 3);
map.put(1, 4);
map.put(2, 3);
map.put(3, 3);
map.put(4, 3);
map.put(5, 3);
System.out.println(map);
//结果  {1=[4, 2, 3], 2=[3], 3=[3], 4=[3], 5=[3]}

//判断集合中是否存在key-value为指定值得元素
System.out.println(map.containsEntry(1, 2));
System.out.println(map.containsEntry(1, 6));
//获取key为1的value集合
Collection<Integer> list = map.get(1);
System.out.println(list);
//返回集合中所有key的集合,重复的key将会用key * num的方式来表示
Multiset<Integer> set = map.keys();
System.out.println(set);
//返回集合中所有不重复的key的集合
Set<Integer> kset = map.keySet();
System.out.println(kset);
/*
true
false
[4, 2, 3]
[1 x 3, 2 x 2, 3, 4, 5]
[1, 2, 3, 4, 5]
*/
  • 从上面的结果集可以看出,key不可以重复,相同key的key-value pair 的value值是放在同一个数组中,相同的value会去重

ArrayListMultimap.create()

参考:https://blog.youkuaiyun.com/yaomingyang/article/details/80955872

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值