本文整理汇总了Java中java.util.function.BiPredicate类的典型用法代码示例。如果您正苦于以下问题:Java BiPredicate类的具体用法?Java BiPredicate怎么用?Java BiPredicate使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
BiPredicate类属于java.util.function包,在下文中一共展示了BiPredicate类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testToImmutableSetMultimap
import java.util.function.BiPredicate; //导入依赖的package包/类
public void testToImmutableSetMultimap() {
Collector<Entry<String, Integer>, ?, ImmutableSetMultimap<String, Integer>> collector =
ImmutableSetMultimap.toImmutableSetMultimap(Entry::getKey, Entry::getValue);
BiPredicate<ImmutableSetMultimap<?, ?>, ImmutableSetMultimap<?, ?>> equivalence =
Equivalence.equals()
.onResultOf(
(ImmutableSetMultimap<?, ?> mm)
-> ImmutableListMultimap.copyOf(mm).asMap().entrySet().asList())
.and(Equivalence.equals());
CollectorTester.of(collector, equivalence)
.expectCollects(ImmutableSetMultimap.of())
.expectCollects(
ImmutableSetMultimap.of("a", 1, "b", 2, "a", 3, "c", 4),
mapEntry("a", 1),
mapEntry("b", 2),
mapEntry("a", 3),
mapEntry("c", 4));
}
示例2: testFlatteningToImmutableListMultimap
import java.util.function.BiPredicate; //导入依赖的package包/类
public void testFlatteningToImmutableListMultimap() {
Collector<String, ?, ImmutableListMultimap<Character, Character>> collector =
ImmutableListMultimap.flatteningToImmutableListMultimap(
str -> str.charAt(0), str -> str.substring(1).chars().mapToObj(c -> (char) c));
BiPredicate<Multimap<?, ?>, Multimap<?, ?>> equivalence =
Equivalence.equals()
.onResultOf((Multimap<?, ?> mm) -> ImmutableList.copyOf(mm.asMap().entrySet()))
.and(Equivalence.equals());
ImmutableListMultimap<Character, Character> empty = ImmutableListMultimap.of();
ImmutableListMultimap<Character, Character> filled =
ImmutableListMultimap.<Character, Character>builder()
.putAll('b', Arrays.asList('a', 'n', 'a', 'n', 'a'))
.putAll('a', Arrays.asList('p', 'p', 'l', 'e'))
.putAll('c', Arrays.asList('a', 'r', 'r', 'o', 't'))
.putAll('a', Arrays.asList('s', 'p', 'a', 'r', 'a', 'g', 'u', 's'))
.putAll('c', Arrays.asList('h', 'e', 'r', 'r', 'y'))
.build();
CollectorTester.of(collector, equivalence)
.expectCollects(empty)
.expectCollects(filled, "banana", "apple", "carrot", "asparagus", "cherry");
}
示例3: convert
import java.util.function.BiPredicate; //导入依赖的package包/类
/**
* Null-safely convert between two maps by applying a function to each key and value. A predicate is also specified to filter
* the results, in case the mapping function were to generate duplicate keys, etc.
*/
public static <T1, T2, U1, U2> Map<U1, U2> convert(Map<T1, T2> toConvert,
Function<? super T1, ? extends U1> keyConverter,
Function<? super T2, ? extends U2> valueConverter,
BiPredicate<U1, U2> resultFilter) {
if (toConvert == null) {
return null;
}
Map<U1, U2> result = toConvert.entrySet().stream()
.map(e -> new SimpleImmutableEntry<>(keyConverter.apply(e.getKey()),
valueConverter.apply(e.getValue())))
.filter(p -> resultFilter.test(p.getKey(), p.getValue()))
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
return Collections.unmodifiableMap(result);
}
示例4: expireIf
import java.util.function.BiPredicate; //导入依赖的package包/类
@Nonnull
@Override
public SingleSubscriptionBuilder<T> expireIf(@Nonnull BiPredicate<SingleSubscription<T>, T> predicate, @Nonnull ExpiryTestStage... testPoints) {
Preconditions.checkNotNull(testPoints, "testPoints");
Preconditions.checkNotNull(predicate, "predicate");
for (ExpiryTestStage testPoint : testPoints) {
switch (testPoint) {
case PRE:
preExpiryTests.add(predicate);
break;
case POST_FILTER:
midExpiryTests.add(predicate);
break;
case POST_HANDLE:
postExpiryTests.add(predicate);
break;
default:
throw new IllegalArgumentException("Unknown ExpiryTestPoint: " + testPoint);
}
}
return this;
}
示例5: expireIf
import java.util.function.BiPredicate; //导入依赖的package包/类
@Nonnull
@Override
public MergedSubscriptionBuilder<T> expireIf(@Nonnull BiPredicate<MergedSubscription<T>, T> predicate, @Nonnull ExpiryTestStage... testPoints) {
Preconditions.checkNotNull(testPoints, "testPoints");
Preconditions.checkNotNull(predicate, "predicate");
for (ExpiryTestStage testPoint : testPoints) {
switch (testPoint) {
case PRE:
preExpiryTests.add(predicate);
break;
case POST_FILTER:
midExpiryTests.add(predicate);
break;
case POST_HANDLE:
postExpiryTests.add(predicate);
break;
default:
throw new IllegalArgumentException("Unknown ExpiryTestPoint: " + testPoint);
}
}
return this;
}
示例6: findAll
import java.util.function.BiPredicate; //导入依赖的package包/类
/**
* Finds all values matching the predicate, returns as a map.
*/
public static <K,V> Map<K,V> findAll(Map<K,V> receiver, BiPredicate<K,V> predicate) {
// try to preserve some properties of the receiver (see the groovy javadocs)
final Map<K,V> map;
if (receiver instanceof TreeMap) {
map = new TreeMap<>();
} else {
map = new LinkedHashMap<>();
}
for (Map.Entry<K,V> kvPair : receiver.entrySet()) {
if (predicate.test(kvPair.getKey(), kvPair.getValue())) {
map.put(kvPair.getKey(), kvPair.getValue());
}
}
return map;
}
示例7: _search
import java.util.function.BiPredicate; //导入依赖的package包/类
private int _search(final List<K> list, final int start, final int end, final BiPredicate<K,K> isGreater, final K searchable) {
final int mid = (start + end) / 2;
if(list.get(mid) == searchable) return mid;
if(start == end) {
if (isGreater.test(list.get(mid), searchable)) {
return mid;
} else {
return mid + 1;
}
}
if(isGreater.test(list.get(mid), searchable)) {
return _search(list, start, mid, isGreater, searchable);
} else {
return _search(list, mid+1, end, isGreater, searchable);
}
}
示例8: sort
import java.util.function.BiPredicate; //导入依赖的package包/类
public List<K> sort(final List<K> input, final BiPredicate<K, K> isGreater) {
int sortedResult = isSorted(input, isGreater);
if (sortedResult == 1) return input;
if (sortedResult == -1) {
Collections.reverse(input);
return input;
}
for(int i=1; i<input.size(); i++) {
for(int j=0; j<i; j++) {
if(isGreater.test(input.get(j), input.get(i))) {
input.add(j, input.get(i));
input.remove(i+1);
}
}
}
return input;
}
示例9: sort
import java.util.function.BiPredicate; //导入依赖的package包/类
@Override
public List<K> sort(final List<K> input, final BiPredicate<K, K> isGreater) {
int sortedResult = isSorted(input, isGreater);
if (sortedResult == 1) return input;
if (sortedResult == -1) {
Collections.reverse(input);
return input;
}
final Future<List<K>> future = threadPool.submit(new SorterThread<>(input, isGreater, threadPool, 1));
try {
return future.get();
} catch(final Exception ex) {
throw new RuntimeException(ex);
}
}
示例10: testToImmutableSortedMultiset
import java.util.function.BiPredicate; //导入依赖的package包/类
public void testToImmutableSortedMultiset() {
BiPredicate<ImmutableSortedMultiset<String>, ImmutableSortedMultiset<String>> equivalence =
(ms1, ms2)
-> ms1.equals(ms2)
&& ms1.entrySet().asList().equals(ms2.entrySet().asList())
&& ms1.comparator().equals(ms2.comparator());
CollectorTester.of(
ImmutableSortedMultiset.<String>toImmutableSortedMultiset(
String.CASE_INSENSITIVE_ORDER),
equivalence)
.expectCollects(ImmutableSortedMultiset.emptyMultiset(String.CASE_INSENSITIVE_ORDER))
.expectCollects(
ImmutableSortedMultiset.orderedBy(String.CASE_INSENSITIVE_ORDER)
.addCopies("a", 2)
.addCopies("b", 1)
.addCopies("c", 3)
.build(),
"a",
"c",
"b",
"c",
"A",
"C");
}
示例11: testCaptureArrayList
import java.util.function.BiPredicate; //导入依赖的package包/类
@Test public void testCaptureArrayList() {
BiPredicate<Integer, Integer> biPredicate;
if (System.currentTimeMillis() > 5) {
// Always uses this branch
String data2 = optimisationBarrier(rawCityData);
ArrayList<CountryData> cities = CountryData.parseCityData(data2);
biPredicate = (i, e) -> cities.get(i).getPopulation() == e;
} else {
biPredicate = (i, e) -> i == e;
}
System.gc();
assertTrue(biPredicate.test(0, 20693000)); // China
assertTrue(biPredicate.test(16, 3040740)); // Ethiopia
assertFalse(biPredicate.test(3, 24)); // Japan
}
示例12: removeIf
import java.util.function.BiPredicate; //导入依赖的package包/类
boolean removeIf(BiPredicate<? super K, ? super V> filter) {
checkNotNull(filter);
boolean changed = false;
for (K key : keySet()) {
while (true) {
V value = get(key);
if (value == null || !filter.test(key, value)) {
break;
} else if (LocalCache.this.remove(key, value)) {
changed = true;
break;
}
}
}
return changed;
}
示例13: testToImmutableListMultimap
import java.util.function.BiPredicate; //导入依赖的package包/类
public void testToImmutableListMultimap() {
Collector<Entry<String, Integer>, ?, ImmutableListMultimap<String, Integer>> collector =
ImmutableListMultimap.toImmutableListMultimap(Entry::getKey, Entry::getValue);
BiPredicate<ImmutableListMultimap<?, ?>, ImmutableListMultimap<?, ?>> equivalence =
Equivalence.equals()
.onResultOf((ImmutableListMultimap<?, ?> mm) -> mm.asMap().entrySet().asList())
.and(Equivalence.equals());
CollectorTester.of(collector, equivalence)
.expectCollects(ImmutableListMultimap.of())
.expectCollects(
ImmutableListMultimap.of("a", 1, "b", 2, "a", 3, "c", 4),
mapEntry("a", 1),
mapEntry("b", 2),
mapEntry("a", 3),
mapEntry("c", 4));
}
示例14: testFlatteningToImmutableSetMultimap
import java.util.function.BiPredicate; //导入依赖的package包/类
public void testFlatteningToImmutableSetMultimap() {
Collector<String, ?, ImmutableSetMultimap<Character, Character>> collector =
ImmutableSetMultimap.flatteningToImmutableSetMultimap(
str -> str.charAt(0), str -> str.substring(1).chars().mapToObj(c -> (char) c));
BiPredicate<Multimap<?, ?>, Multimap<?, ?>> equivalence =
Equivalence.equals()
.onResultOf((Multimap<?, ?> mm) -> ImmutableList.copyOf(mm.asMap().entrySet()))
.and(Equivalence.equals());
ImmutableSetMultimap<Character, Character> empty = ImmutableSetMultimap.of();
ImmutableSetMultimap<Character, Character> filled =
ImmutableSetMultimap.<Character, Character>builder()
.putAll('b', Arrays.asList('a', 'n', 'a', 'n', 'a'))
.putAll('a', Arrays.asList('p', 'p', 'l', 'e'))
.putAll('c', Arrays.asList('a', 'r', 'r', 'o', 't'))
.putAll('a', Arrays.asList('s', 'p', 'a', 'r', 'a', 'g', 'u', 's'))
.putAll('c', Arrays.asList('h', 'e', 'r', 'r', 'y'))
.build();
CollectorTester.of(collector, equivalence)
.expectCollects(empty)
.expectCollects(filled, "banana", "apple", "carrot", "asparagus", "cherry");
}
示例15: testFlatteningToMultimap
import java.util.function.BiPredicate; //导入依赖的package包/类
public void testFlatteningToMultimap() {
Collector<String, ?, ListMultimap<Character, Character>> collector =
Multimaps.flatteningToMultimap(
str -> str.charAt(0),
str -> str.substring(1).chars().mapToObj(c -> (char) c),
MultimapBuilder.linkedHashKeys().arrayListValues()::build);
BiPredicate<Multimap<?, ?>, Multimap<?, ?>> equivalence =
Equivalence.equals()
.onResultOf((Multimap<?, ?> mm) -> ImmutableList.copyOf(mm.asMap().entrySet()))
.and(Equivalence.equals());
ListMultimap<Character, Character> empty =
MultimapBuilder.linkedHashKeys().arrayListValues().build();
ListMultimap<Character, Character> filled =
MultimapBuilder.linkedHashKeys().arrayListValues().build();
filled.putAll('b', Arrays.asList('a', 'n', 'a', 'n', 'a'));
filled.putAll('a', Arrays.asList('p', 'p', 'l', 'e'));
filled.putAll('c', Arrays.asList('a', 'r', 'r', 'o', 't'));
filled.putAll('a', Arrays.asList('s', 'p', 'a', 'r', 'a', 'g', 'u', 's'));
filled.putAll('c', Arrays.asList('h', 'e', 'r', 'r', 'y'));
CollectorTester.of(collector, equivalence)
.expectCollects(empty)
.expectCollects(filled, "banana", "apple", "carrot", "asparagus", "cherry");
}