private HashMap<K, V> createFromEntries(Iterable<Tuple2<K, V>> tuples) { return HashMap.ofEntries(tuples); } }
/** * 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); }
@Override public HashMap<K, V> orElse(Iterable<? extends Tuple2<K, V>> other) { return isEmpty() ? ofEntries(other) : this; }
@Override public HashMap<K, V> orElse(Supplier<? extends Iterable<? extends Tuple2<K, V>>> supplier) { return isEmpty() ? ofEntries(supplier.get()) : this; }
/** * 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)); }
/** * 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)); }
@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); }
/** * 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); }
/** * 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); }
private HashMap<K, V> createFromEntries(Iterable<Tuple2<K, V>> tuples) { return HashMap.ofEntries(tuples); } }
/** * 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); }
@Override public HashMap<K, V> orElse(Supplier<? extends Iterable<? extends Tuple2<K, V>>> supplier) { return isEmpty() ? ofEntries(supplier.get()) : this; }
@Override public HashMap<K, V> orElse(Iterable<? extends Tuple2<K, V>> other) { return isEmpty() ? ofEntries(other) : this; }
/** * 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)); }
@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); }
/** * 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); }
@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()); }
@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()); }
@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()); }
@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()); }