1、guava的table类,可代替map
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import java.util.Map;
import java.util.Set;
public class Test03 {
public static void main(String[] args) {
Table employeeTable = HashBasedTable.create();
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", "102", "Shyam");
employeeTable.put("TCS", "123", "Sunil");
//返回所有行数据
System.out.println(employeeTable.cellSet());
//返回所有公司
System.out.println(employeeTable.rowKeySet());
//返回所有员工编号
System.out.println(employeeTable.columnKeySet());
//返回所有员工姓名
System.out.println(employeeTable.values());
//公司中的所有员工和员工编号
System.out.println(employeeTable.rowMap());
//员工编号对应的公司和员工名称
System.out.println(employeeTable.columnMap());
//IBM公司中所有信息
Map ibmEmployees = employeeTable.row("IBM");
//row+column对应的value
System.out.println(employeeTable.get("IBM","101"));
System.out.println("List of IBM Employees");
for (Map.Entry entry : ibmEmployees.entrySet()) {
System.out.println("Emp Id: " + entry.getKey() + ", Name: " + entry.getValue());
}
//table中所有的不重复的key
Set employers = employeeTable.rowKeySet();
System.out.print("Employers: ");
for (String employer : employers) {
System.out.print(employer + " ");
}
System.out.println();
//得到员工编号为102的所有公司和姓名
Map EmployerMap = employeeTable.column("102");
for (Map.Entry entry : EmployerMap.entrySet()) {
System.out.println("Employer: " + entry.getKey() + ", Name: " + entry.getValue());
}
}
}
结果:
[(IBM,101)=Mahesh, (IBM,102)=Ramesh, (IBM,103)=Suresh, (Microsoft,111)=Sohan, (Microsoft,112)=Mohan, (Microsoft,113)=Rohan, (TCS,121)=Ram, (TCS,102)=Shyam, (TCS,123)=Sunil]
[IBM, Microsoft, TCS]
[101, 102, 103, 111, 112, 113, 121, 123]
[Mahesh, Ramesh, Suresh, Sohan, Mohan, Rohan, Ram, Shyam, Sunil]
{IBM={101=Mahesh, 102=Ramesh, 103=Suresh}, Microsoft={111=Sohan, 112=Mohan, 113=Rohan}, TCS={121=Ram, 102=Shyam, 123=Sunil}}
{101={IBM=Mahesh}, 102={IBM=Ramesh, TCS=Shyam}, 103={IBM=Suresh}, 111={Microsoft=Sohan}, 112={Microsoft=Mohan}, 113={Microsoft=Rohan}, 121={TCS=Ram}, 123={TCS=Sunil}}
Mahesh
List of IBM Employees
Emp Id: 101, Name: Mahesh
Emp Id: 102, Name: Ramesh
Emp Id: 103, Name: Suresh
Employers: IBM Microsoft TCS
Employer: IBM, Name: Ramesh
Employer: TCS, Name: Shyam