com.google.common.collect.Maps.uniqueIndex()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(8.6k)|赞(0)|评价(0)|浏览(455)

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

Maps.uniqueIndex介绍

[英]Returns an immutable map for which the Map#values are the given elements in the given order, and each key is the product of invoking a supplied function on its corresponding value.
[中]返回一个不可变的映射,其映射#值是给定顺序中的给定元素,每个键都是对其相应值调用提供的函数的结果。

代码示例

代码示例来源:origin: prestodb/presto

public final void addProperties(ConnectorId connectorId, List<PropertyMetadata<?>> properties)
{
  requireNonNull(connectorId, "connectorId is null");
  requireNonNull(properties, "properties is null");
  Map<String, PropertyMetadata<?>> propertiesByName = Maps.uniqueIndex(properties, PropertyMetadata::getName);
  checkState(connectorProperties.putIfAbsent(connectorId, propertiesByName) == null, "Properties for connector '%s' are already registered", connectorId);
}

代码示例来源:origin: prestodb/presto

public void addConnectorSessionProperties(ConnectorId connectorId, List<PropertyMetadata<?>> properties)
{
  requireNonNull(connectorId, "connectorId is null");
  requireNonNull(properties, "properties is null");
  Map<String, PropertyMetadata<?>> propertiesByName = Maps.uniqueIndex(properties, PropertyMetadata::getName);
  checkState(connectorSessionProperties.putIfAbsent(connectorId, propertiesByName) == null, "Session properties for connectorId '%s' are already registered", connectorId);
}

代码示例来源:origin: prestodb/presto

private static Function<List<ExampleTable>, Map<String, ExampleTable>> resolveAndIndexTables(final URI metadataUri)
{
  return tables -> {
    Iterable<ExampleTable> resolvedTables = transform(tables, tableUriResolver(metadataUri));
    return ImmutableMap.copyOf(uniqueIndex(resolvedTables, ExampleTable::getName));
  };
}

代码示例来源:origin: Graylog2/graylog2-server

@Override
public Map<String, Role> loadAllIdMap() throws NotFoundException {
  final Set<Role> roles = loadAll();
  return Maps.uniqueIndex(roles, new Function<Role, String>() {
    @Nullable
    @Override
    public String apply(Role input) {
      return input.getId();
    }
  });
}

代码示例来源:origin: prestodb/presto

@Override
public Map<String, ColumnHandle> getColumnHandles(ConnectorSession session, ConnectorTableHandle tableHandle)
{
  JmxTableHandle jmxTableHandle = (JmxTableHandle) tableHandle;
  return ImmutableMap.copyOf(Maps.uniqueIndex(jmxTableHandle.getColumnHandles(), column -> column.getColumnName().toLowerCase(ENGLISH)));
}

代码示例来源:origin: prestodb/presto

public void addProcedures(ConnectorId connectorId, Collection<Procedure> procedures)
{
  requireNonNull(connectorId, "connectorId is null");
  requireNonNull(procedures, "procedures is null");
  procedures.forEach(this::validateProcedure);
  Map<SchemaTableName, Procedure> proceduresByName = Maps.uniqueIndex(
      procedures,
      procedure -> new SchemaTableName(procedure.getSchema(), procedure.getName()));
  checkState(connectorProcedures.putIfAbsent(connectorId, proceduresByName) == null, "Procedures already registered for connector: %s", connectorId);
}

代码示例来源:origin: prestodb/presto

public static String printDistributed(SubPlan plan)
{
  List<PlanFragment> fragments = plan.getAllFragments();
  Map<PlanFragmentId, PlanFragment> fragmentsById = Maps.uniqueIndex(fragments, PlanFragment::getId);
  PlanNodeIdGenerator idGenerator = new PlanNodeIdGenerator();
  StringBuilder output = new StringBuilder();
  output.append("digraph distributed_plan {\n");
  printSubPlan(plan, fragmentsById, idGenerator, output);
  output.append("}\n");
  return output.toString();
}

代码示例来源:origin: Graylog2/graylog2-server

@Override
public Map<String, Role> loadAllLowercaseNameMap() throws NotFoundException {
  final Set<Role> roles = loadAll();
  return Maps.uniqueIndex(roles, Roles.roleToNameFunction(true));
}

代码示例来源:origin: prestodb/presto

public static String printLogical(List<PlanFragment> fragments)
{
  Map<PlanFragmentId, PlanFragment> fragmentsById = Maps.uniqueIndex(fragments, PlanFragment::getId);
  PlanNodeIdGenerator idGenerator = new PlanNodeIdGenerator();
  StringBuilder output = new StringBuilder();
  output.append("digraph logical_plan {\n");
  for (PlanFragment fragment : fragments) {
    printFragmentNodes(output, fragment, idGenerator);
  }
  for (PlanFragment fragment : fragments) {
    fragment.getRoot().accept(new EdgePrinter(output, fragmentsById, idGenerator), null);
  }
  output.append("}\n");
  return output.toString();
}

代码示例来源:origin: prestodb/presto

private static PlanMatchPattern values(List<String> aliases, Optional<List<List<Expression>>> expectedRows)
{
  return values(
      Maps.uniqueIndex(IntStream.range(0, aliases.size()).boxed().iterator(), aliases::get),
      Optional.of(aliases.size()),
      expectedRows);
}

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

/** Null values are not allowed. */
public void testUniqueIndexNullValue() {
 List<String> listWithNull = Lists.newArrayList((String) null);
 try {
  Maps.uniqueIndex(listWithNull, Functions.constant(1));
  fail();
 } catch (NullPointerException expected) {
 }
}

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

/** Null keys aren't allowed either. */
public void testUniqueIndexNullKey() {
 List<String> oneStringList = Lists.newArrayList("foo");
 try {
  Maps.uniqueIndex(oneStringList, Functions.constant(null));
  fail();
 } catch (NullPointerException expected) {
 }
}

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

/** Can't create the map if more than one value maps to the same key. */
public void testUniqueIndexDuplicates() {
 try {
  Map<Integer, String> unused =
    Maps.uniqueIndex(ImmutableSet.of("one", "uno"), Functions.constant(1));
  fail();
 } catch (IllegalArgumentException expected) {
  assertThat(expected.getMessage()).contains("Multimaps.index");
 }
}

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

public void testUniqueIndexIterable() {
 ImmutableMap<Integer, String> outputMap =
   Maps.uniqueIndex(
     new Iterable<String>() {
      @Override
      public Iterator<String> iterator() {
       return INT_TO_STRING_MAP.values().iterator();
      }
     },
     Functions.forMap(INT_TO_STRING_MAP.inverse()));
 assertEquals(INT_TO_STRING_MAP, outputMap);
}

代码示例来源:origin: prestodb/presto

@Test
public void testGetTableSchemaOffline()
{
  try (Transaction transaction = newTransaction()) {
    ConnectorMetadata metadata = transaction.getMetadata();
    Map<SchemaTableName, List<ColumnMetadata>> columns = metadata.listTableColumns(newSession(), tableOffline.toSchemaTablePrefix());
    assertEquals(columns.size(), 1);
    Map<String, ColumnMetadata> map = uniqueIndex(getOnlyElement(columns.values()), ColumnMetadata::getName);
    assertPrimitiveField(map, "t_string", createUnboundedVarcharType(), false);
  }
}

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

public void testUniqueIndexCollection() {
 ImmutableMap<Integer, String> outputMap =
   Maps.uniqueIndex(INT_TO_STRING_MAP.values(), Functions.forMap(INT_TO_STRING_MAP.inverse()));
 assertEquals(INT_TO_STRING_MAP, outputMap);
}

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

public void testUniqueIndexIterator() {
 ImmutableMap<Integer, String> outputMap =
   Maps.uniqueIndex(
     INT_TO_STRING_MAP.values().iterator(), Functions.forMap(INT_TO_STRING_MAP.inverse()));
 assertEquals(INT_TO_STRING_MAP, outputMap);
}

代码示例来源:origin: prestodb/presto

@Test
public void testGetTableSchemaOfflinePartition()
{
  try (Transaction transaction = newTransaction()) {
    ConnectorMetadata metadata = transaction.getMetadata();
    ConnectorTableHandle tableHandle = getTableHandle(metadata, tableOfflinePartition);
    ConnectorTableMetadata tableMetadata = metadata.getTableMetadata(newSession(), tableHandle);
    Map<String, ColumnMetadata> map = uniqueIndex(tableMetadata.getColumns(), ColumnMetadata::getName);
    assertPrimitiveField(map, "t_string", createUnboundedVarcharType(), false);
  }
}

代码示例来源:origin: prestodb/presto

@Test
public void testGetTableSchemaNotReadablePartition()
{
  try (Transaction transaction = newTransaction()) {
    ConnectorMetadata metadata = transaction.getMetadata();
    ConnectorTableHandle tableHandle = getTableHandle(metadata, tableNotReadable);
    ConnectorTableMetadata tableMetadata = metadata.getTableMetadata(newSession(), tableHandle);
    Map<String, ColumnMetadata> map = uniqueIndex(tableMetadata.getColumns(), ColumnMetadata::getName);
    assertPrimitiveField(map, "t_string", createUnboundedVarcharType(), false);
  }
}

代码示例来源:origin: prestodb/presto

@Test
public void testGetTableSchemaUnpartitioned()
{
  try (Transaction transaction = newTransaction()) {
    ConnectorMetadata metadata = transaction.getMetadata();
    ConnectorTableHandle tableHandle = getTableHandle(metadata, tableUnpartitioned);
    ConnectorTableMetadata tableMetadata = metadata.getTableMetadata(newSession(), tableHandle);
    Map<String, ColumnMetadata> map = uniqueIndex(tableMetadata.getColumns(), ColumnMetadata::getName);
    assertPrimitiveField(map, "t_string", createUnboundedVarcharType(), false);
    assertPrimitiveField(map, "t_tinyint", TINYINT, false);
  }
}

相关文章