下面列出了com.google.common.collect.Iterables#removeIf ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void removeEmptyExpirationQueues() {
// Periodically go through and remove empty expiration queues so they don't accumulate forever.
// This cleans up queues with really long TTLs where every member was explicitly removed.
Iterables.removeIf(_expirationQueues.values(), new Predicate<LinkedHashSet<Claim>>() {
@Override
public boolean apply(LinkedHashSet<Claim> queue) {
return queue.isEmpty();
}
});
Iterables.removeIf(_schedule, new Predicate<ProcessAt>() {
@Override
public boolean apply(ProcessAt processAt) {
return processAt.getQueue().isEmpty();
}
});
checkState(_schedule.size() == _expirationQueues.size());
}
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
@Scheduled(fixedRate = MINUTE)
public void checkActiveServices() {
for (final Map.Entry<ServiceDescriptor, Long> entry : pingedServers.entrySet()) {
if (isActive(entry.getValue())) {
LOGGER.info("Removing services with address {}", entry.getKey());
Iterables.removeIf(services, new Predicate<ServiceDescriptor>() {
@Override
public boolean apply(ServiceDescriptor service) {
return service.equals(entry.getKey());
}
});
pingedServers.remove(entry.getKey());
}
}
gaugeService.submit("registered.services.count", services.size());
gaugeService.submit("registered.servers.count", pingedServers.size());
}
@Override
public void filterRowCells(List<Cell> kvs) throws IOException {
if (kvs.isEmpty()) return;
Cell firstKV = kvs.get(0);
Iterables.removeIf(kvs, new Predicate<Cell>() {
@Override
public boolean apply(Cell kv) {
int qualifier = encodingScheme.decode(kv.getQualifierArray(), kv.getQualifierOffset(), kv.getQualifierLength());
return !trackedColumns.get(qualifier);
}
});
if (kvs.isEmpty()) {
kvs.add(new KeyValue(firstKV.getRowArray(), firstKV.getRowOffset(), firstKV.getRowLength(),
this.emptyCFName, 0, this.emptyCFName.length, ENCODED_EMPTY_COLUMN_BYTES, 0,
ENCODED_EMPTY_COLUMN_BYTES.length, HConstants.LATEST_TIMESTAMP, Type.Maximum, null, 0, 0));
}
}
/**
* Remove potential LanguageProfiles, e.g. in combination with {@link #loadAllBuiltInLanguageProfiles()}.
*
* @param isoString the ISO string of the LanguageProfile to be removed.
*/
public LanguageProfileValidator removeLanguageProfile(final String isoString) {
Iterables.removeIf(this.languageProfiles, new Predicate<LanguageProfile>() {
@Override
public boolean apply(LanguageProfile languageProfile) {
return languageProfile.getLocale().getLanguage().equals(isoString);
}
});
return this;
}
@Test
public final void givenListContainsNulls_whenRemovingNullsWithGuavaV1_thenCorrect() {
final List<Integer> list = Lists.newArrayList(null, 1, null);
Iterables.removeIf(list, Predicates.isNull());
assertThat(list, hasSize(1));
}
private Map<String, String> getContextMap(String processId, Request request) {
Map<String, String> contextMap = (Map) request.getContext();
ProjectLogger.log(
"UserBulkMigrationActor:getContextMap:started preparing record for processId:"
+ processId
+ "with request context:"
+ contextMap,
LoggerEnum.INFO.name());
contextMap.put(JsonKey.ACTOR_TYPE, StringUtils.capitalize(JsonKey.SYSTEM));
contextMap.put(JsonKey.ACTOR_ID, ProjectUtil.getUniqueIdFromTimestamp(0));
Iterables.removeIf(contextMap.values(), value -> StringUtils.isBlank(value));
return contextMap;
}
@Test
@SuppressWarnings("unchecked") // Needed because type information lost in vargs.
public void testZipRemove() {
final int DEFAULT = 10;
Iterable<List<Integer>> meta = Iterables2.zip(DEFAULT,
list(1, 2, 3, 4),
list(5, 6, 7, 8),
list(9));
// Attempt to trim all rows that have the default value.
Iterables.removeIf(meta, input -> Iterables.contains(input, DEFAULT));
assertValues(meta, list(1, 5, 9));
}
/**
* Cleans up old claim sets for subscriptions that have become inactive. Ensures the map of claim sets doesn't
* grow forever.
*/
private synchronized void removeEmptyClaimSets() {
Iterables.removeIf(_map.values(), new Predicate<Handle>() {
@Override
public boolean apply(Handle handle) {
handle.getClaimSet().pump();
return handle.getRefCount().get() == 0 && handle.getClaimSet().size() == 0;
}
});
}
private static void removeTest() {
Collection<String> jdk = Lists.newArrayList("a1", "a2", "a3", "a1");
Iterable<String> guava = Lists.newArrayList(jdk);
Iterable<String> apache = Lists.newArrayList(jdk);
MutableCollection<String> gs = FastList.newList(jdk);
// remove if
jdk.removeIf((s) -> s.contains("1")); // using jdk
Iterables.removeIf(guava, (s) -> s.contains("1")); // using guava
CollectionUtils.filter(apache, (s) -> !s.contains("1")); // using apache
gs.removeIf((Predicate<String>) (s) -> s.contains("1")); // using gs
System.out.println("removeIf = " + jdk + ":" + guava + ":" + apache + ":" + gs); // print removeIf = [a2, a3]:[a2, a3]:[a2, a3]:[a2, a3]
}
private static void retainTest() {
Collection<String> jdk = Lists.newArrayList("a1", "a2", "a3", "a1");
Iterable<String> guava = Lists.newArrayList(jdk);
Iterable<String> apache = Lists.newArrayList(jdk);
MutableCollection<String> gs = FastList.newList(jdk);
// remove if not
jdk.removeIf((s) -> !s.contains("1")); // using jdk
Iterables.removeIf(guava, (s) -> !s.contains("1")); // using guava
CollectionUtils.filter(apache, (s) -> s.contains("1")); // using apache
gs.removeIf((Predicate<String>) (s) -> !s.contains("1")); //using gs
System.out.println("retainIf = " + jdk + ":" + guava + ":" + apache + ":" + gs); // print retainIf = [a1, a1]:[a1, a1]:[a1, a1]:[a1, a1]
}
private static void removeTest() {
Collection<String> jdk = Lists.newArrayList("a1", "a2", "a3", "a1");
Iterable<String> guava = Lists.newArrayList(jdk);
Iterable<String> apache = Lists.newArrayList(jdk);
MutableCollection<String> gs = FastList.newList(jdk);
// удаление по условию
jdk.removeIf((s) -> s.contains("1")); // с помощью jdk
Iterables.removeIf(guava, (s) -> s.contains("1")); // с помощью guava
CollectionUtils.filter(apache, (s) -> !s.contains("1")); // с помощью apache
gs.removeIf((Predicate<String>) (s) -> s.contains("1")); // с помощью gs
System.out.println("removeIf = " + jdk + ":" + guava + ":" + apache + ":" + gs); // напечатает removeIf = [a2, a3]:[a2, a3]:[a2, a3]:[a2, a3]
}
private static void retainTest() {
Collection<String> jdk = Lists.newArrayList("a1", "a2", "a3", "a1");
Iterable<String> guava = Lists.newArrayList(jdk);
Iterable<String> apache = Lists.newArrayList(jdk);
MutableCollection<String> gs = FastList.newList(jdk);
// удаление всех не равных условию
jdk.removeIf((s) -> !s.contains("1")); // с помощью jdk
Iterables.removeIf(guava, (s) -> !s.contains("1")); // с помощью guava
CollectionUtils.filter(apache, (s) -> s.contains("1")); // с помощью apache
gs.removeIf((Predicate<String>) (s) -> !s.contains("1")); //с помощью gs
System.out.println("retainIf = " + jdk + ":" + guava + ":" + apache + ":" + gs); // напечатает retainIf = [a1, a1]:[a1, a1]:[a1, a1]:[a1, a1]
}
public void removeRealization(final RealizationType type, final String realization) {
Iterables.removeIf(this.realizationEntries, new Predicate<RealizationEntry>() {
@Override
public boolean apply(RealizationEntry input) {
return input.getType() == type && input.getRealization().equalsIgnoreCase(realization);
}
});
}
/**
* Remove potential LanguageProfiles, e.g. in combination with {@link #loadAllBuiltInLanguageProfiles()}.
* @param isoString the ISO string of the LanguageProfile to be removed.
*/
public LanguageProfileValidator removeLanguageProfile(final String isoString) {
Iterables.removeIf(this.languageProfiles, new Predicate<LanguageProfile>() {
@Override
public boolean apply(LanguageProfile languageProfile) {
return languageProfile.getLocale().getLanguage().equals(isoString);
}
});
return this;
}
@Override
public void filterRowCells(List<Cell> kvs) throws IOException {
if (kvs.isEmpty()) return;
Cell firstKV = kvs.get(0);
Iterables.removeIf(kvs, new Predicate<Cell>() {
@Override
public boolean apply(Cell kv) {
ptr.set(kv.getFamilyArray(), kv.getFamilyOffset(), kv.getFamilyLength());
if (columnsTracker.containsKey(ptr)) {
Set<ImmutableBytesPtr> cols = columnsTracker.get(ptr);
ptr.set(kv.getQualifierArray(), kv.getQualifierOffset(), kv.getQualifierLength());
if (cols != null && !(cols.contains(ptr))) {
return true;
}
} else {
return true;
}
return false;
}
});
// make sure we're not holding to any of the byte[]'s
ptr.set(HConstants.EMPTY_BYTE_ARRAY);
if (kvs.isEmpty()) {
kvs.add(new KeyValue(firstKV.getRowArray(), firstKV.getRowOffset(), firstKV.getRowLength(),
this.emptyCFName, 0, this.emptyCFName.length, emptyKVQualifier, 0,
emptyKVQualifier.length, HConstants.LATEST_TIMESTAMP, Type.Maximum, null, 0, 0));
}
}
private static void addMember(
Multimap<String, MemberDescriptor> memberByMemberName, MemberDescriptor member) {
String name = member.getSimpleJsName();
Iterables.removeIf(memberByMemberName.get(name), m -> overrides(member, m));
memberByMemberName.put(name, member);
}
private void filterProposals(List<ICompletionProposal> proposals) {
Iterables.removeIf(proposals, not(validProposal()));
}
private void filterProposals(List<IGroovyProposal> proposals) {
Iterables.removeIf(proposals, not(validProposal()));
}
private void removeBlankLinesIn(Iterable<String> services) {
Iterables.removeIf(services, Predicates.equalTo(""));
}
/**
* Chains a given {@link List} of {@link JobListener}s into a single {@link JobListener}. The specified {@link JobListener}s
* will all be executed in parallel.
*
* @param jobListeners is a {@link List} of {@link JobListener}s that need to be executed
*
* @return a {@link CloseableJobListener}, which is similar to {@link JobListener}, except
* {@link CloseableJobListener#close()} will block until all {@link JobListener}s have finished their executions.
*/
public static CloseableJobListener parallelJobListener(List<JobListener> jobListeners) {
Iterables.removeIf(jobListeners, Predicates.isNull());
return new ParallelJobListener(jobListeners);
}