/** * Alias for {@link HashMap#of(Object, Object)} * * @param <K> The key type. * @param <V> The value type. * @param k1 The key * @param v1 The value * @return A new {@link HashMap} instance containing the given entries */ public static <K, V> Map<K, V> Map(K k1, V v1) { return HashMap.of(k1, v1); }
/** * Alias for {@link HashMap#empty()} * * @param <K> The key type. * @param <V> The value type. * @return A singleton instance of empty {@link HashMap} */ public static <K, V> Map<K, V> Map() { return HashMap.empty(); }
@Override public java.util.HashMap<K, V> toJavaMap() { return toJavaMap(java.util.HashMap::new, t -> t); }
/** * 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); }
@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; }); }
@Override public io.vavr.collection.Map<String, ?> unhandleChildKeys(Map<String, Map<String, String>> value, String pathPrefix) { HashMap<String, String> retVal = HashMap.ofAll(value).flatMap((taskKey, configs) -> { if (!configs.isEmpty()) { return HashMap.ofAll(configs).map((configKey, configValue) -> Tuple.of(taskKey + "." + configKey, configValue)); } else { return HashMap.of(taskKey, ""); } }); return retVal; } }
/** * 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); } }
@Test public void testHashMap() throws Exception { HashMap<String, A> src = HashMap.of("a", new B("a", "b")); String json = MAPPER.writeValueAsString(new HashMapPojo().setValue(src)); Assert.assertEquals(json, "{\"value\":{\"a\":{\"ExtFieldsPojoTest$B\":{\"a\":\"a\",\"b\":\"b\"}}}}"); HashMapPojo pojo = MAPPER.readValue(json, HashMapPojo.class); HashMap<String, A> restored = pojo.getValue(); Assert.assertTrue(restored.get("a").get() instanceof B); Assert.assertEquals(restored.get("a").get().a, "a"); Assert.assertEquals(((B) restored.get("a").get()).b, "b"); }
private HashMap<K, V> createFromEntries(Iterable<Tuple2<K, V>> tuples) { return HashMap.ofEntries(tuples); } }
Session initializeSession(final String keyspaceName) { final Session s = this.cluster.connect(); // if the keyspace already exists, just return the session if (this.cluster.getMetadata().getKeyspace(keyspaceName) != null) { return s; } final Configuration configuration = getStorageConfig(); // Setting replication strategy based on value reading from the configuration: either "SimpleStrategy" or "NetworkTopologyStrategy" final Map<String, Object> replication = Match(configuration.get(REPLICATION_STRATEGY)).of( Case($("SimpleStrategy"), strategy -> HashMap.<String, Object> of("class", strategy, "replication_factor", configuration.get(REPLICATION_FACTOR))), Case($("NetworkTopologyStrategy"), strategy -> HashMap.<String, Object> of("class", strategy) .merge(Array.of(configuration.get(REPLICATION_OPTIONS)) .grouped(2) .toMap(array -> Tuple.of(array.get(0), Integer.parseInt(array.get(1))))))) .toJavaMap(); s.execute(createKeyspace(keyspaceName) .ifNotExists() .with() .replication(replication)); return s; }
@Bean InitializingBean initDB() { return () -> HashMap.of(123, "ololo", 456, "trololo") .map((orderNumber, description) -> Tuple.of(BigDecimal.valueOf(orderNumber * System.currentTimeMillis() / 1234567890.987654321), singletonList(description))) .map(t -> Order.of(OrderNumber.of(t._1().intValue()), Price.of(t._1()))) .forEach(orderRepository::save); } }
/** * Creates a HashMap 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> HashMap<K, V> ofEntries(Iterable<? extends Tuple2<? extends K, ? extends V>> entries) { Objects.requireNonNull(entries, "entries is null"); if (entries instanceof HashMap) { return (HashMap<K, V>) entries; } else { HashArrayMappedTrie<K, V> trie = HashArrayMappedTrie.empty(); for (Tuple2<? extends K, ? extends V> entry : entries) { trie = trie.put(entry._1, entry._2); } return trie.isEmpty() ? empty() : wrap(trie); } }
@Test public void testHashMapClass() throws Exception { HashMapClass src = new HashMapClass(HashMap.of(42, new ImplementedClass())); String json = MAPPER.writeValueAsString(src); HashMapClass restored = MAPPER.readValue(json, HashMapClass.class); Assert.assertEquals(restored.value.head()._2.getClass(), ImplementedClass.class); }
@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; } }
@Override public <V2> HashMap<K, V2> mapValues(Function<? super V, ? extends V2> valueMapper) { Objects.requireNonNull(valueMapper, "valueMapper is null"); return map((k, v) -> Tuple.of(k, valueMapper.apply(v))); }
@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); }