本文整理了Java中java.util.LinkedHashMap.computeIfAbsent()
方法的一些代码示例,展示了LinkedHashMap.computeIfAbsent()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。LinkedHashMap.computeIfAbsent()
方法的具体详情如下:
包路径:java.util.LinkedHashMap
类名称:LinkedHashMap
方法名:computeIfAbsent
暂无
代码示例来源:origin: SonarSource/sonarqube
private LinkedHashMap<String, Long> getOrCreateFacet(String facetName) {
return facetsByName.computeIfAbsent(facetName, n -> new LinkedHashMap<>());
}
}
代码示例来源:origin: spring-projects/spring-framework
@Override
@Nullable
public V computeIfAbsent(String key, Function<? super String, ? extends V> mappingFunction) {
String oldKey = this.caseInsensitiveKeys.putIfAbsent(convertKey(key), key);
if (oldKey != null) {
return this.targetMap.get(oldKey);
}
return this.targetMap.computeIfAbsent(key, mappingFunction);
}
代码示例来源:origin: apache/kafka
private void update(Map<TopicPartition, S> partitionToState) {
LinkedHashMap<String, List<TopicPartition>> topicToPartitions = new LinkedHashMap<>();
for (TopicPartition tp : partitionToState.keySet()) {
List<TopicPartition> partitions = topicToPartitions.computeIfAbsent(tp.topic(), k -> new ArrayList<>());
partitions.add(tp);
}
for (Map.Entry<String, List<TopicPartition>> entry : topicToPartitions.entrySet()) {
for (TopicPartition tp : entry.getValue()) {
S state = partitionToState.get(tp);
map.put(tp, state);
}
}
}
代码示例来源:origin: org.springframework/spring-core
@Override
@Nullable
public V computeIfAbsent(String key, Function<? super String, ? extends V> mappingFunction) {
String oldKey = this.caseInsensitiveKeys.putIfAbsent(convertKey(key), key);
if (oldKey != null) {
return this.targetMap.get(oldKey);
}
return this.targetMap.computeIfAbsent(key, mappingFunction);
}
代码示例来源:origin: speedment/speedment
/**
* This method will return the value for the specified key if it is cached,
* and if not, will calculate the value using the supplied method. The
* computed value may or may not be stored in the cache afterwards. This
* method is guaranteed to be lock-free, and might calculate the value
* instead of using the cached one to avoid blocking.
*
* @param key the key to retrieve the value for
* @param compute method to use to compute the value if it is not cached
* @return the cached or computed value
*/
public long getOrCompute(K key, LongSupplier compute) {
if (free.compareAndSet(true, false)) {
try {
return cache.computeIfAbsent(key, k -> compute.getAsLong());
} finally {
free.set(true);
}
} else {
return compute.getAsLong();
}
}
代码示例来源:origin: org.apache.lucene/lucene-analyzers-common
private void handleWrappedFilterArgs(Map<String, String> args) {
LinkedHashMap<String, Map<String, String>> wrappedFilterArgs = new LinkedHashMap<>();
splitAt(',', wrappedFilters).forEach(filterName -> { // Format: SPIname[-id]
filterName = filterName.trim().toLowerCase(Locale.ROOT); // Treat case-insensitively
if (wrappedFilterArgs.containsKey(filterName)) {
throw new IllegalArgumentException("wrappedFilters contains duplicate '"
+ filterName + "'. Add unique '-id' suffixes (stripped prior to SPI lookup).");
}
wrappedFilterArgs.put(filterName, new HashMap<>());
});
for (Iterator<Map.Entry<String, String>> iterator = args.entrySet().iterator(); iterator.hasNext(); ) {
Map.Entry<String, String> entry = iterator.next();
String filterArgKey = entry.getKey();
String argValue = entry.getValue();
List<String> splitKey = splitAt(FILTER_ARG_SEPARATOR, filterArgKey); // Format: filterName.argKey
if (splitKey.size() == 2) { // Skip if no slash
String filterName = splitKey.get(0).toLowerCase(Locale.ROOT);
if (wrappedFilterArgs.containsKey(filterName)) { // Skip if not in "wrappedFilter" arg
Map<String, String> filterArgs = wrappedFilterArgs.computeIfAbsent(filterName, k -> new HashMap<>());
String argKey = splitKey.get(1);
filterArgs.put(argKey, argValue); // argKey is guaranteed unique, don't need to check for duplicates
iterator.remove();
}
}
}
if (args.isEmpty()) {
populateInnerFilters(wrappedFilterArgs);
}
}
代码示例来源:origin: ebean-orm/ebean
/**
* Add a 'pending drops' changeSet for the given version.
*/
public void add(MigrationVersion version, ChangeSet changeSet) {
Entry entry = map.computeIfAbsent(version.normalised(), k -> new Entry(version));
entry.add(changeSet);
}
代码示例来源:origin: com.blackducksoftware.magpie/magpie
@Override
public String[] computeIfAbsent(String key, Function<? super String, ? extends String[]> mappingFunction) {
synchronized (this) {
return super.computeIfAbsent(key, mappingFunction);
}
}
};
代码示例来源:origin: org.seedstack.shed/shed
@Override
public V get(K key) {
synchronized (map) {
return map.computeIfAbsent(key, loadingFunction);
}
}
代码示例来源:origin: com.ca.apim.gateway/gateway-export-plugin
@Override
public synchronized Object computeIfAbsent(Object key, Function<? super Object, ?> mappingFunction) {
return propertyMap.computeIfAbsent(key, mappingFunction);
}
代码示例来源:origin: org.sonarsource.sonarqube/sonar-server
private LinkedHashMap<String, Long> getOrCreateFacet(String facetName) {
return facetsByName.computeIfAbsent(facetName, n -> new LinkedHashMap<>());
}
}
代码示例来源:origin: SonarSonic/Calculator
public static ArrayList<IResearch> getPlayerResearch(UUID uuid) {
if (uuid != null) {
research.computeIfAbsent(uuid, k -> new ArrayList<>());
return research.get(uuid);
}
return new ArrayList<>();
}
}
代码示例来源:origin: com.qwazr/qwazr-extractor
/**
* Add a field/value pair to the document
*
* @param field the name of the field
* @param value any value
*/
public void add(final ParserField field, final Object value) {
if (value == null)
return;
if (fields == null)
fields = new LinkedHashMap<>();
final List<Object> values = (List<Object>) fields.computeIfAbsent(field.name, f -> new ArrayList<>(1));
if (value instanceof Collection)
values.addAll((Collection) value);
else
values.add(value);
}
代码示例来源:origin: org.opencb.biodata/biodata-models
public VariantBuilder addSample(String sampleName, List<String> data) {
checkStudy("add sample");
if (samplesData == null) {
samplesData = new ArrayList<>(samplesPosition != null ? samplesPosition.size() : 1);
}
if (samplesPosition == null) {
samplesPosition = new LinkedHashMap<>();
}
Integer idx = samplesPosition.computeIfAbsent(sampleName, k -> samplesPosition.size());
addSample(idx, data);
return this;
}
代码示例来源:origin: org.apache.servicemix.bundles/org.apache.servicemix.bundles.spring-core
@Override
@Nullable
public V computeIfAbsent(String key, Function<? super String, ? extends V> mappingFunction) {
String oldKey = this.caseInsensitiveKeys.putIfAbsent(convertKey(key), key);
if (oldKey != null) {
return this.targetMap.get(oldKey);
}
return this.targetMap.computeIfAbsent(key, mappingFunction);
}
代码示例来源:origin: com.steammachine.org/common.utils
public void setProperty(String key, String value) {
Key keyy = new Key(ItemType.VALUE, key);
properties.computeIfAbsent(keyy, key1 -> Item.value("%s=%s", key1.id, null)).value = value;
}
代码示例来源:origin: cool.pandora/modeller-core
/**
* @param bagger Bagger
*/
private void initializeProfile(final Bagger bagger) {
final Collection<Profile> profiles = bagger.loadProfiles();
userProfiles = new HashMap<>();
profileFieldsMap = new LinkedHashMap<>();
for (final Profile profile : profiles) {
userProfiles.put(profile.getName(), profile);
final LinkedHashMap<String, ProfileField> standardFields = profile.getStandardFields();
final LinkedHashMap<String, ProfileField> customFields = profile.getCustomFields();
final LinkedHashMap<String, ProfileField> mergedMap = new LinkedHashMap<>();
mergedMap.putAll(standardFields);
mergedMap.putAll(customFields);
for (final ProfileField profileField : mergedMap.values()) {
final List<ProfileField> list =
profileFieldsMap.computeIfAbsent(profile.getName(), k -> new ArrayList<>());
list.add(profileField);
}
}
}
代码示例来源:origin: io.ebean/ebean
/**
* Add a 'pending drops' changeSet for the given version.
*/
public void add(MigrationVersion version, ChangeSet changeSet) {
Entry entry = map.computeIfAbsent(version.normalised(), k -> new Entry(version));
entry.add(changeSet);
}
代码示例来源:origin: making/spring-boot-db-samples
private static Collector<PizzaView, ?, List<Pizza>> toPizzaList() {
BiConsumer<LinkedHashMap<Long, Pizza>, PizzaView> accumulator = (acc, pv) -> {
Topping t = new Topping(pv.toppingId);
t.setName(pv.toppingName);
Pizza p = acc.computeIfAbsent(pv.id, pv::toPizza);
p.getToppings().add(t);
acc.put(pv.id, p);
};
return Collector.of(LinkedHashMap::new, accumulator, (l, r) -> {
r.forEach((key, value) -> l.merge(key, value, (p1, p2) -> {
p1.getToppings().addAll(p2.getToppings());
return p1;
}));
return l;
}, map -> new ArrayList<>(map.values()));
}
代码示例来源:origin: vavr-io/vavr-jackson
private static String expectedMultimapJson(Multimap<?, ?> multimap, int opts) {
final LinkedHashMap<Object, List<Object>> map = new LinkedHashMap<>();
multimap.forEach(e -> {
List<Object> list = map.computeIfAbsent(e._1, k -> new ArrayList<>());
list.add(e._2);
});
StringBuilder sb = new StringBuilder("{");
map.forEach((k, l) -> sb.append(expectedJson(k.toString(), opts)).append(":").append(expectedJson(io.vavr.collection.Stream.ofAll(l))));
sb.append("}");
return sb.toString();
}
内容来源于网络,如有侵权,请联系作者删除!