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

x33g5p2x  于2022-01-20 转载在 其他  
字(11.0k)|赞(0)|评价(0)|浏览(186)

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

HashMap.put介绍

暂无

代码示例

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

/**
 * Creates a HashMap of the given list of key-value pairs.
 *
 * @param k1  a key for the map
 * @param v1  the value for k1
 * @param k2  a key for the map
 * @param v2  the value for k2
 * @param <K> The key type
 * @param <V> The value type
 * @return A new Map containing the given entries
 */
public static <K, V> HashMap<K, V> of(K k1, V v1, K k2, V v2) {
  return of(k1, v1).put(k2, v2);
}

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

/**
 * Creates a HashMap of the given list of key-value pairs.
 *
 * @param k1  a key for the map
 * @param v1  the value for k1
 * @param k2  a key for the map
 * @param v2  the value for k2
 * @param k3  a key for the map
 * @param v3  the value for k3
 * @param <K> The key type
 * @param <V> The value type
 * @return A new Map containing the given entries
 */
public static <K, V> HashMap<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3) {
  return of(k1, v1, k2, v2).put(k3, v3);
}

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

/**
 * Creates a HashMap of the given list of key-value pairs.
 *
 * @param <K> The key type
 * @param <V> The value type
 * @param k1  a key for the map
 * @param v1  the value for k1
 * @param k2  a key for the map
 * @param v2  the value for k2
 * @param k3  a key for the map
 * @param v3  the value for k3
 * @param k4  a key for the map
 * @param v4  the value for k4
 * @return A new Map containing the given entries
 */
public static <K, V> HashMap<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {
  return of(k1, v1, k2, v2, k3, v3).put(k4, v4);
}

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

@Override
public <K2, V2> HashMap<K2, V2> flatMap(BiFunction<? super K, ? super V, ? extends Iterable<Tuple2<K2, V2>>> mapper) {
  Objects.requireNonNull(mapper, "mapper is null");
  return foldLeft(HashMap.<K2, V2> empty(), (acc, entry) -> {
    for (Tuple2<? extends K2, ? extends V2> mappedEntry : mapper.apply(entry._1, entry._2)) {
      acc = acc.put(mappedEntry);
    }
    return acc;
  });
}

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

/**
 * Creates a HashMap of the given list of key-value pairs.
 *
 * @param k1  a key for the map
 * @param v1  the value for k1
 * @param k2  a key for the map
 * @param v2  the value for k2
 * @param k3  a key for the map
 * @param v3  the value for k3
 * @param k4  a key for the map
 * @param v4  the value for k4
 * @param k5  a key for the map
 * @param v5  the value for k5
 * @param <K> The key type
 * @param <V> The value type
 * @return A new Map containing the given entries
 */
public static <K, V> HashMap<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) {
  return of(k1, v1, k2, v2, k3, v3, k4, v4).put(k5, v5);
}

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

/**
 * Creates a HashMap of the given list of key-value pairs.
 *
 * @param k1  a key for the map
 * @param v1  the value for k1
 * @param k2  a key for the map
 * @param v2  the value for k2
 * @param k3  a key for the map
 * @param v3  the value for k3
 * @param k4  a key for the map
 * @param v4  the value for k4
 * @param k5  a key for the map
 * @param v5  the value for k5
 * @param k6  a key for the map
 * @param v6  the value for k6
 * @param <K> The key type
 * @param <V> The value type
 * @return A new Map containing the given entries
 */
public static <K, V> HashMap<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6) {
  return of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5).put(k6, v6);
}

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

/**
 * Creates a HashMap of the given list of key-value pairs.
 *
 * @param k1  a key for the map
 * @param v1  the value for k1
 * @param k2  a key for the map
 * @param v2  the value for k2
 * @param k3  a key for the map
 * @param v3  the value for k3
 * @param k4  a key for the map
 * @param v4  the value for k4
 * @param k5  a key for the map
 * @param v5  the value for k5
 * @param k6  a key for the map
 * @param v6  the value for k6
 * @param k7  a key for the map
 * @param v7  the value for k7
 * @param <K> The key type
 * @param <V> The value type
 * @return A new Map containing the given entries
 */
public static <K, V> HashMap<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7) {
  return of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6).put(k7, v7);
}

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

/**
 * Creates a HashMap of the given list of key-value pairs.
 *
 * @param k1  a key for the map
 * @param v1  the value for k1
 * @param k2  a key for the map
 * @param v2  the value for k2
 * @param k3  a key for the map
 * @param v3  the value for k3
 * @param k4  a key for the map
 * @param v4  the value for k4
 * @param k5  a key for the map
 * @param v5  the value for k5
 * @param k6  a key for the map
 * @param v6  the value for k6
 * @param k7  a key for the map
 * @param v7  the value for k7
 * @param k8  a key for the map
 * @param v8  the value for k8
 * @param <K> The key type
 * @param <V> The value type
 * @return A new Map containing the given entries
 */
public static <K, V> HashMap<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8) {
  return of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7).put(k8, v8);
}

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

/**
 * Creates a HashMap of the given list of key-value pairs.
 *
 * @param k1  a key for the map
 * @param v1  the value for k1
 * @param k2  a key for the map
 * @param v2  the value for k2
 * @param k3  a key for the map
 * @param v3  the value for k3
 * @param k4  a key for the map
 * @param v4  the value for k4
 * @param k5  a key for the map
 * @param v5  the value for k5
 * @param k6  a key for the map
 * @param v6  the value for k6
 * @param k7  a key for the map
 * @param v7  the value for k7
 * @param k8  a key for the map
 * @param v8  the value for k8
 * @param k9  a key for the map
 * @param v9  the value for k9
 * @param k10 a key for the map
 * @param v10 the value for k10
 * @param <K> The key type
 * @param <V> The value type
 * @return A new Map containing the given entries
 */
public static <K, V> HashMap<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10) {
  return of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8, k9, v9).put(k10, v10);
}

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

/**
 * Creates a HashMap of the given list of key-value pairs.
 *
 * @param k1  a key for the map
 * @param v1  the value for k1
 * @param k2  a key for the map
 * @param v2  the value for k2
 * @param k3  a key for the map
 * @param v3  the value for k3
 * @param k4  a key for the map
 * @param v4  the value for k4
 * @param k5  a key for the map
 * @param v5  the value for k5
 * @param k6  a key for the map
 * @param v6  the value for k6
 * @param k7  a key for the map
 * @param v7  the value for k7
 * @param k8  a key for the map
 * @param v8  the value for k8
 * @param k9  a key for the map
 * @param v9  the value for k9
 * @param <K> The key type
 * @param <V> The value type
 * @return A new Map containing the given entries
 */
public static <K, V> HashMap<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9) {
  return of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8).put(k9, v9);
}

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

@Override
public <K2, V2> HashMap<K2, V2> map(BiFunction<? super K, ? super V, Tuple2<K2, V2>> mapper) {
  Objects.requireNonNull(mapper, "mapper is null");
  return foldLeft(HashMap.empty(), (acc, entry) -> acc.put(entry.map(mapper)));
}

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

/**
 * Creates a LinkedHashMap of the given entries.
 *
 * @param entries Map entries
 * @param <K>     The key type
 * @param <V>     The value type
 * @return A new Map containing the given entries
 */
@SuppressWarnings("unchecked")
public static <K, V> LinkedHashMap<K, V> ofEntries(java.util.Map.Entry<? extends K, ? extends V>... entries) {
  HashMap<K, V> map = HashMap.empty();
  Queue<Tuple2<K, V>> list = Queue.empty();
  for (java.util.Map.Entry<? extends K, ? extends V> entry : entries) {
    final Tuple2<K, V> tuple = Tuple.of(entry.getKey(), entry.getValue());
    map = map.put(tuple);
    list = list.append(tuple);
  }
  return wrapNonUnique(list, map);
}

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

/**
 * Creates a LinkedHashMap of the given entries.
 *
 * @param entries Map entries
 * @param <K>     The key type
 * @param <V>     The value type
 * @return A new Map containing the given entries
 */
@SuppressWarnings("unchecked")
public static <K, V> LinkedHashMap<K, V> ofEntries(Iterable<? extends Tuple2<? extends K, ? extends V>> entries) {
  Objects.requireNonNull(entries, "entries is null");
  if (entries instanceof LinkedHashMap) {
    return (LinkedHashMap<K, V>) entries;
  } else {
    HashMap<K, V> map = HashMap.empty();
    Queue<Tuple2<K, V>> list = Queue.empty();
    for (Tuple2<? extends K, ? extends V> entry : entries) {
      map = map.put(entry);
      list = list.append((Tuple2<K, V>) entry);
    }
    return wrapNonUnique(list, map);
  }
}

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

@Override
public LinkedHashMap<K, V> replace(Tuple2<K, V> currentElement, Tuple2<K, V> newElement) {
  Objects.requireNonNull(currentElement, "currentElement is null");
  Objects.requireNonNull(newElement, "newElement is null");
  // We replace the whole element, i.e. key and value have to be present.
  if (!Objects.equals(currentElement, newElement) && contains(currentElement)) {
    Queue<Tuple2<K, V>> newList = list;
    HashMap<K, V> newMap = map;
    final K currentKey = currentElement._1;
    final K newKey = newElement._1;
    // If current key and new key are equal, the element will be automatically replaced,
    // otherwise we need to remove the pair (newKey, ?) from the list manually.
    if (!Objects.equals(currentKey, newKey)) {
      final Option<V> value = newMap.get(newKey);
      if (value.isDefined()) {
        newList = newList.remove(Tuple.of(newKey, value.get()));
      }
    }
    newList = newList.replace(currentElement, newElement);
    newMap = newMap.remove(currentKey).put(newElement);
    return wrap(newList, newMap);
  } else {
    return this;
  }
}

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

/**
 * Associates the specified value with the specified key in this map.
 * If the map previously contained a mapping for the key, the old value is
 * replaced by the specified value.
 * <p>
 * Note that this method has a worst-case linear complexity.
 *
 * @param key   key with which the specified value is to be associated
 * @param value value to be associated with the specified key
 * @return A new Map containing these elements and that entry.
 */
@Override
public LinkedHashMap<K, V> put(K key, V value) {
  final Queue<Tuple2<K, V>> newList;
  final Option<V> currentEntry = get(key);
  if (currentEntry.isDefined()) {
    newList = list.replace(Tuple.of(key, currentEntry.get()), Tuple.of(key, value));
  } else {
    newList = list.append(Tuple.of(key, value));
  }
  final HashMap<K, V> newMap = map.put(key, value);
  return wrap(newList, newMap);
}

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

/**
 * Creates a HashMap of the given list of key-value pairs.
 *
 * @param k1  a key for the map
 * @param v1  the value for k1
 * @param k2  a key for the map
 * @param v2  the value for k2
 * @param <K> The key type
 * @param <V> The value type
 * @return A new Map containing the given entries
 */
public static <K, V> HashMap<K, V> of(K k1, V v1, K k2, V v2) {
  return of(k1, v1).put(k2, v2);
}

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

@Override
public <K2, V2> HashMap<K2, V2> flatMap(BiFunction<? super K, ? super V, ? extends Iterable<Tuple2<K2, V2>>> mapper) {
  Objects.requireNonNull(mapper, "mapper is null");
  return foldLeft(HashMap.<K2, V2> empty(), (acc, entry) -> {
    for (Tuple2<? extends K2, ? extends V2> mappedEntry : mapper.apply(entry._1, entry._2)) {
      acc = acc.put(mappedEntry);
    }
    return acc;
  });
}

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

@Override
public <K2, V2> HashMap<K2, V2> map(BiFunction<? super K, ? super V, Tuple2<K2, V2>> mapper) {
  Objects.requireNonNull(mapper, "mapper is null");
  return foldLeft(HashMap.empty(), (acc, entry) -> acc.put(entry.map(mapper)));
}

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

@Test
public void test2() throws IOException {
  Object src = List.of(HashMap.empty().put("key1", 1), HashMap.empty().put("key2", 2));
  String json = mapper().writer().writeValueAsString(src);
  Assert.assertEquals(mapper().readValue(json, new TypeReference<List<HashMap<?,?>>>() {}), src);
}

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

@Test
public void test1() throws IOException {
  Object src = HashMap.empty().put("key1", List.of(1, 2)).put("key2", List.of(3, 4));
  String json = mapper().writer().writeValueAsString(src);
  Assert.assertEquals(mapper().readValue(json, new TypeReference<HashMap<?,List<?>>>() {}), src);
}

相关文章