本文整理了Java中com.google.common.collect.Maps.filterValues()
方法的一些代码示例,展示了Maps.filterValues()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Maps.filterValues()
方法的具体详情如下:
包路径:com.google.common.collect.Maps
类名称:Maps
方法名:filterValues
[英]Returns a bimap containing the mappings in unfiltered whose values satisfy a predicate. The returned bimap is a live view of unfiltered; changes to one affect the other.
The resulting bimap's keySet(), entrySet(), and values() views have iterators that don't support remove(), but all other methods are supported by the bimap and its views. When given a value that doesn't satisfy the predicate, the bimap's put(), forcePut() and putAll() methods throw an IllegalArgumentException. Similarly, the map's entries have a Entry#setValue method that throws an IllegalArgumentException when the provided value doesn't satisfy the predicate.
When methods such as removeAll() and clear() are called on the filtered bimap or its views, only mappings that satisfy the filter will be removed from the underlying bimap.
The returned bimap isn't threadsafe or serializable, even if unfiltered is.
Many of the filtered bimap's methods, such as size(), iterate across every value in the underlying bimap and determine which satisfy the filter. When a live view is not needed, it may be faster to copy the filtered bimap and use the copy.
Warning: entryPredicate must be consistent with equals , as documented at Predicate#apply.
[中]返回一个bimap,其中包含值满足谓词的未筛选映射。返回的bimap是未过滤的实时视图;对其中一个的更改会影响另一个。
生成的bimap的keySet()、entrySet()和values()视图具有不支持remove()的迭代器,但bimap及其视图支持所有其他方法。当给定一个不满足谓词的值时,bimap的put()、forcePut()和putAll()方法会抛出一个IllegalArgumentException。类似地,map的条目有一个Entry#setValue方法,当提供的值不满足谓词时,该方法会抛出一个IllegalArgumentException。
当对过滤后的bimap或其视图调用removeAll()和clear()等方法时,只会从基础bimap中删除满足过滤器的映射。
返回的bimap不是线程安全的或可序列化的,即使是未筛选的。
许多过滤后的bimap方法,如size(),都会在基础bimap中的每个值上迭代,并确定哪些值满足过滤器的要求。当不需要实时视图时,复制过滤后的bimap并使用副本可能会更快。
警告:entryPredicate必须与equals一致,如Predicate#apply中所述。
代码示例来源:origin: atomix/atomix
@Override
public int size() {
checkState(!closed, destroyedMessage);
// TODO: Maintain a separate counter for tracking live elements in map.
return Maps.filterValues(items, MapValue::isAlive).size();
}
代码示例来源:origin: atomix/atomix
@Override
public Set<K> keySet() {
checkState(!closed, destroyedMessage);
return Maps.filterValues(items, MapValue::isAlive).keySet()
.stream()
.map(this::decodeKey)
.collect(Collectors.toSet());
}
代码示例来源:origin: apache/pulsar
/**
* Helper for getting stats.
*
* @return
*/
public Map<String, ManagedLedgerImpl> getManagedLedgers() {
// Return a view of already created ledger by filtering futures not yet completed
return Maps.filterValues(Maps.transformValues(ledgers, future -> future.getNow(null)), Predicates.notNull());
}
代码示例来源:origin: atomix/atomix
@Override
public Collection<V> values() {
checkState(!closed, destroyedMessage);
return Collections2.transform(Maps.filterValues(items, MapValue::isAlive).values(), value -> value.get(this::decodeValue));
}
代码示例来源:origin: prestodb/presto
@Override
public Map<Symbol, Symbol> visitProject(ProjectNode node, Set<Symbol> lookupSymbols)
{
// Map from output Symbols to source Symbols
Map<Symbol, Symbol> directSymbolTranslationOutputMap = Maps.transformValues(Maps.filterValues(node.getAssignments().getMap(), SymbolReference.class::isInstance), Symbol::from);
Map<Symbol, Symbol> outputToSourceMap = lookupSymbols.stream()
.filter(directSymbolTranslationOutputMap.keySet()::contains)
.collect(toImmutableMap(identity(), directSymbolTranslationOutputMap::get));
checkState(!outputToSourceMap.isEmpty(), "No lookup symbols were able to pass through the projection");
// Map from source Symbols to underlying index source Symbols
Map<Symbol, Symbol> sourceToIndexMap = node.getSource().accept(this, ImmutableSet.copyOf(outputToSourceMap.values()));
// Generate the Map the connects lookup symbols to underlying index source symbols
Map<Symbol, Symbol> outputToIndexMap = Maps.transformValues(Maps.filterValues(outputToSourceMap, in(sourceToIndexMap.keySet())), Functions.forMap(sourceToIndexMap));
return ImmutableMap.copyOf(outputToIndexMap);
}
代码示例来源:origin: apache/incubator-druid
public static List<Map> filterNullValues(List<Map<String, Object>> mapList)
{
return Lists.transform(mapList, (Function<Map, Map>) input -> Maps.filterValues(input, Objects::nonNull));
}
代码示例来源:origin: google/guava
@Override
protected Map<String, String> create(Entry<String, String>[] entries) {
Map<String, String> map = Maps.newHashMap();
putEntries(map, entries);
map.putAll(ENTRIES_TO_FILTER);
return Maps.filterValues(map, FILTER_VALUES);
}
})
代码示例来源:origin: google/error-prone
/** Parameters which must be referenced in any tree matched to this placeholder. */
Set<UVariableDecl> requiredParameters() {
return Maps.filterValues(
annotatedParameters(),
(ImmutableClassToInstanceMap<Annotation> annotations) ->
!annotations.containsKey(MayOptionallyUse.class))
.keySet();
}
代码示例来源:origin: atomix/atomix
@Override
public Set<Map.Entry<K, V>> entrySet() {
checkState(!closed, destroyedMessage);
return Maps.filterValues(items, MapValue::isAlive)
.entrySet()
.stream()
.map(e -> Pair.of(decodeKey(e.getKey()), decodeValue(e.getValue().get())))
.collect(Collectors.toSet());
}
代码示例来源:origin: atomix/atomix
@Override
public void clear() {
checkState(!closed, destroyedMessage);
Maps.filterValues(items, MapValue::isAlive)
.forEach((k, v) -> remove(decodeKey(k)));
}
代码示例来源:origin: apache/activemq
/**
* Filter, sort and page results.
*
* Returns JSON map with resulting destination views and total number of matched destinations
*
* @param page - defines result page to be returned
* @param pageSize - defines page size to be used
* @throws IOException
*/
String filter(int page, int pageSize) throws IOException {
ObjectMapper mapper = new ObjectMapper();
destinations = Maps.filterValues(destinations, getPredicate());
Map<ObjectName, DestinationView> pagedDestinations = getPagedDestinations(page, pageSize);
Map<String, Object> result = new HashMap<String, Object>();
result.put("data", pagedDestinations);
result.put("count", destinations.size());
StringWriter writer = new StringWriter();
mapper.writeValue(writer, result);
return writer.toString();
}
代码示例来源:origin: google/guava
@Override
protected NavigableMap<String, String> create(Entry<String, String>[] entries) {
NavigableMap<String, String> map = new SafeTreeMap<>();
putEntries(map, entries);
map.put("banana", "toast");
map.put("eggplant", "spam");
return Maps.filterValues(map, FILTER_VALUES);
}
})
代码示例来源:origin: google/guava
@Override
protected SortedMap<String, String> create(Entry<String, String>[] entries) {
SortedMap<String, String> map = new NonNavigableSortedMap();
putEntries(map, entries);
map.putAll(ENTRIES_TO_FILTER);
return Maps.filterValues(map, FILTER_VALUES);
}
})
代码示例来源:origin: google/guava
@Override
protected BiMap<String, String> create(Entry<String, String>[] entries) {
BiMap<String, String> map = HashBiMap.create();
putEntries(map, entries);
map.putAll(ENTRIES_TO_FILTER);
return Maps.filterValues(map, FILTER_VALUES);
}
})
代码示例来源:origin: google/guava
public void testFilteredValuesClear() {
Map<String, Integer> unfiltered = createUnfiltered();
unfiltered.put("one", 1);
unfiltered.put("two", 2);
unfiltered.put("three", 3);
unfiltered.put("four", 4);
Map<String, Integer> filtered = Maps.filterValues(unfiltered, EVEN);
assertEquals(ImmutableMap.of("one", 1, "two", 2, "three", 3, "four", 4), unfiltered);
assertEquals(ImmutableMap.of("two", 2, "four", 4), filtered);
filtered.clear();
assertEquals(ImmutableMap.of("one", 1, "three", 3), unfiltered);
assertTrue(filtered.isEmpty());
}
代码示例来源:origin: google/guava
public void testFilteredValuesIllegalPutAll() {
Map<String, Integer> unfiltered = createUnfiltered();
Map<String, Integer> filtered = Maps.filterValues(unfiltered, EVEN);
filtered.put("a", 2);
unfiltered.put("b", 4);
unfiltered.put("c", 5);
assertEquals(ImmutableMap.of("a", 2, "b", 4), filtered);
try {
filtered.putAll(ImmutableMap.of("c", 4, "zzz", 5, "b", 6));
fail();
} catch (IllegalArgumentException expected) {
}
assertEquals(ImmutableMap.of("a", 2, "b", 4), filtered);
}
代码示例来源:origin: google/guava
public void testFilteredValuesIllegalSetValue() {
Map<String, Integer> unfiltered = createUnfiltered();
Map<String, Integer> filtered = Maps.filterValues(unfiltered, EVEN);
filtered.put("a", 2);
filtered.put("b", 4);
assertEquals(ImmutableMap.of("a", 2, "b", 4), filtered);
Entry<String, Integer> entry = filtered.entrySet().iterator().next();
try {
entry.setValue(5);
fail();
} catch (IllegalArgumentException expected) {
}
assertEquals(ImmutableMap.of("a", 2, "b", 4), filtered);
}
代码示例来源:origin: google/guava
public void testFilteredValuesIllegalPut() {
Map<String, Integer> unfiltered = createUnfiltered();
Map<String, Integer> filtered = Maps.filterValues(unfiltered, EVEN);
filtered.put("a", 2);
unfiltered.put("b", 4);
unfiltered.put("c", 5);
assertEquals(ImmutableMap.of("a", 2, "b", 4), filtered);
try {
filtered.put("yyy", 3);
fail();
} catch (IllegalArgumentException expected) {
}
assertEquals(ImmutableMap.of("a", 2, "b", 4), filtered);
}
代码示例来源:origin: atomix/atomix
@Override
public void evict(byte[] id) {
try {
List<LeadershipEvent<byte[]>> changes = Lists.newArrayList();
Set<String> topics = Maps.filterValues(elections, e -> e.candidates().stream().anyMatch(candidate -> Arrays.equals(candidate, id))).keySet();
topics.forEach(topic -> {
Leadership<byte[]> oldLeadership = leadership(topic);
elections.compute(topic, (k, v) -> v.evict(id, termCounter(topic)::incrementAndGet));
Leadership<byte[]> newLeadership = leadership(topic);
if (!Objects.equal(oldLeadership, newLeadership)) {
notifyLeadershipChange(topic, oldLeadership, newLeadership);
}
});
} catch (Exception e) {
getLogger().error("State machine operation failed", e);
throw Throwables.propagate(e);
}
}
代码示例来源:origin: apache/hive
@Test public void computeOptimizedScanPartitionOrAndCombinedFilter() {
KafkaScanTrimmer kafkaScanTrimmer = new KafkaScanTrimmer(fullHouse, null);
// partition = 1 or (partition >2 and <= 3)
ExprNodeGenericFuncDesc eq = eq(Lists.newArrayList(partitionColumn, ConstantExprBuilder.build(1)));
ExprNodeGenericFuncDesc lessEq = lessThanEq(Lists.newArrayList(partitionColumn, ConstantExprBuilder.build(3)));
ExprNodeGenericFuncDesc greater = greaterThan(Lists.newArrayList(partitionColumn, ConstantExprBuilder.build(2)));
ExprNodeGenericFuncDesc orNode = or(Lists.newArrayList(and(Lists.newArrayList(lessEq, greater)), eq));
Map
actual =
kafkaScanTrimmer.computeOptimizedScan(SerializationUtilities
.deserializeExpression(SerializationUtilities.serializeExpression(orNode)));
Map
expected =
Maps.filterValues(fullHouse, tp -> Objects.requireNonNull(tp).getPartition() == 1 || tp.getPartition() == 3);
Assert.assertEquals(expected, actual);
assertNotNull(orNode);
}
内容来源于网络,如有侵权,请联系作者删除!