- 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