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

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

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

Maps.toMap介绍

[英]Returns an immutable map whose keys are the distinct elements of keys and whose value for each key was computed by valueFunction. The map's iteration order is the order of the first appearance of each key in keys.

If keys is a Set, a live view can be obtained instead of a copy using Maps#asMap(Set,Function).
[中]返回一个不可变映射,其键是键的不同元素,每个键的值由valueFunction计算。贴图的迭代顺序是每个关键帧中的关键帧第一次出现的顺序。
如果关键点是一个集合,可以使用Maps#asMap(集合,函数)获取实时视图,而不是副本。

代码示例

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

/**
 * Returns an immutable map whose keys are the distinct elements of {@code keys} and whose value
 * for each key was computed by {@code valueFunction}. The map's iteration order is the order of
 * the first appearance of each key in {@code keys}.
 *
 * <p>When there are multiple instances of a key in {@code keys}, it is unspecified whether {@code
 * valueFunction} will be applied to more than one instance of that key and, if it is, which
 * result will be mapped to that key in the returned map.
 *
 * <p>If {@code keys} is a {@link Set}, a live view can be obtained instead of a copy using {@link
 * Maps#asMap(Set, Function)}.
 *
 * @throws NullPointerException if any element of {@code keys} is {@code null}, or if {@code
 *     valueFunction} produces {@code null} for any key
 * @since 14.0
 */
public static <K, V> ImmutableMap<K, V> toMap(
  Iterable<K> keys, Function<? super K, V> valueFunction) {
 return toMap(keys.iterator(), valueFunction);
}

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

/**
 * Returns an immutable map whose keys are the distinct elements of this {@code FluentIterable}
 * and whose value for each key was computed by {@code valueFunction}. The map's iteration order
 * is the order of the first appearance of each key in this iterable.
 *
 * <p>When there are multiple instances of a key in this iterable, it is unspecified whether
 * {@code valueFunction} will be applied to more than one instance of that key and, if it is,
 * which result will be mapped to that key in the returned map.
 *
 * <p><b>{@code Stream} equivalent:</b> after the next release of Guava, use {@code
 * stream.collect(ImmutableMap.toImmutableMap(k -> k, valueFunction))}. Before then you can use
 * {@code ImmutableMap.copyOf(stream.collect(Collectors.toMap(k -> k, valueFunction)))}, but be
 * aware that this may not preserve the order of entries.
 *
 * @throws NullPointerException if any element of this iterable is {@code null}, or if {@code
 *     valueFunction} produces {@code null} for any key
 * @since 14.0
 */
public final <V> ImmutableMap<E, V> toMap(Function<? super E, V> valueFunction) {
 return Maps.toMap(getDelegate(), valueFunction);
}

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

/**
 * Returns an immutable map whose keys are the distinct elements of this {@code FluentIterable}
 * and whose value for each key was computed by {@code valueFunction}. The map's iteration order
 * is the order of the first appearance of each key in this iterable.
 *
 * <p>When there are multiple instances of a key in this iterable, it is unspecified whether
 * {@code valueFunction} will be applied to more than one instance of that key and, if it is,
 * which result will be mapped to that key in the returned map.
 *
 * <p><b>{@code Stream} equivalent:</b> {@code stream.collect(ImmutableMap.toImmutableMap(k -> k,
 * valueFunction))}.
 *
 * @throws NullPointerException if any element of this iterable is {@code null}, or if {@code
 *     valueFunction} produces {@code null} for any key
 * @since 14.0
 */
public final <V> ImmutableMap<E, V> toMap(Function<? super E, V> valueFunction) {
 return Maps.toMap(getDelegate(), valueFunction);
}

代码示例来源:origin: wildfly/wildfly

/**
 * Returns an immutable map whose keys are the distinct elements of {@code keys} and whose value
 * for each key was computed by {@code valueFunction}. The map's iteration order is the order of
 * the first appearance of each key in {@code keys}.
 *
 * <p>When there are multiple instances of a key in {@code keys}, it is unspecified whether {@code
 * valueFunction} will be applied to more than one instance of that key and, if it is, which
 * result will be mapped to that key in the returned map.
 *
 * <p>If {@code keys} is a {@link Set}, a live view can be obtained instead of a copy using {@link
 * Maps#asMap(Set, Function)}.
 *
 * @throws NullPointerException if any element of {@code keys} is {@code null}, or if {@code
 *     valueFunction} produces {@code null} for any key
 * @since 14.0
 */
public static <K, V> ImmutableMap<K, V> toMap(
  Iterable<K> keys, Function<? super K, V> valueFunction) {
 return toMap(keys.iterator(), valueFunction);
}

代码示例来源:origin: pentaho/pentaho-kettle

public Map<String, String> asMap() {
 checkState( keys != null
  && values != null
  && keys.size() == values.size() );
 return Maps.toMap( keys, key -> values.get( keys.indexOf( key ) ) );
}

代码示例来源:origin: apache/incubator-shardingsphere

private void initForFirstGroupByValue(final QueryResult queryResult, final GroupByValue groupByValue, final Map<GroupByValue, MemoryQueryResultRow> dataMap,
                   final Map<GroupByValue, Map<AggregationSelectItem, AggregationUnit>> aggregationMap) throws SQLException {
  if (!dataMap.containsKey(groupByValue)) {
    dataMap.put(groupByValue, new MemoryQueryResultRow(queryResult));
  }
  if (!aggregationMap.containsKey(groupByValue)) {
    Map<AggregationSelectItem, AggregationUnit> map = Maps.toMap(selectStatement.getAggregationSelectItems(), new Function<AggregationSelectItem, AggregationUnit>() {
      
      @Override
      public AggregationUnit apply(final AggregationSelectItem input) {
        return AggregationUnitFactory.create(input.getType());
      }
    });
    aggregationMap.put(groupByValue, map);
  }
}

代码示例来源:origin: ben-manes/caffeine

@Override
 public Map<Integer, Integer> loadAll(Iterable<? extends Integer> keys) {
  return Maps.toMap(ImmutableSet.copyOf(keys), this::load);
 }
}

代码示例来源:origin: wildfly/wildfly

/**
 * Returns an immutable map whose keys are the distinct elements of this {@code FluentIterable}
 * and whose value for each key was computed by {@code valueFunction}. The map's iteration order
 * is the order of the first appearance of each key in this iterable.
 *
 * <p>When there are multiple instances of a key in this iterable, it is unspecified whether
 * {@code valueFunction} will be applied to more than one instance of that key and, if it is,
 * which result will be mapped to that key in the returned map.
 *
 * <p><b>{@code Stream} equivalent:</b> {@code stream.collect(ImmutableMap.toImmutableMap(k -> k,
 * valueFunction))}.
 *
 * @throws NullPointerException if any element of this iterable is {@code null}, or if {@code
 *     valueFunction} produces {@code null} for any key
 * @since 14.0
 */
public final <V> ImmutableMap<E, V> toMap(Function<? super E, V> valueFunction) {
 return Maps.toMap(getDelegate(), valueFunction);
}

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

@GwtIncompatible // NavigableMap
private static final class NavigableAsMapView<K, V> extends AbstractNavigableMap<K, V> {
 /*
  * Using AbstractNavigableMap is simpler than extending SortedAsMapView and rewriting all the
  * NavigableMap methods.
  */
 private final NavigableSet<K> set;
 private final Function<? super K, V> function;
 NavigableAsMapView(NavigableSet<K> ks, Function<? super K, V> vFunction) {
  this.set = checkNotNull(ks);
  this.function = checkNotNull(vFunction);
 }
 @Override
 public NavigableMap<K, V> subMap(
   K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) {
  return asMap(set.subSet(fromKey, fromInclusive, toKey, toInclusive), function);
 }
 @Override
 public NavigableMap<K, V> headMap(K toKey, boolean inclusive) {
  return asMap(set.headSet(toKey, inclusive), function);
 }
 @Override
 public NavigableMap<K, V> tailMap(K fromKey, boolean inclusive) {
  return asMap(set.tailSet(fromKey, inclusive), function);
 }

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

public void testToMapWithNullKeys() {
 Iterable<String> strings = Arrays.asList("one", null, "three");
 try {
  Maps.toMap(strings, Functions.constant("foo"));
  fail();
 } catch (NullPointerException expected) {
 }
}

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

public void testToMapWithNullValues() {
 Iterable<String> strings = ImmutableList.of("one", "two", "three");
 try {
  Maps.toMap(strings, Functions.constant(null));
  fail();
 } catch (NullPointerException expected) {
 }
}

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

public void testToMapIterator() {
 Iterator<String> strings = ImmutableList.of("one", "two", "three").iterator();
 ImmutableMap<String, Integer> map = Maps.toMap(strings, LENGTH_FUNCTION);
 assertEquals(ImmutableMap.of("one", 3, "two", 3, "three", 5), map);
 assertThat(map.entrySet())
   .containsExactly(mapEntry("one", 3), mapEntry("two", 3), mapEntry("three", 5))
   .inOrder();
}

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

public void testToMapWithDuplicateKeys() {
 Iterable<String> strings = ImmutableList.of("one", "two", "three", "two", "one");
 ImmutableMap<String, Integer> map = Maps.toMap(strings, LENGTH_FUNCTION);
 assertEquals(ImmutableMap.of("one", 3, "two", 3, "three", 5), map);
 assertThat(map.entrySet())
   .containsExactly(mapEntry("one", 3), mapEntry("two", 3), mapEntry("three", 5))
   .inOrder();
}

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

public void testToMap() {
 Iterable<String> strings = ImmutableList.of("one", "two", "three");
 ImmutableMap<String, Integer> map = Maps.toMap(strings, LENGTH_FUNCTION);
 assertEquals(ImmutableMap.of("one", 3, "two", 3, "three", 5), map);
 assertThat(map.entrySet())
   .containsExactly(mapEntry("one", 3), mapEntry("two", 3), mapEntry("three", 5))
   .inOrder();
}

代码示例来源:origin: apache/hive

final Map<TopicPartition, Long> timePartitionsMap = Maps.toMap(fullHouse.keySet(), tp -> timestamp + increment);
try {
 return Maps.toMap(fullHouse.keySet(), tp -> {
  KafkaInputSplit existing = fullHouse.get(tp);
  OffsetAndTimestamp foundOffsetAndTime = offsetAndTimestamp.get(tp);

代码示例来源:origin: apache/incubator-shardingsphere

private boolean aggregateCurrentGroupByRowAndNext() throws SQLException {
  boolean result = false;
  Map<AggregationSelectItem, AggregationUnit> aggregationUnitMap = Maps.toMap(selectStatement.getAggregationSelectItems(), new Function<AggregationSelectItem, AggregationUnit>() {
    
    @Override
    public AggregationUnit apply(final AggregationSelectItem input) {
      return AggregationUnitFactory.create(input.getType());
    }
  });
  while (currentGroupByValues.equals(new GroupByValue(getCurrentQueryResult(), selectStatement.getGroupByItems()).getGroupValues())) {
    aggregate(aggregationUnitMap);
    cacheCurrentRow();
    result = super.next();
    if (!result) {
      break;
    }
  }
  setAggregationValueToCurrentRow(aggregationUnitMap);
  return result;
}

代码示例来源:origin: ben-manes/caffeine

@Test(dataProvider = "caches")
@CacheSpec(requiresWeakOrSoft = true, expireAfterAccess = Expire.DISABLED,
  expireAfterWrite = Expire.DISABLED, maximumSize = Maximum.DISABLED,
  weigher = CacheWeigher.DEFAULT, population = Population.FULL, stats = Stats.ENABLED,
  loader = {Loader.NEGATIVE, Loader.BULK_NEGATIVE}, removalListener = Listener.CONSUMING)
public void getAll(LoadingCache<Integer, Integer> cache, CacheContext context) {
 Set<Integer> keys = context.firstMiddleLastKeys();
 context.clear();
 GcFinalization.awaitFullGc();
 awaitFullCleanup(cache);
 assertThat(cache.getAll(keys), is(Maps.toMap(keys, key -> -key)));
 long count = context.initialSize() - (context.isStrongValues() ? keys.size() : 0);
 assertThat(cache.estimatedSize(), is((long) keys.size()));
 assertThat(cache, hasRemovalNotifications(context, count, RemovalCause.COLLECTED));
 verifyWriter(context, (verifier, writer) -> verifier.deletions(count, RemovalCause.COLLECTED));
}

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

public TopNNode topN(long count, List<Symbol> orderBy, PlanNode source)
{
  return new TopNNode(
      idAllocator.getNextId(),
      source,
      count,
      new OrderingScheme(
          orderBy,
          Maps.toMap(orderBy, Functions.constant(SortOrder.ASC_NULLS_FIRST))),
      TopNNode.Step.SINGLE);
}

代码示例来源:origin: pentaho/pentaho-kettle

@Test
public void testNullMin() throws Exception {
 variables.setVariable( Const.KETTLE_AGGREGATION_MIN_NULL_IS_VALUED, "Y" );
 addColumn( new ValueMetaInteger( "intg" ), null, 0L, 1L, -1L );
 addColumn( new ValueMetaString( "str" ), "A", null, "B", null );
 aggregates = Maps.toMap( ImmutableList.of( "min", "max" ), Functions.forMap( default_aggregates ) );
 RowMetaAndData output = runStep();
 assertThat( output.getInteger( "intg_min" ), nullValue() );
 assertThat( output.getInteger( "intg_max" ), is( 1L ) );
 assertThat( output.getString( "str_min", null ), nullValue() );
 assertThat( output.getString( "str_max", "invalid" ), is( "B" ) );
}

代码示例来源:origin: apache/hive

text("Hello"), // c4:string
newArrayList(intW(0), intW(1), intW(2)), // c5:array<int>
Maps.toMap(
  newArrayList(intW(0), intW(1), intW(2)),
  input -> text("Number " + input)), // c6:map<int,string>
Maps.toMap(
  newArrayList(text("apple"), text("banana"), text("carrot")),
  newArrayList(text("a"), text("b"), text("c")),
  newArrayList(text("A"), text("B"), text("C"))), // c13:array<array<string>>
Maps.toMap(
  newArrayList(intW(0), intW(1), intW(2)),
  x -> Maps.toMap(
    newArrayList(x, intW(x.get() * 2)),
Collections.singletonList(
  newArrayList(
    Maps.toMap(
      newArrayList(text("hello")),
      input -> text(input.toString().toUpperCase())),

相关文章