Guava Table 示例教程
本文带你学习Guava Table 接口及其实现。Guava Table 是类似表格结构的集合,包含行、列以及相应的单元格值。行和列作为有序的键。
概述
如果把Guava Table和java核心类中的集合进行比较,则为行map,且每行包含列map,其中有对应的单元格值。Table表示一个特定的map,有两个key以组合的形式引用单个值。类似场景一个map的map,举例:Map<UniversityName, Map<CoursesOffered, SeatAvailable>>。
依赖
maven依赖如下,读者可以查找对应版本:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>21.0</version>
</dependency>
创建
有多种方式创建其实例:
- 使用HashBasedTable 类的create方法,其内部使用LinkedHashMap 类:
Table<String, String, Integer> universityCourseSeatTable
= HashBasedTable.create();
- 如果我们需要Table的行key和列key有序(按自然顺序或提供的比较器排序),可以使用TreeBasedTable的create方法,其内部使用TreeMap:
Table<String, String, Integer> universityCourseSeatTable
= TreeBasedTable.create();
- 如何事先就确定行key和列可以,且table大小固定,可以使用ArrayTable的create方法:
List<String> universityRowTable
= Lists.newArrayList("Mumbai", "Harvard");
List<String> courseColumnTables
= Lists.newArrayList("Chemical", "IT", "Electrical");
Table<String, String, Integer> universityCourseSeatTable
= ArrayTable.create(universityRowTable, courseColumnTables);
- 如果需要创建不可变的table实例,其内部数据不能修改,可以使用ImmutableTable 类,使用其builder:
Table<String, String, Integer> universityCourseSeatTable
= ImmutableTable.<String, String, Integer> builder()
.put("Mumbai", "Chemical", 120).build();
应用
下面通过简单示例说明如何应用Table:
取值
如果确定行key和列key,那么可以通过它们获取关联值:
@Test
public void givenTable_whenGet_returnsSuccessfully() {
Table<String, String, Integer> universityCourseSeatTable
= HashBasedTable.create();
universityCourseSeatTable.put("Mumbai", "Chemical", 120);
universityCourseSeatTable.put("Mumbai", "IT", 60);
universityCourseSeatTable.put("Harvard", "Electrical", 60);
universityCourseSeatTable.put("Harvard", "IT", 120);
int seatCount
= universityCourseSeatTable.get("Mumbai", "IT");
Integer seatCountForNoEntry
= universityCourseSeatTable.get("Oxford", "IT");
assertThat(seatCount).isEqualTo(60);
assertThat(seatCountForNoEntry).isEqualTo(null);
}
检查项是否存在
可以通过下面几种方式检查table的项是否存在:
- 行key
- 列key
- 行key和列key
- 值
请看示例:
@Test
public void givenTable_whenContains_returnsSuccessfully() {
Table<String, String, Integer> universityCourseSeatTable
= HashBasedTable.create();
universityCourseSeatTable.put("Mumbai", "Chemical", 120);
universityCourseSeatTable.put("Mumbai", "IT", 60);
universityCourseSeatTable.put("Harvard", "Electrical", 60);
universityCourseSeatTable.put("Harvard", "IT", 120);
boolean entryIsPresent
= universityCourseSeatTable.contains("Mumbai", "IT");
boolean courseIsPresent
= universityCourseSeatTable.containsColumn("IT");
boolean universityIsPresent
= universityCourseSeatTable.containsRow("Mumbai");
boolean seatCountIsPresent
= universityCourseSeatTable.containsValue(60);
assertThat(entryIsPresent).isEqualTo(true);
assertThat(courseIsPresent).isEqualTo(true);
assertThat(universityIsPresent).isEqualTo(true);
assertThat(seatCountIsPresent).isEqualTo(true);
}
删除
可以通过行key和列key从table中删除项:
@Test
public void givenTable_whenRemove_returnsSuccessfully() {
Table<String, String, Integer> universityCourseSeatTable
= HashBasedTable.create();
universityCourseSeatTable.put("Mumbai", "Chemical", 120);
universityCourseSeatTable.put("Mumbai", "IT", 60);
int seatCount
= universityCourseSeatTable.remove("Mumbai", "IT");
assertThat(seatCount).isEqualTo(60);
assertThat(universityCourseSeatTable.remove("Mumbai", "IT")).
isEqualTo(null);
}
获取行key和单元格值的映射map
通过列key 获取行key和单元格值的映射map:
@Test
public void givenTable_whenColumn_returnsSuccessfully() {
Table<String, String, Integer> universityCourseSeatTable
= HashBasedTable.create();
universityCourseSeatTable.put("Mumbai", "Chemical", 120);
universityCourseSeatTable.put("Mumbai", "IT", 60);
universityCourseSeatTable.put("Harvard", "Electrical", 60);
universityCourseSeatTable.put("Harvard", "IT", 120);
Map<String, Integer> universitySeatMap
= universityCourseSeatTable.column("IT");
assertThat(universitySeatMap).hasSize(2);
assertThat(universitySeatMap.get("Mumbai")).isEqualTo(60);
assertThat(universitySeatMap.get("Harvard")).isEqualTo(120);
}
获取table的map表示
通过使用columnMap方法,可以获取 Map<UniversityName, Map<CoursesOffered, SeatAvailable>> 形式的map结构:
@Test
public void givenTable_whenColumnMap_returnsSuccessfully() {
Table<String, String, Integer> universityCourseSeatTable
= HashBasedTable.create();
universityCourseSeatTable.put("Mumbai", "Chemical", 120);
universityCourseSeatTable.put("Mumbai", "IT", 60);
universityCourseSeatTable.put("Harvard", "Electrical", 60);
universityCourseSeatTable.put("Harvard", "IT", 120);
Map<String, Map<String, Integer>> courseKeyUniversitySeatMap
= universityCourseSeatTable.columnMap();
assertThat(courseKeyUniversitySeatMap).hasSize(3);
assertThat(courseKeyUniversitySeatMap.get("IT")).hasSize(2);
assertThat(courseKeyUniversitySeatMap.get("Electrical")).hasSize(1);
assertThat(courseKeyUniversitySeatMap.get("Chemical")).hasSize(1);
}
获取列key和单元格值的映射map
通过行key获取列key和单元格值的映射map:
@Test
public void givenTable_whenRow_returnsSuccessfully() {
Table<String, String, Integer> universityCourseSeatTable
= HashBasedTable.create();
universityCourseSeatTable.put("Mumbai", "Chemical", 120);
universityCourseSeatTable.put("Mumbai", "IT", 60);
universityCourseSeatTable.put("Harvard", "Electrical", 60);
universityCourseSeatTable.put("Harvard", "IT", 120);
Map<String, Integer> courseSeatMap
= universityCourseSeatTable.row("Mumbai");
assertThat(courseSeatMap).hasSize(2);
assertThat(courseSeatMap.get("IT")).isEqualTo(60);
assertThat(courseSeatMap.get("Chemical")).isEqualTo(120);
}
获取唯一的行key
rowKeySet 方法可以获取所有行key,并自动去重:
@Test
public void givenTable_whenRowKeySet_returnsSuccessfully() {
Table<String, String, Integer> universityCourseSeatTable
= HashBasedTable.create();
universityCourseSeatTable.put("Mumbai", "Chemical", 120);
universityCourseSeatTable.put("Mumbai", "IT", 60);
universityCourseSeatTable.put("Harvard", "Electrical", 60);
universityCourseSeatTable.put("Harvard", "IT", 120);
Set<String> universitySet = universityCourseSeatTable.rowKeySet();
assertThat(universitySet).hasSize(2);
}
获取唯一的列key
columnKeySet 方法可以获取所有列key,并自动去重:
@Test
public void givenTable_whenColKeySet_returnsSuccessfully() {
Table<String, String, Integer> universityCourseSeatTable
= HashBasedTable.create();
universityCourseSeatTable.put("Mumbai", "Chemical", 120);
universityCourseSeatTable.put("Mumbai", "IT", 60);
universityCourseSeatTable.put("Harvard", "Electrical", 60);
universityCourseSeatTable.put("Harvard", "IT", 120);
Set<String> courseSet = universityCourseSeatTable.columnKeySet();
assertThat(courseSet).hasSize(3);
}
总结
本文我们介绍了Guava中Table类及其常用方法。table提供类似表格的结构,包括行、列以及关联值,在实际项目中非常有用。
本文参考链接:https://blog.csdn.net/neweastsun/article/details/84996250