qq1452718326 2019-01-18 15:34:07

善用二维数组,HashBasedTable 双键Map

		Table<Integer, Integer, Integer> table = HashBasedTable.<Integer, Integer, Integer>create();
		table.put(1, 2, 3);
		//允许row和column确定的二维点重复
		table.put(1, 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);
		System.out.println("column:" + column);
		//获取column为5的数据集2
		Map<Integer, Integer> column2 = table.column(2);
		System.out.println("column2:" + column2);
		//获取rowkey为1的数据集
		Map<Integer, Integer> row = table.row(1);
		System.out.println("row:" + row);
		//获取rowKey为1,columnKey为2的的结果
		Integer value = table.get(1, 2);
		System.out.println("value:" + value);
		//判断是否包含columnKey的值
		System.out.println("containsColumn:" + table.containsColumn(3));
		//判断是否包含rowKey为1的视图
		System.out.println("containsRow:" + table.containsRow(1));
		//判断是否包含值为2的集合
		System.out.println("containsValue:" + table.containsValue(2));
		//将table转换为Map套Map格式
		Map<Integer, Map<Integer, Integer>> rowMap = table.rowMap();
		System.out.println("rowMap:" + rowMap);
		//获取所有的rowKey值的集合
		Set<Integer> keySet = table.rowKeySet();
		System.out.println("keySet:" + 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}}
		column:{2=4}
		column2:{1=3}
		row:{4=4, 2=3, 6=3}
		value:3
		containsColumn:false
		containsRow:true
		containsValue:false
		rowMap:{1={4=4, 2=3, 6=3}, 2={5=4}}
		keySet:[1, 2]
		3
		{}

评论

trang chủ - Wiki
Copyright © 2011-2024 iteam. Current version is 2.137.3. UTC+08:00, 2024-11-28 13:49
浙ICP备14020137号-1 $bản đồ khách truy cập$