Guava Collections - Table Interface

Table Interface

Table represents a special map where two keys can be specified in combined fashion to refer to a single value. It is similar to creating a map of maps.

package org.fool.guava.collections;

import java.util.Map;
import java.util.Set;

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;

public class TableTest {
	public static void main(String[] args) {
		// Table<R,C,V> == Map<R,Map<C,V>>
		// create a table
		Table<String, String, String> employeeTable = HashBasedTable.create();

		// initialize the table with employee details
		employeeTable.put("IBM", "101", "Mahesh");
		employeeTable.put("IBM", "102", "Ramesh");
		employeeTable.put("IBM", "103", "Suresh");

		employeeTable.put("Microsoft", "111", "Sohan");
		employeeTable.put("Microsoft", "112", "Mohan");
		employeeTable.put("Microsoft", "113", "Rohan");

		employeeTable.put("TCS", "121", "Ram");
		employeeTable.put("TCS", "122", "Shyam");
		employeeTable.put("TCS", "123", "Sunil");

		// get Map corresponding to IBM
		Map<String, String> ibmEmployees = employeeTable.row("IBM");

		System.out.println("List of IBM Employees");
		ibmEmployees.forEach((id, name) -> System.out.println("Emp Id: " + id + ", Name: " + name));

		// get all the unique keys of the table
		Set<String> employers = employeeTable.rowKeySet();
		System.out.print("Employers: ");
		employers.forEach(employer -> System.out.print(employer + " "));
		System.out.println();
		
		//get a Map corresponding to 102
		Map<String,String> employerMap = employeeTable.column("102");
		employerMap.forEach((id, name) -> System.out.println("Emp Id: " + id + ", Name: " + name));
	}
}

 output

List of IBM Employees
Emp Id: 103, Name: Suresh
Emp Id: 101, Name: Mahesh
Emp Id: 102, Name: Ramesh
Employers: IBM TCS Microsoft 
Emp Id: IBM, Name: Ramesh

 

 

Guava类库中的Table是一个双键的Map,可以理解为一个行列式的数据结构。 Table可以用来表示一个映射,其中每个键都有一个与之关联的值,而且这个键需要由两个参数来确定。在Table中,第一个键称为"行键",第二个键称为"列键",而对应的值称为"值"。 Table的实现方式可以看作是一个Map<RowKey, Map<ColumnKey, Value>>的嵌套结构,其中RowKey和ColumnKey分别表示行键和列键,Value表示对应的值。 Table提供了多种视图,包括行视图、列视图、单元格视图等,这些视图可以方便地进行表格的操作和查询。 下面是一个简单的示例代码: ```java Table<String, String, Integer> table = HashBasedTable.create(); table.put("row1", "col1", 1); table.put("row1", "col2", 2); table.put("row2", "col1", 3); table.put("row2", "col2", 4); System.out.println(table.get("row1", "col1")); // 输出1 System.out.println(table.row("row1")); // 输出{col1=1, col2=2} System.out.println(table.column("col1")); // 输出{row1=1, row2=3} System.out.println(table.cellSet()); // 输出[(row1,col1)=1, (row1,col2)=2, (row2,col1)=3, (row2,col2)=4] ``` 在上面的示例中,我们创建了一个Table对象,并往其中添加了四个元素。然后,我们分别通过get方法、row方法、column方法和cellSet方法获取了对应的视图,并输出了它们的内容。 需要注意的是,Table中的行键、列键和值都可以为null,但是在使用时需要特别注意空指针异常的问题。此外,Table中的行键和列键必须实现equals和hashCode方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值