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

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

本文整理了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

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

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

  1. /**
  2. * Alias for {@link HashMap#ofEntries(Tuple2...)}
  3. *
  4. * @param <K> The key type.
  5. * @param <V> The value type.
  6. * @param entries Map entries.
  7. * @return A new {@link HashMap} instance containing the given entries
  8. * @deprecated Will be removed in a future version.
  9. */
  10. @Deprecated
  11. @SuppressWarnings("varargs")
  12. @SafeVarargs
  13. public static <K, V> Map<K, V> Map(Tuple2<? extends K, ? extends V>... entries) {
  14. return HashMap.ofEntries(entries);
  15. }

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

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

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

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

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

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

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

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

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

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

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

  1. /**
  2. * Creates a LinkedHashMap of the given entries.
  3. *
  4. * @param entries Map entries
  5. * @param <K> The key type
  6. * @param <V> The value type
  7. * @return A new Map containing the given entries
  8. */
  9. @SuppressWarnings("unchecked")
  10. public static <K, V> LinkedHashMap<K, V> ofEntries(Tuple2<? extends K, ? extends V>... entries) {
  11. final HashMap<K, V> map = HashMap.ofEntries(entries);
  12. final Queue<Tuple2<K, V>> list = Queue.of((Tuple2<K, V>[]) entries);
  13. return wrapNonUnique(list, map);
  14. }

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

  1. /**
  2. * Returns a {@link java.util.stream.Collector} which may be used in conjunction with
  3. * {@link java.util.stream.Stream#collect(java.util.stream.Collector)} to obtain a {@link HashMap}.
  4. *
  5. * @param keyMapper The key mapper
  6. * @param valueMapper The value mapper
  7. * @param <K> The key type
  8. * @param <V> The value type
  9. * @param <T> Initial {@link java.util.stream.Stream} elements type
  10. * @return A {@link HashMap} Collector.
  11. */
  12. public static <K, V, T> Collector<T, ArrayList<T>, HashMap<K, V>> collector(
  13. Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) {
  14. Objects.requireNonNull(keyMapper, "keyMapper is null");
  15. Objects.requireNonNull(valueMapper, "valueMapper is null");
  16. final Supplier<ArrayList<T>> supplier = ArrayList::new;
  17. final BiConsumer<ArrayList<T>, T> accumulator = ArrayList::add;
  18. final BinaryOperator<ArrayList<T>> combiner = (left, right) -> {
  19. left.addAll(right);
  20. return left;
  21. };
  22. final Function<ArrayList<T>, HashMap<K, V>> finisher = arr -> HashMap.ofEntries(Iterator.ofAll(arr)
  23. .map(t -> Tuple.of(keyMapper.apply(t), valueMapper.apply(t))));
  24. return Collector.of(supplier, accumulator, combiner, finisher);
  25. }

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

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

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

  1. /**
  2. * Alias for {@link HashMap#ofEntries(Tuple2...)}
  3. *
  4. * @param <K> The key type.
  5. * @param <V> The value type.
  6. * @param entries Map entries.
  7. * @return A new {@link HashMap} instance containing the given entries
  8. * @deprecated Will be removed in a future version.
  9. */
  10. @Deprecated
  11. @SuppressWarnings("varargs")
  12. @SafeVarargs
  13. public static <K, V> Map<K, V> Map(Tuple2<? extends K, ? extends V>... entries) {
  14. return HashMap.ofEntries(entries);
  15. }

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

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

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

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

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

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

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

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

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

  1. /**
  2. * Creates a LinkedHashMap of the given entries.
  3. *
  4. * @param entries Map entries
  5. * @param <K> The key type
  6. * @param <V> The value type
  7. * @return A new Map containing the given entries
  8. */
  9. @SuppressWarnings("unchecked")
  10. public static <K, V> LinkedHashMap<K, V> ofEntries(Tuple2<? extends K, ? extends V>... entries) {
  11. final HashMap<K, V> map = HashMap.ofEntries(entries);
  12. final Queue<Tuple2<K, V>> list = Queue.of((Tuple2<K, V>[]) entries);
  13. return wrapNonUnique(list, map);
  14. }

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

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

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

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

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

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

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

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

相关文章