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

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

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

HashMap.ofEntries介绍

[英]Creates a HashMap of the given entries.
[中]创建给定项的哈希映射。

代码示例

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

private HashMap<K, V> createFromEntries(Iterable<Tuple2<K, V>> tuples) {
    return HashMap.ofEntries(tuples);
  }
}

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

/**
 * Alias for {@link HashMap#ofEntries(Tuple2...)}
 *
 * @param <K>     The key type.
 * @param <V>     The value type.
 * @param entries Map entries.
 * @return A new {@link HashMap} instance containing the given entries
 * @deprecated Will be removed in a future version.
 */
@Deprecated
@SuppressWarnings("varargs")
@SafeVarargs
public static <K, V> Map<K, V> Map(Tuple2<? extends K, ? extends V>... entries) {
  return HashMap.ofEntries(entries);
}

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

@Override
public HashMap<K, V> orElse(Iterable<? extends Tuple2<K, V>> other) {
  return isEmpty() ? ofEntries(other) : this;
}

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

@Override
public HashMap<K, V> orElse(Supplier<? extends Iterable<? extends Tuple2<K, V>>> supplier) {
  return isEmpty() ? ofEntries(supplier.get()) : this;
}

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

/**
 * Returns a HashMap containing tuples returned by {@code n} calls to a given Supplier {@code s}.
 *
 * @param <K> The key type
 * @param <V> The value type
 * @param n   The number of elements in the HashMap
 * @param s   The Supplier computing element values
 * @return An HashMap of size {@code n}, where each element contains the result supplied by {@code s}.
 * @throws NullPointerException if {@code s} is null
 */
@SuppressWarnings("unchecked")
public static <K, V> HashMap<K, V> fill(int n, Supplier<? extends Tuple2<? extends K, ? extends V>> s) {
  Objects.requireNonNull(s, "s is null");
  return ofEntries(Collections.fill(n, (Supplier<? extends Tuple2<K, V>>) s));
}

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

/**
 * Returns an HashMap containing {@code n} values of a given Function {@code f}
 * over a range of integer values from 0 to {@code n - 1}.
 *
 * @param <K> The key type
 * @param <V> The value type
 * @param n   The number of elements in the HashMap
 * @param f   The Function computing element values
 * @return An HashMap consisting of elements {@code f(0),f(1), ..., f(n - 1)}
 * @throws NullPointerException if {@code f} is null
 */
@SuppressWarnings("unchecked")
public static <K, V> HashMap<K, V> tabulate(int n, Function<? super Integer, ? extends Tuple2<? extends K, ? extends V>> f) {
  Objects.requireNonNull(f, "f is null");
  return ofEntries(Collections.tabulate(n, (Function<? super Integer, ? extends Tuple2<K, V>>) f));
}

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

@Override
public <K2, V2> HashMap<K2, V2> bimap(Function<? super K, ? extends K2> keyMapper, Function<? super V, ? extends V2> valueMapper) {
  Objects.requireNonNull(keyMapper, "keyMapper is null");
  Objects.requireNonNull(valueMapper, "valueMapper is null");
  final Iterator<Tuple2<K2, V2>> entries = iterator().map(entry -> Tuple.of(keyMapper.apply(entry._1), valueMapper.apply(entry._2)));
  return HashMap.ofEntries(entries);
}

代码示例来源: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(Tuple2<? extends K, ? extends V>... entries) {
  final HashMap<K, V> map = HashMap.ofEntries(entries);
  final Queue<Tuple2<K, V>> list = Queue.of((Tuple2<K, V>[]) entries);
  return wrapNonUnique(list, map);
}

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

/**
 * Returns a {@link java.util.stream.Collector} which may be used in conjunction with
 * {@link java.util.stream.Stream#collect(java.util.stream.Collector)} to obtain a {@link HashMap}.
 *
 * @param keyMapper The key mapper
 * @param valueMapper The value mapper
 * @param <K> The key type
 * @param <V> The value type
 * @param <T> Initial {@link java.util.stream.Stream} elements type
 * @return A {@link HashMap} Collector.
 */
public static <K, V, T> Collector<T, ArrayList<T>, HashMap<K, V>> collector(
    Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) {
  Objects.requireNonNull(keyMapper, "keyMapper is null");
  Objects.requireNonNull(valueMapper, "valueMapper is null");
  final Supplier<ArrayList<T>> supplier = ArrayList::new;
  final BiConsumer<ArrayList<T>, T> accumulator = ArrayList::add;
  final BinaryOperator<ArrayList<T>> combiner = (left, right) -> {
    left.addAll(right);
    return left;
  };
  final Function<ArrayList<T>, HashMap<K, V>> finisher = arr -> HashMap.ofEntries(Iterator.ofAll(arr)
      .map(t -> Tuple.of(keyMapper.apply(t), valueMapper.apply(t))));
  return Collector.of(supplier, accumulator, combiner, finisher);
}

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

private HashMap<K, V> createFromEntries(Iterable<Tuple2<K, V>> tuples) {
    return HashMap.ofEntries(tuples);
  }
}

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

/**
 * Alias for {@link HashMap#ofEntries(Tuple2...)}
 *
 * @param <K>     The key type.
 * @param <V>     The value type.
 * @param entries Map entries.
 * @return A new {@link HashMap} instance containing the given entries
 * @deprecated Will be removed in a future version.
 */
@Deprecated
@SuppressWarnings("varargs")
@SafeVarargs
public static <K, V> Map<K, V> Map(Tuple2<? extends K, ? extends V>... entries) {
  return HashMap.ofEntries(entries);
}

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

@Override
public HashMap<K, V> orElse(Supplier<? extends Iterable<? extends Tuple2<K, V>>> supplier) {
  return isEmpty() ? ofEntries(supplier.get()) : this;
}

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

@Override
public HashMap<K, V> orElse(Iterable<? extends Tuple2<K, V>> other) {
  return isEmpty() ? ofEntries(other) : this;
}

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

/**
 * Returns a HashMap containing tuples returned by {@code n} calls to a given Supplier {@code s}.
 *
 * @param <K> The key type
 * @param <V> The value type
 * @param n   The number of elements in the HashMap
 * @param s   The Supplier computing element values
 * @return An HashMap of size {@code n}, where each element contains the result supplied by {@code s}.
 * @throws NullPointerException if {@code s} is null
 */
@SuppressWarnings("unchecked")
public static <K, V> HashMap<K, V> fill(int n, Supplier<? extends Tuple2<? extends K, ? extends V>> s) {
  Objects.requireNonNull(s, "s is null");
  return ofEntries(Collections.fill(n, (Supplier<? extends Tuple2<K, V>>) s));
}

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

@Override
public <K2, V2> HashMap<K2, V2> bimap(Function<? super K, ? extends K2> keyMapper, Function<? super V, ? extends V2> valueMapper) {
  Objects.requireNonNull(keyMapper, "keyMapper is null");
  Objects.requireNonNull(valueMapper, "valueMapper is null");
  final Iterator<Tuple2<K2, V2>> entries = iterator().map(entry -> Tuple.of(keyMapper.apply(entry._1), valueMapper.apply(entry._2)));
  return HashMap.ofEntries(entries);
}

代码示例来源:origin: io.vavr/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(Tuple2<? extends K, ? extends V>... entries) {
  final HashMap<K, V> map = HashMap.ofEntries(entries);
  final Queue<Tuple2<K, V>> list = Queue.of((Tuple2<K, V>[]) entries);
  return wrapNonUnique(list, map);
}

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

@Test
public void testHashMapOfString() throws Exception {
  Integer src00 = 1;
  String src01 = "A";
  Tuple2<Integer, String> src0 = Tuple.of(src00, src01);
  HashMap<Integer, String> src = HashMap.ofEntries(src0);
  String json = MAPPER.writeValueAsString(new ParameterizedHashMapPojo<>(src));
  Assert.assertEquals(json, "{\"value\":{\"1\":\"A\"}}");
  ParameterizedHashMapPojo<java.lang.Integer, java.lang.String> restored = 
      MAPPER.readValue(json, new TypeReference<ParameterizedHashMapPojo<java.lang.Integer, java.lang.String>>(){});
  Assert.assertEquals(src, restored.getValue());
}

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

@Test
public void testHashMapOfString() throws Exception {
  Integer src00 = 1;
  String src01 = "A";
  Tuple2<Integer, String> src0 = Tuple.of(src00, src01);
  HashMap<Integer, String> src = HashMap.ofEntries(src0);
  String json = MAPPER.writeValueAsString(new HashMapOfString().setValue(src));
  Assert.assertEquals(json, "{\"value\":{\"1\":\"A\"}}");
  HashMapOfString restored = MAPPER.readValue(json, HashMapOfString.class);
  Assert.assertEquals(src, restored.getValue());
}

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

@Test
public void testHashMapOfTuple() throws Exception {
  Integer src00 = 1;
  String src010 = "A";
  String src011 = "B";
  Tuple2<String, String> src01 = Tuple.of(src010, src011);
  Tuple2<Integer, Tuple2<String, String>> src0 = Tuple.of(src00, src01);
  HashMap<Integer, Tuple2<String, String>> src = HashMap.ofEntries(src0);
  String json = MAPPER.writeValueAsString(new ParameterizedHashMapPojo<>(src));
  Assert.assertEquals(json, "{\"value\":{\"1\":[\"A\",\"B\"]}}");
  ParameterizedHashMapPojo<java.lang.Integer, io.vavr.Tuple2<java.lang.String, java.lang.String>> restored = 
      MAPPER.readValue(json, new TypeReference<ParameterizedHashMapPojo<java.lang.Integer, io.vavr.Tuple2<java.lang.String, java.lang.String>>>(){});
  Assert.assertEquals(src, restored.getValue());
}

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

@Test
public void testHashMapOfTuple() throws Exception {
  Integer src00 = 1;
  String src010 = "A";
  String src011 = "B";
  Tuple2<String, String> src01 = Tuple.of(src010, src011);
  Tuple2<Integer, Tuple2<String, String>> src0 = Tuple.of(src00, src01);
  HashMap<Integer, Tuple2<String, String>> src = HashMap.ofEntries(src0);
  String json = MAPPER.writeValueAsString(new HashMapOfTuple().setValue(src));
  Assert.assertEquals(json, "{\"value\":{\"1\":[\"A\",\"B\"]}}");
  HashMapOfTuple restored = MAPPER.readValue(json, HashMapOfTuple.class);
  Assert.assertEquals(src, restored.getValue());
}

相关文章