當前位置: 首頁>>代碼示例>>Java>>正文


Java IntPredicate類代碼示例

本文整理匯總了Java中java.util.function.IntPredicate的典型用法代碼示例。如果您正苦於以下問題:Java IntPredicate類的具體用法?Java IntPredicate怎麽用?Java IntPredicate使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


IntPredicate類屬於java.util.function包,在下文中一共展示了IntPredicate類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: IntPredicate

import java.util.function.IntPredicate; //導入依賴的package包/類
@Test
public void IntPredicate()
{
    // TODO - Convert the anonymous inner class to a lambda
    IntPredicate predicate = new IntPredicate()
    {
        @Override
        public boolean test(int value)
        {
            return value % 2 == 0;
        }
    };
    List<Integer> evens =
            IntStream.rangeClosed(1, 5).filter(predicate).boxed().collect(Collectors.toList());
    Assert.assertEquals(Arrays.asList(2, 4), evens);
    List<Integer> odds =
            IntStream.rangeClosed(1, 5).filter(predicate.negate()).boxed().collect(Collectors.toList());
    Assert.assertEquals(Arrays.asList(1, 3, 5), odds);
    Assert.assertTrue(IntStream.rangeClosed(1, 5).anyMatch(predicate));
    Assert.assertFalse(IntStream.rangeClosed(1, 5).allMatch(predicate));
    Assert.assertFalse(IntStream.rangeClosed(1, 5).noneMatch(predicate));
}
 
開發者ID:BNYMellon,項目名稱:CodeKatas,代碼行數:23,代碼來源:PrimitiveFunctionalInterfaceTest.java

示例2: makeInt

import java.util.function.IntPredicate; //導入依賴的package包/類
/**
 * Constructs a quantified predicate matcher for an {@code IntStream}.
 *
 * @param predicate the {@code Predicate} to apply to stream elements
 * @param matchKind the kind of quantified match (all, any, none)
 * @return a {@code TerminalOp} implementing the desired quantified match
 *         criteria
 */
public static TerminalOp<Integer, Boolean> makeInt(IntPredicate predicate,
                                                   MatchKind matchKind) {
    Objects.requireNonNull(predicate);
    Objects.requireNonNull(matchKind);
    class MatchSink extends BooleanTerminalSink<Integer> implements Sink.OfInt {
        MatchSink() {
            super(matchKind);
        }

        @Override
        public void accept(int t) {
            if (!stop && predicate.test(t) == matchKind.stopOnPredicateMatches) {
                stop = true;
                value = matchKind.shortCircuitResult;
            }
        }
    }

    return new MatchOp<>(StreamShape.INT_VALUE, matchKind, MatchSink::new);
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:29,代碼來源:MatchOps.java

示例3: filter

import java.util.function.IntPredicate; //導入依賴的package包/類
@Override
public final IntStream filter(IntPredicate predicate) {
    Objects.requireNonNull(predicate);
    return new StatelessOp<Integer>(this, StreamShape.INT_VALUE,
                                    StreamOpFlag.NOT_SIZED) {
        @Override
        Sink<Integer> opWrapSink(int flags, Sink<Integer> sink) {
            return new Sink.ChainedInt<Integer>(sink) {
                @Override
                public void begin(long size) {
                    downstream.begin(-1);
                }

                @Override
                public void accept(int t) {
                    if (predicate.test(t))
                        downstream.accept(t);
                }
            };
        }
    };
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:23,代碼來源:IntPipeline.java

示例4: takeIntWhile

import java.util.function.IntPredicate; //導入依賴的package包/類
/**
 * Adapted from StackOverflow {@linkplain so http://stackoverflow.com/questions/20746429/limit-a-stream-by-a-predicate}
 *
 * @param splitr the original Spliterator
 * @param predicate the predicate
 * @return a Spliterator.OfInt
 */
private static Spliterator.OfInt takeIntWhile(Spliterator.OfInt splitr, IntPredicate predicate) {
    return new Spliterators.AbstractIntSpliterator(splitr.estimateSize(), 0) {
        boolean stillGoing = true;

        @Override
        public boolean tryAdvance(IntConsumer consumer) {
            if (stillGoing) {
                boolean hadNext = splitr.tryAdvance((int elem) -> {
                    if (predicate.test(elem)) {
                        consumer.accept(elem);
                    } else {
                        stillGoing = false;
                    }
                });
                return hadNext && stillGoing;
            }
            return false;
        }
    };
}
 
開發者ID:fralalonde,項目名稱:iostream,代碼行數:28,代碼來源:StreamInputAdapter.java

示例5: estimateSize

import java.util.function.IntPredicate; //導入依賴的package包/類
/**
 * Estimates the size of a filter applied to an IMembershipSet
 * @return an approximation of the size based on sampling. May return 0.
 * There are no strict guarantees on the quality of the approximation,
 * but is good enough for initialization of a hash table sizes.
 */
public static int estimateSize(final IMembershipSet baseMap,
                               final IntPredicate filter) {
    final IMembershipSet sampleSet = baseMap.sample(sizeEstimationSampleSize, 0);
    if (sampleSet.getSize() == 0)
        return 0;
    int eSize = 0;
    final IRowIterator iter = sampleSet.getIterator();
    int curr = iter.getNextRow();
    while (curr >= 0) {
        if (filter.test(curr))
            eSize++;
        curr = iter.getNextRow();
    }
    return (baseMap.getSize() * eSize) / sampleSet.getSize();
}
 
開發者ID:vmware,項目名稱:hillview,代碼行數:22,代碼來源:MembershipSetFactory.java

示例6: shouldWrapExecutionExceptionInMemoizationException

import java.util.function.IntPredicate; //導入依賴的package包/類
/**
 * @throws ExecutionException
 *             Added for the call to 'cache.get(..)'.
 */
@Test
@SuppressWarnings(CompilerWarnings.UNCHECKED)
public void shouldWrapExecutionExceptionInMemoizationException() throws ExecutionException {
    // given
    final IntPredicate predicate = a -> true;
    final IntFunction<String> keyFunction = a -> "key";
    final Cache<String, Boolean> cache = Mockito.mock(Cache.class);
    given(cache.get(any(), any())).willThrow(ExecutionException.class);
    final GuavaCacheBasedIntPredicateMemoizer<String> memoizer = new GuavaCacheBasedIntPredicateMemoizer<>(
            cache, keyFunction, predicate);

    // when
    thrown.expect(MemoizationException.class);

    // then
    memoizer.test(123);
}
 
開發者ID:sebhoss,項目名稱:memoization.java,代碼行數:22,代碼來源:GuavaCacheBasedIntPredicateMemoizerTest.java

示例7: shouldRequireNonNullPredicate

import java.util.function.IntPredicate; //導入依賴的package包/類
/**
*
*/
@Test
@SuppressWarnings(CompilerWarnings.UNUSED)
public void shouldRequireNonNullPredicate() {
    // given
    final ConcurrentMap<Integer, Boolean> cache = new ConcurrentHashMap<>();
    final IntPredicate predicate = null;
    final IntFunction<Integer> keyFunction = Integer::valueOf;

    // when
    thrown.expect(NullPointerException.class);
    thrown.expectMessage("Cannot memoize a NULL Predicate - provide an actual Predicate to fix this.");

    // then
    new ConcurrentMapBasedIntPredicateMemoizer<>(cache, keyFunction, predicate);
}
 
開發者ID:sebhoss,項目名稱:memoization.java,代碼行數:19,代碼來源:ConcurrentMapBasedIntPredicateMemoizerTest.java

示例8: shouldTestGivenValue

import java.util.function.IntPredicate; //導入依賴的package包/類
/**
*
*/
@Test
public void shouldTestGivenValue() {
    // given
    final IntPredicate predicate = Mockito.mock(IntPredicate.class);
    final IntFunction<String> keyFunction = a -> "key";
    final Cache<String, Boolean> cache = CacheBuilder.newBuilder().build();

    // when
    final GuavaCacheBasedIntPredicateMemoizer<String> memoizer = new GuavaCacheBasedIntPredicateMemoizer<>(
            cache, keyFunction, predicate);

    // then
    memoizer.test(123);
    Mockito.verify(predicate).test(123);
}
 
開發者ID:sebhoss,項目名稱:memoization.java,代碼行數:19,代碼來源:GuavaCacheBasedIntPredicateMemoizerTest.java

示例9: scanAllVersion

import java.util.function.IntPredicate; //導入依賴的package包/類
void scanAllVersion(IntPredicate predicate) {
    SkipListScanner scanner = this.slist.scan(0, true, 0, true);
    if (scanner == null) {
        return;
    }
    while (scanner.next()) {
        long pHead = scanner.getValuePointer();
        if (pHead == 0) {
            continue;
        }
        int oHead = Unsafe.getInt(pHead);
        if (oHead == 0) {
            continue;
        }
        for (ListNode i=ListNode.create(this.base, oHead); i!=null; i=i.getNextNode()) {
            boolean cont = predicate.test(i.getOffset());
            if (!cont) {
                return;
            }
        }
    }
}
 
開發者ID:waterguo,項目名稱:antsdb,代碼行數:23,代碼來源:MemTablet.java

示例10: assertIntPredicates

import java.util.function.IntPredicate; //導入依賴的package包/類
private void assertIntPredicates(Supplier<IntStream> source, Kind kind, IntPredicate[] predicates, boolean... answers) {
    for (int i = 0; i < predicates.length; i++) {
        setContext("i", i);
        boolean match = intKinds.get(kind).apply(predicates[i]).apply(source.get());
        assertEquals(answers[i], match, kind.toString() + predicates[i].toString());
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:8,代碼來源:MatchOpTest.java

示例11: testIntStream

import java.util.function.IntPredicate; //導入依賴的package包/類
@Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class)
public void testIntStream(String name, TestData.OfInt data) {
    for (IntPredicate p : INT_PREDICATES) {
        setContext("p", p);
        for (Kind kind : Kind.values()) {
            setContext("kind", kind);
            exerciseTerminalOps(data, intKinds.get(kind).apply(p));
            exerciseTerminalOps(data, s -> s.filter(ipFalse), intKinds.get(kind).apply(p));
            exerciseTerminalOps(data, s -> s.filter(ipEven), intKinds.get(kind).apply(p));
        }
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:13,代碼來源:MatchOpTest.java

示例12: checkAndSetOrder

import java.util.function.IntPredicate; //導入依賴的package包/類
private void checkAndSetOrder(IntPredicate expectedValue,
                              IntUnaryOperator newValue) {
    if (!expectedValue.test(invocationOrder)) {
        throw new TestSupport.AssertionFailedException(
                expectedValue + " -> " + newValue);
    }
    invocationOrder = newValue.applyAsInt(invocationOrder);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:9,代碼來源:ReaderTest.java

示例13: should_verify_an_actual_int_predicate_is_conform_to_an_expected_result

import java.util.function.IntPredicate; //導入依賴的package包/類
@Test
public void should_verify_an_actual_int_predicate_is_conform_to_an_expected_result() {
    assertThat(resultOf(() -> {
        gwtMock.whenAnEventHappensInRelationToAnActionOfTheConsumer();
        return (IntPredicate) number -> number < 100;
    }).accepts(99)).hasSameClassAs(assertThat((IntPredicate) t -> t == 0));
}
 
開發者ID:xapn,項目名稱:test-as-you-think,代碼行數:8,代碼來源:ResultOfEventTest.java

示例14: RangeOfInts

import java.util.function.IntPredicate; //導入依賴的package包/類
/**
 * Constructor
 * @param start     the start for range, inclusive
 * @param end       the end for range, exclusive
 * @param step      the step increment
 * @param excludes  optional predicate to exclude elements in this range
 */
RangeOfInts(int start, int end, int step, IntPredicate excludes) {
    super(start, end);
    this.start = start;
    this.end = end;
    this.step = step;
    this.ascend = start <= end;
    this.excludes = excludes;
}
 
開發者ID:zavtech,項目名稱:morpheus-core,代碼行數:16,代碼來源:RangeOfInts.java

示例15: filterNumbersWithPredicate

import java.util.function.IntPredicate; //導入依賴的package包/類
public List<Integer> filterNumbersWithPredicate(final List<Integer> numbers,
    IntPredicate predicate) {
    List<Integer> filteredNumbers = new ArrayList<>();
    for (Integer number : numbers) {
        if (predicate.test(number)) {
            filteredNumbers.add(number);
        }
    }
    return filteredNumbers; 
}
 
開發者ID:PacktPublishing,項目名稱:Java-9-with-JShell,代碼行數:11,代碼來源:example12_02.java


注:本文中的java.util.function.IntPredicate類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。