Guava 集合工具类 Tables
提供 Table 的静态方法。
一、接口声明
以下是 com.google.common.collect.Tables 接口的声明:
@GwtCompatible public final class Tables extends Object
二、接口方法
官方文档:https://google.github.io/guava/releases/27.0.1-jre/api/docs/com/google/common/collect/Tables.html
| 修饰符和类型 | 方法描述 |
|---|---|
static <R,C,V>Table.Cell<R,C,V> | immutableCell(R rowKey, C columnKey, V value) 返回具有指定行键,列键和值的不可变单元格. |
static <R,C,V>Table<R,C,V> | newCustomTable(Map<R,Map<C,V>> backingMap, Supplier<? extends Map<C,V>> factory) 创建一个使用指定的支持映射和工厂的表. |
static <R,C,V>Table<R,C,V> | synchronizedTable(Table<R,C,V> table) 返回由指定表支持的同步(线程安全)表. |
static <T,R,C,V,Iextends Table<R,C,V>> Collector<T,?,I> | toTable(Function<? super T,? extends R> rowFunction, Function<? super T,?extends C> columnFunction,Function<? super T,? extends V> valueFunction, BinaryOperator<V> mergeFunction, Supplier<I> tableSupplier) 返回一个Collector,它将元素累积到特定指定的表中,通过将提供的映射函数应用于输入元素来生成其cells. |
static <T,R,C,V,Iextends Table<R,C,V>> Collector<T,?,I> | toTable(Function<? super T,? extends R> rowFunction, Function<? super T,?extends C> columnFunction,Function<? super T,? extends V> valueFunction, Supplier<I> tableSupplier) 返回一个Collector,它将元素累积到特定指定的表中,通过将提供的映射函数应用于输入元素来生成其cells. |
static <R,C,V1,V2>Table<R,C,V2> | transformValues(Table<R,C,V1> fromTable, Function<? super V1,V2> function) 返回表的视图,其中每个值都由函数转换。 |
static <R,C,V>Table<C,R,V> | transpose(Table<R,C,V> table) 创建翻转其行键和列键的给定表的转置视图. |
static <R,C,V>RowSortedTable<R,C,V> | unmodifiableRowSortedTable(RowSortedTable<R,? extends C,? extends V> table) 返回指定行排序表的不可修改视图. |
static <R,C,V>Table<R,C,V> | unmodifiableTable(Table<? extends R,? extends C,? extends V> table) 返回指定表的不可修改视图. |
三、测试类
package com.example.guava.collect;
import com.google.common.base.Function;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import com.google.common.collect.Table.Cell;
import com.google.common.collect.Tables;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.junit.Test;
import java.util.Set;
public class TablesTest {
/**
* 返回具有指定行键,列键和值的不可变单元格
*/
@Test
public void test_immutableCell() {
Cell<String, Integer, Character> entry = Tables.immutableCell("foo", 1, 'a');
System.out.println(entry.getRowKey()); // foo
System.out.println(entry.getColumnKey()); // 1
System.out.println(entry.getValue()); // a
System.out.println(entry); // (foo,1)=a
Cell<String, Integer, Character> nullEntry = Tables.immutableCell(null, null, null);
System.out.println(nullEntry.toString()); // (null,null)=null
}
/**
* transpose rowKey与columnKey翻转
*/
@Test
public void test_transpose() {
Table<String, String, Integer> table = HashBasedTable.create();
table.put("jack", "java", 98);
table.put("jack", "php", 65);
table.put("jack", "ui", 80);
table.put("jack", "mysql", 86);
System.out.println("遍历 table");
Set<Cell<String, String, Integer>> cells = table.cellSet();
cells.forEach(c -> System.out.println(c.getRowKey() + "-" + c.getColumnKey() + " : " + c.getValue()));
System.out.println("\n遍历 transposeTable");
Table<String, String, Integer> transposeTable = Tables.transpose(table);
transposeTable.cellSet().forEach(c -> System.out.println(c.getRowKey() + "-" + c.getColumnKey() + " : " + c.getValue()));
System.out.println("\n遍历 transformValues");
Table<String, String, Object> transformValues = Tables.transformValues(table, new Function<Integer, Object>() {
@Override
public Object apply(@Nullable Integer input) {
return input > 80;
}
});
transformValues.cellSet().forEach(c -> System.out.println(c.getRowKey() + "-" + c.getColumnKey() + " : " + c.getValue()));
}
}四、相关文章
未经允许请勿转载:程序喵 » Google Guava 快速入门 —— 【集合工具】Tables 类
程序喵