io.vavr.collection.Map.put()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(9.8k)|赞(0)|评价(0)|浏览(195)

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

Map.put介绍

[英]Convenience method for put(entry._1, entry._2).
[中]方便的看跌期权方法(记项1,记项2)。

代码示例

代码示例来源:origin: vavr-io/vavr

@SuppressWarnings("unchecked")
static <K, V, M extends Map<K, V>> M put(M map, Tuple2<? extends K, ? extends V> entry) {
  Objects.requireNonNull(entry, "entry is null");
  return (M) map.put(entry._1, entry._2);
}

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

private static Map<String, Object> storeSimpleFieldIntoMap(Object object, Class<?> type, String keyName,
   Map<String, Object> values) {
  // No object to write
  if (object == null) {
   return values;
  }

  if (Enum.class.isAssignableFrom(type)) {
   object = object.toString();
  }

  return values.put(keyName, object);
 }
}

代码示例来源:origin: vavr-io/vavr

@SuppressWarnings("unchecked")
static <K, V, M extends Map<K, V>> M replaceValue(M map, K key, V value) {
  return map.containsKey(key) ? (M) map.put(key, value) : map;
}

代码示例来源:origin: vavr-io/vavr

@SuppressWarnings("unchecked")
static <K, V, U extends V, M extends Map<K, V>> M put(M map, K key, U value,
    BiFunction<? super V, ? super U, ? extends V> merge) {
  Objects.requireNonNull(merge, "the merge function is null");
  final Option<V> currentValue = map.get(key);
  if (currentValue.isEmpty()) {
    return (M) map.put(key, value);
  } else {
    return (M) map.put(key, merge.apply(currentValue.get(), value));
  }
}

代码示例来源:origin: vavr-io/vavr

@SuppressWarnings("unchecked")
static <T, K, V, M extends Map<K, V>> M ofStream(M map, java.util.stream.Stream<? extends T> stream,
    Function<? super T, ? extends K> keyMapper,
    Function<? super T, ? extends V> valueMapper) {
  Objects.requireNonNull(stream, "stream is null");
  Objects.requireNonNull(keyMapper, "keyMapper is null");
  Objects.requireNonNull(valueMapper, "valueMapper is null");
  return Stream.ofAll(stream).foldLeft(map, (m, el) -> (M) m.put(keyMapper.apply(el), valueMapper.apply(el)));
}

代码示例来源:origin: vavr-io/vavr

static <T, C, R extends Iterable<T>> Map<C, R> groupBy(Traversable<T> source, Function<? super T, ? extends C> classifier, Function<? super Iterable<T>, R> mapper) {
  Objects.requireNonNull(classifier, "classifier is null");
  Objects.requireNonNull(mapper, "mapper is null");
  Map<C, R> results = LinkedHashMap.empty();
  for (java.util.Map.Entry<? extends C, Collection<T>> entry : groupBy(source, classifier)) {
    results = results.put(entry.getKey(), mapper.apply(entry.getValue()));
  }
  return results;
}

代码示例来源:origin: vavr-io/vavr

@SuppressWarnings("unchecked")
static <T, K, V, M extends Map<K, V>> M ofStream(M map, java.util.stream.Stream<? extends T> stream,
    Function<? super T, Tuple2<? extends K, ? extends V>> entryMapper) {
  Objects.requireNonNull(stream, "stream is null");
  Objects.requireNonNull(entryMapper, "entryMapper is null");
  return Stream.ofAll(stream).foldLeft(map, (m, el) -> (M) m.put(entryMapper.apply(el)));
}

代码示例来源:origin: vavr-io/vavr

@SuppressWarnings("unchecked")
static <K, V, M extends Map<K, V>> M replace(M map, Tuple2<K, V> currentElement, Tuple2<K, V> newElement) {
  Objects.requireNonNull(currentElement, "currentElement is null");
  Objects.requireNonNull(newElement, "newElement is null");
  return (M) (map.containsKey(currentElement._1) ? map.remove(currentElement._1).put(newElement) : map);
}

代码示例来源:origin: vavr-io/vavr

@SuppressWarnings("unchecked")
static <K, V, K2, U extends Map<K2, V>> U mapKeys(Map<K, V> source, U zero, Function<? super K, ? extends K2> keyMapper, BiFunction<? super V, ? super V, ? extends V> valueMerge) {
  Objects.requireNonNull(zero, "zero is null");
  Objects.requireNonNull(keyMapper, "keyMapper is null");
  Objects.requireNonNull(valueMerge, "valueMerge is null");
  return source.foldLeft(zero, (acc, entry) -> {
    final K2 k2 = keyMapper.apply(entry._1);
    final V v2 = entry._2;
    final Option<V> v1 = acc.get(k2);
    final V v = v1.isDefined() ? valueMerge.apply(v1.get(), v2) : v2;
    return (U) acc.put(k2, v);
  });
}

代码示例来源:origin: vavr-io/vavr

@SuppressWarnings("unchecked")
static <K, V, M extends Map<K, V>> M replace(M map, K key, V oldValue, V newValue) {
  return map.contains(Tuple(key, oldValue)) ? (M) map.put(key, newValue) : map;
}

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

@Override
 public Map<String, ?> unapply(Map<String, ?> childKeys, String pathPrefix) {
  String tableNameWithSuffix = childKeys.filterKeys(key -> key.startsWith("table.name")).head()._2.toString();

  return ((Map<String, Object>) childKeys).filterKeys(key -> !key.startsWith("table.name"))
    .put("table.name", TableNameBuilder.extractRawTableName(tableNameWithSuffix));
 }
}

代码示例来源:origin: vavr-io/vavr

@SuppressWarnings("unchecked")
static <K, V, M extends Map<K, V>> Tuple2<V, M> computeIfAbsent(M map, K key, Function<? super K, ? extends V> mappingFunction) {
  Objects.requireNonNull(mappingFunction, "mappingFunction is null");
  final Option<V> value = map.get(key);
  if (value.isDefined()) {
    return Tuple.of(value.get(), map);
  } else {
    final V newValue = mappingFunction.apply(key);
    final M newMap = (M) map.put(key, newValue);
    return Tuple.of(newValue, newMap);
  }
}

代码示例来源:origin: vavr-io/vavr

@SuppressWarnings("unchecked")
private <K2, V2> Multimap<K2, V2> createFromEntries(Iterable<? extends Tuple2<? extends K2, ? extends V2>> entries) {
  Map<K2, Traversable<V2>> back = emptyMapSupplier();
  for (Tuple2<? extends K2, ? extends V2> entry : entries) {
    if (back.containsKey(entry._1)) {
      back = back.put(entry._1, containerType.add(back.get(entry._1).get(), entry._2));
    } else {
      back = back.put(entry._1, containerType.add(emptyContainer.get(), entry._2));
    }
  }
  return createFromMap(back);
}

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

@Override
public java.util.List<T> handleChildKeys(Map<String, ?> childKeys, String pathPrefix) {
 Seq<T> valueList = childKeys.groupBy(tuple2 -> tuple2._1.split("\\.", 2)[0]).flatMap(tuple2 -> {
  String key = tuple2._1;
  Map<String, ?> values = tuple2._2;
  // Drop the prefix
  Map<String, Object> valuesWithoutPrefix =
    values.map((configKey, configValue) -> Tuple.of(configKey.substring(key.length() + 1), configValue));
  valuesWithoutPrefix = valuesWithoutPrefix.put("name", key);
  T value;
  try {
   value = Deserializer.deserialize(_type, valuesWithoutPrefix, "");
   return Option.some(value);
  } catch (Exception e) {
   e.printStackTrace();
   return Option.none();
  }
 });
 return valueList.asJava();
}

代码示例来源:origin: vavr-io/vavr

@SuppressWarnings("unchecked")
static <K, V, U extends V, M extends Map<K, V>> M merge(
    M map, OfEntries<K, V, M> ofEntries,
    Map<? extends K, U> that, BiFunction<? super V, ? super U, ? extends V> collisionResolution) {
  Objects.requireNonNull(that, "that is null");
  Objects.requireNonNull(collisionResolution, "collisionResolution is null");
  if (map.isEmpty()) {
    return ofEntries.apply(Map.narrow(that));
  } else if (that.isEmpty()) {
    return map;
  } else {
    return that.foldLeft(map, (result, entry) -> {
      final K key = entry._1;
      final U value = entry._2;
      final V newValue = result.get(key).map(v -> (V) collisionResolution.apply(v, value)).getOrElse(value);
      return (M) result.put(key, newValue);
    });
  }
}

代码示例来源:origin: vavr-io/vavr

@SuppressWarnings("unchecked")
@Override
public M put(K key, V value) {
  final Traversable<V> values = back.get(key).getOrElse((Traversable<V>) emptyContainer.get());
  final Traversable<V> newValues = containerType.add(values, value);
  return (M) (newValues == values ? this : createFromMap(back.put(key, newValues)));
}

代码示例来源:origin: vavr-io/vavr

@SuppressWarnings("unchecked")
static <K, V, M extends Map<K, V>> Tuple2<Option<V>, M> computeIfPresent(M map, K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
  final Option<V> value = map.get(key);
  if (value.isDefined()) {
    final V newValue = remappingFunction.apply(key, value.get());
    final M newMap = (M) map.put(key, newValue);
    return Tuple.of(Option.of(newValue), newMap);
  } else {
    return Tuple.of(Option.none(), map);
  }
}

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

@Override
 public Map<String, ?> unapply(Map<String, ?> config, String keyPrefix) {
  Map<String, ?> tableTypeMap = config.filterKeys(key -> key.startsWith("table.type."));

  java.util.List<String> tableTypes = tableTypeMap.values().map(Object::toString).toSet().toJavaList();

  Map<String, Object> remappedConfig =
    ((Map<String, Object>) config).removeAll(tableTypeMap.keySet()).put("table.types", tableTypes);

  return remappedConfig;
 }
}

代码示例来源:origin: vavr-io/vavr

@SuppressWarnings("unchecked")
@Override
public <K2 extends K, V2 extends V> M merge(Multimap<K2, V2> that, BiFunction<Traversable<V>, Traversable<V2>, Traversable<V>> collisionResolution) {
  Objects.requireNonNull(that, "that is null");
  Objects.requireNonNull(collisionResolution, "collisionResolution is null");
  if (isEmpty()) {
    return (M) createFromEntries(that);
  } else if (that.isEmpty()) {
    return (M) this;
  } else {
    final Map<K, Traversable<V>> result = that.keySet().foldLeft(this.back, (map, key) -> {
      final Traversable<V> thisValues = map.get(key).getOrElse((Traversable<V>) emptyContainer.get());
      final Traversable<V2> thatValues = that.get(key).get();
      final Traversable<V> newValues = collisionResolution.apply(thisValues, thatValues);
      return map.put(key, newValues);
    });
    return (M) createFromMap(result);
  }
}

代码示例来源:origin: vavr-io/vavr

@SuppressWarnings("unchecked")
@Override
public M remove(K key, V value) {
  final Traversable<V> values = back.get(key).getOrElse((Traversable<V>) emptyContainer.get());
  final Traversable<V> newValues = containerType.remove(values, value);
  if (newValues == values) {
    return (M) this;
  } else if (newValues.isEmpty()) {
    return (M) createFromMap(back.remove(key));
  } else {
    return (M) createFromMap(back.put(key, newValues));
  }
}

相关文章