本文整理了Java中io.vavr.collection.HashMap
类的一些代码示例,展示了HashMap
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HashMap
类的具体详情如下:
包路径:io.vavr.collection.HashMap
类名称:HashMap
[英]An immutable HashMap implementation based on a Hash array mapped trie (HAMT).
[中]基于Hash array mapped trie (HAMT)的不可变HashMap实现。
代码示例来源:origin: vavr-io/vavr
/**
* 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);
}
代码示例来源:origin: vavr-io/vavr
/**
* 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();
}
代码示例来源:origin: vavr-io/vavr
@Override
public java.util.HashMap<K, V> toJavaMap() {
return toJavaMap(java.util.HashMap::new, t -> t);
}
代码示例来源: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
/**
* 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
@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: daggerok/spring-5-examples
.uri(
"/search/users?q={q}&page={page}&per_page={per_page}",
HashMap.of(
"q", request.pathVariable("username"),
"page", request.queryParam("page").orElse("0"),
"per_page", request.queryParam("size").orElse("3")
).toJavaMap()
代码示例来源:origin: apache/incubator-pinot
@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;
}
}
代码示例来源: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-jackson
@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");
}
代码示例来源:origin: vavr-io/vavr
private HashMap<K, V> createFromEntries(Iterable<Tuple2<K, V>> tuples) {
return HashMap.ofEntries(tuples);
}
}
代码示例来源:origin: org.janusgraph/janusgraph-cql
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;
}
代码示例来源:origin: daggerok/spring-5-examples
@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);
}
}
代码示例来源:origin: vavr-io/vavr
/**
* 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);
}
}
代码示例来源:origin: vavr-io/vavr
private static <K, V> HashMap<K, V> wrap(HashArrayMappedTrie<K, V> trie) {
return trie.isEmpty() ? empty() : new HashMap<>(trie);
}
代码示例来源:origin: vavr-io/vavr-jackson
@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);
}
代码示例来源: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
@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)));
}
代码示例来源: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
@Override
public HashMap<K, V> tail() {
if (trie.isEmpty()) {
throw new UnsupportedOperationException("tail of empty HashMap");
} else {
return remove(head()._1);
}
}
内容来源于网络,如有侵权,请联系作者删除!