com.google.common.collect.Table类的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(8.5k)|赞(0)|评价(0)|浏览(361)

本文整理了Java中com.google.common.collect.Table类的一些代码示例,展示了Table类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Table类的具体详情如下:
包路径:com.google.common.collect.Table
类名称:Table

Table介绍

[英]A collection that associates an ordered pair of keys, called a row key and a column key, with a single value. A table may be sparse, with only a small fraction of row key / column key pairs possessing a corresponding value.

The mappings corresponding to a given row key may be viewed as a Map whose keys are the columns. The reverse is also available, associating a column with a row key / value map. Note that, in some implementations, data access by column key may have fewer supported operations or worse performance than data access by row key.

The methods returning collections or maps always return views of the underlying table. Updating the table can change the contents of those collections, and updating the collections will change the table.

All methods that modify the table are optional, and the views returned by the table may or may not be modifiable. When modification isn't supported, those methods will throw an UnsupportedOperationException.

See the Guava User Guide article on Table.
[中]一个集合,将一对有序的键(称为行键和列键)与单个值关联。表可能是稀疏的,只有一小部分行键/列键对具有相应的值。
对应于给定行键的映射可以被视为键为列的映射。反过来也可以,将列与行键/值映射相关联。请注意,在某些实现中,通过列键访问数据可能比通过行键访问数据支持的操作更少或性能更差。
返回集合或映射的方法总是返回基础表的视图。更新表可以更改这些集合的内容,更新集合将更改表。
所有修改表的方法都是可选的,表返回的视图可以修改,也可以不修改。当不支持修改时,这些方法将抛出UnsupportedOperationException。
请参阅关于Table的Guava用户指南文章。

代码示例

代码示例来源:origin: google/guava

void put(R row, C column, V value, BinaryOperator<V> merger) {
 MutableCell<R, C, V> oldCell = table.get(row, column);
 if (oldCell == null) {
  MutableCell<R, C, V> cell = new MutableCell<>(row, column, value);
  insertionOrder.add(cell);
  table.put(row, column, cell);
 } else {
  oldCell.merge(value, merger);
 }
}

代码示例来源:origin: google/guava

@Override
 protected Map<String, Integer> makePopulatedMap() {
  Table<Character, String, Integer> table = HashBasedTable.create();
  table.put('a', "one", 2);
  table.put('a', "two", 4);
  table.put('a', "three", 6);
  table.put('b', "four", 8);
  return Tables.transformValues(table, DIVIDE_BY_2).row('a');
 }
}

代码示例来源:origin: google/guava

@Override
public boolean contains(Object rowKey, Object columnKey) {
 return fromTable.contains(rowKey, columnKey);
}

代码示例来源:origin: google/guava

private static <R, C, V> void merge(
  Table<R, C, V> table, R row, C column, V value, BinaryOperator<V> mergeFunction) {
 checkNotNull(value);
 V oldValue = table.get(row, column);
 if (oldValue == null) {
  table.put(row, column, value);
 } else {
  V newValue = mergeFunction.apply(oldValue, value);
  if (newValue == null) {
   table.remove(row, column);
  } else {
   table.put(row, column, newValue);
  }
 }
}

代码示例来源:origin: google/guava

private ArrayTable(Table<R, C, V> table) {
 this(table.rowKeySet(), table.columnKeySet());
 putAll(table);
}

代码示例来源:origin: google/jimfs

/**
 * Gets the value of the given attribute in the given view.
 */
@Nullable
public synchronized final Object getAttribute(String view, String attribute) {
 if (attributes == null) {
  return null;
 }
 return attributes.get(view, attribute);
}

代码示例来源:origin: google/guava

public void testPutOriginalModifiesTranspose() {
 Table<Integer, String, Character> original = HashBasedTable.create();
 Table<String, Integer, Character> transpose = Tables.transpose(original);
 original.put(1, "foo", 'a');
 assertEquals((Character) 'a', transpose.get("foo", 1));
}

代码示例来源:origin: google/guava

public void testIterationOrder() {
 Table<String, String, String> table = HashBasedTable.create();
 for (int i = 0; i < 5; i++) {
  table.put("r" + i, "c" + i, "v" + i);
 }
 assertThat(table.rowKeySet()).containsExactly("r0", "r1", "r2", "r3", "r4").inOrder();
 assertThat(table.columnKeySet()).containsExactly("c0", "c1", "c2", "c3", "c4").inOrder();
 assertThat(table.values()).containsExactly("v0", "v1", "v2", "v3", "v4").inOrder();
}

代码示例来源:origin: google/guava

public void testCreateCopyArrayTable() {
 Table<String, Integer, Character> original =
   create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
 Table<String, Integer, Character> copy = ArrayTable.create(original);
 assertEquals(original, copy);
 original.put("foo", 1, 'd');
 assertEquals((Character) 'd', original.get("foo", 1));
 assertEquals((Character) 'a', copy.get("foo", 1));
 assertEquals(copy.rowKeySet(), original.rowKeySet());
 assertEquals(copy.columnKeySet(), original.columnKeySet());
}

代码示例来源:origin: google/guava

public void testRowClearAndPut() {
  if (supportsRemove()) {
   table = create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
   Map<Integer, Character> row = table.row("foo");
   assertEquals(ImmutableMap.of(1, 'a', 3, 'c'), row);
   table.remove("foo", 3);
   assertEquals(ImmutableMap.of(1, 'a'), row);
   table.remove("foo", 1);
   assertEquals(ImmutableMap.of(), row);
   table.put("foo", 2, 'b');
   assertEquals(ImmutableMap.of(2, 'b'), row);
   row.clear();
   assertEquals(ImmutableMap.of(), row);
   table.put("foo", 5, 'x');
   assertEquals(ImmutableMap.of(5, 'x'), row);
  }
 }
}

代码示例来源:origin: google/guava

@Override
protected Table<String, Integer, Character> create(Object... data) {
 Table<String, Integer, String> table = HashBasedTable.create();
 checkArgument(data.length % 3 == 0);
 for (int i = 0; i < data.length; i += 3) {
  String value = (data[i + 2] == null) ? null : (data[i + 2] + "transformed");
  table.put((String) data[i], (Integer) data[i + 1], value);
 }
 return Tables.transformValues(table, FIRST_CHARACTER);
}

代码示例来源:origin: google/guava

@Override
 protected Map<String, Integer> makePopulatedMap() {
  Table<String, Character, Integer> table = HashBasedTable.create();
  table.put("one", 'a', 1);
  table.put("two", 'a', 2);
  table.put("three", 'a', 3);
  table.put("four", 'b', 4);
  return Tables.unmodifiableTable(table).column('a');
 }
}

代码示例来源:origin: google/guava

@Override
protected Table<String, Integer, Character> create(Object... data) {
 Table<String, Integer, Character> table = HashBasedTable.create();
 table.put("foo", 4, 'a');
 table.put("cat", 1, 'b');
 table.clear();
 populate(table, data);
 return table;
}

代码示例来源:origin: google/guava

@Override
public Set<Cell<String, Integer, Character>> create(Object... elements) {
 Table<String, Integer, Character> table = createTable();
 for (Object element : elements) {
  @SuppressWarnings("unchecked")
  Cell<String, Integer, Character> cell = (Cell<String, Integer, Character>) element;
  table.put(cell.getRowKey(), cell.getColumnKey(), cell.getValue());
 }
 return table.cellSet();
}

代码示例来源:origin: google/guava

public void testCreateExplicitComparators() {
 table = TreeBasedTable.create(Collections.reverseOrder(), Ordering.usingToString());
 table.put("foo", 3, 'a');
 table.put("foo", 12, 'b');
 table.put("bar", 5, 'c');
 table.put("cat", 8, 'd');
 assertThat(table.rowKeySet()).containsExactly("foo", "cat", "bar").inOrder();
 assertThat(table.row("foo").keySet()).containsExactly(12, 3).inOrder();
}

代码示例来源:origin: google/guava

@Override
 protected Map<String, Integer> makePopulatedMap() {
  Table<Character, String, Integer> table = makeTable();
  table.put('a', "one", 1);
  table.put('a', "two", 2);
  table.put('a', "three", 3);
  table.put('b', "four", 4);
  return table.row('a');
 }
}

代码示例来源:origin: google/guava

@Override
 protected Map<String, Map<Integer, Character>> makePopulatedMap() {
  Table<Integer, String, Character> table = HashBasedTable.create();
  table.put(1, "foo", 'a');
  table.put(1, "bar", 'b');
  table.put(3, "foo", 'c');
  return Tables.unmodifiableTable(table).columnMap();
 }
}

代码示例来源:origin: google/guava

@Override
 public Set<Cell<String, Integer, Character>> create(Object... elements) {
  Table<String, Integer, Character> table = HashBasedTable.create();
  for (Object element : elements) {
   @SuppressWarnings("unchecked")
   Cell<String, Integer, Character> cell =
     (Cell<String, Integer, Character>) element;
   table.put(cell.getRowKey(), cell.getColumnKey(), cell.getValue());
  }
  return Tables.unmodifiableTable(table).cellSet();
 }
})

代码示例来源:origin: google/guava

@Override
public V2 get(Object rowKey, Object columnKey) {
 // The function is passed a null input only when the table contains a null
 // value.
 return contains(rowKey, columnKey) ? function.apply(fromTable.get(rowKey, columnKey)) : null;
}

代码示例来源:origin: google/guava

public void testCreateCopy() {
 Table<String, Integer, Character> original =
   create("foo", 1, 'a', "bar", 1, 'b', "foo", 3, 'c');
 Table<String, Integer, Character> copy = HashBasedTable.create(original);
 assertEquals(original, copy);
 assertEquals((Character) 'a', copy.get("foo", 1));
}

相关文章