streams - Matching

本文探讨了Java Stream API中的三种Match操作:allMatch、anyMatch和noneMatch,这些操作用于确定流中的元素是否满足特定的谓词条件。通过实例演示了这些方法的短路特性,当结果可以确定时,它们会立即停止处理,从而提高效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

boolean allMatch(Predicate<? super T> predicate)

Returns whether all elements of this stream match the provided predicate. May not evaluate the predicate on all elements if not necessary for determining the result. If the stream is empty then true is returned and the predicate is not evaluated.

This is a short-circuiting terminal operation.

API Note:

This method evaluates the universal quantification of the predicate over the elements of the stream (for all x P(x)). If the stream is empty, the quantification is said to be vacuously satisfied and is always true (regardless of P(x)).

Parameters:

predicate - a non-interferingstateless predicate to apply to elements of this stream

Returns:

true if either all elements of the stream match the provided predicate or the stream is empty, otherwise false

 

anyMatch

boolean anyMatch(Predicate<? super T> predicate)

Returns whether any elements of this stream match the provided predicate. May not evaluate the predicate on all elements if not necessary for determining the result. If the stream is empty then false is returned and the predicate is not evaluated.

This is a short-circuiting terminal operation.

API Note:

This method evaluates the existential quantification of the predicate over the elements of the stream (for some x P(x)).

Parameters:

predicate - a non-interferingstateless predicate to apply to elements of this stream

Returns:

true if any elements of the stream match the provided predicate, otherwise false

 

noneMatch

boolean noneMatch(Predicate<? super T> predicate)

Returns whether no elements of this stream match the provided predicate. May not evaluate the predicate on all elements if not necessary for determining the result. If the stream is empty then true is returned and the predicate is not evaluated.

This is a short-circuiting terminal operation.

API Note:

This method evaluates the universal quantification of the negated predicate over the elements of the stream (for all x ~P(x)). If the stream is empty, the quantification is said to be vacuously satisfied and is always true, regardless of P(x).

Parameters:

predicate - a non-interferingstateless predicate to apply to elements of this stream

Returns:

true if either no elements of the stream match the provided predicate or the stream is empty, otherwise false

// streams/Matching.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Demonstrates short-circuiting of *Match() operations

import java.util.function.*;
import java.util.stream.*;

interface Matcher extends BiPredicate<Stream<Integer>, Predicate<Integer>> {}

public class Matching {
  static void show(Matcher match, int val) {
    System.out.println(
        match.test(
            IntStream.rangeClosed(1, 9).boxed().peek(n -> System.out.format("%d ", n)),
            n -> n < val));
  }

  public static void main(String[] args) {
    show(Stream::allMatch, 10);
    // show(Stream::allMatch, 6); // 1 2 3 4 5 6 false
    // show(Stream::allMatch, 5); // 1 2 3 4 5 false
    show(Stream::allMatch, 4);
    show(Stream::anyMatch, 2);
    show(Stream::anyMatch, 0);
    show(Stream::noneMatch, 5);
    show(Stream::noneMatch, 0);
  }
}
/* Output:
1 2 3 4 5 6 7 8 9 true
1 2 3 4 false
1 true
1 2 3 4 5 6 7 8 9 false
1 false
1 2 3 4 5 6 7 8 9 true
*/

references:

1. On Java 8 - Bruce Eckel

2. https://docs.oracle.com/javase/8/docs/api/java/util/function/BiPredicate.html#test-T-U-

3. https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#allMatch-java.util.function.Predicate-

4. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/streams/Matching.java

Build started: Project: STM32F103DIMIAN *** Using Compiler 'V5.06 update 7 (build 960)', folder: 'F:\KEIL\ARM\ARMCC\Bin' Build target 'STM32F103DIMIAN' compiling main.c... ../Core/Src/main.c(109): warning: #223-D: function "memset" declared implicitly memset(map, 0, sizeof(map)); ../Core/Src/main.c(127): warning: #223-D: function "printf" declared implicitly printf("起点位于障碍物上,无法开始\n"); ../Core/Src/main.c(131): warning: #223-D: function "printf" declared implicitly printf("起点: (%d,%d)\n", start.x, start.y); ../Core/Src/main.c(133): warning: #223-D: function "spiral_coverage" declared implicitly spiral_coverage(start); ../Core/Src/main.c(135): warning: #223-D: function "print_result" declared implicitly print_result(); ../Core/Src/main.c: 5 warnings, 0 errors compiling path_planning.c... linking... STM32F103DIMIAN\STM32F103DIMIAN.axf: Error: L6406E: No space in execution regions with .ANY selector matching path_planning.o(.bss). STM32F103DIMIAN\STM32F103DIMIAN.axf: Error: L6406E: No space in execution regions with .ANY selector matching startup_stm32f103xb.o(STACK). STM32F103DIMIAN\STM32F103DIMIAN.axf: Error: L6406E: No space in execution regions with .ANY selector matching startup_stm32f103xb.o(HEAP). STM32F103DIMIAN\STM32F103DIMIAN.axf: Error: L6406E: No space in execution regions with .ANY selector matching usart.o(.bss). STM32F103DIMIAN\STM32F103DIMIAN.axf: Error: L6406E: No space in execution regions with .ANY selector matching libspace.o(.bss). STM32F103DIMIAN\STM32F103DIMIAN.axf: Error: L6406E: No space in execution regions with .ANY selector matching stdio_streams.o(.bss). STM32F103DIMIAN\STM32F103DIMIAN.axf: Error: L6406E: No space in execution regions with .ANY selector matching stdio_streams.o(.bss). STM32F103DIMIAN\STM32F103DIMIAN.axf: Error: L6406E: No space in execution regions with .ANY selector matching stdio_streams.o(.bss). STM32F103DIMIAN\STM32F103DIMIAN.axf: Error: L6406E: No space in execution regions with .ANY selector matching path_planning.o(.data). STM32F103DIMIAN\STM32F103DIMIAN.axf: Error: L6406E: No space in execution regions with .ANY selector matching stm32f1xx_hal.o(.data). STM32F103DIMIAN\STM32F103DIMIAN.axf: Error: L6406E: No space in execution regions with .ANY selector matching system_stm32f1xx.o(.data). STM32F103DIMIAN\STM32F103DIMIAN.axf: Error: L6406E: No space in execution regions with .ANY selector matching stdio_streams.o(.data). STM32F103DIMIAN\STM32F103DIMIAN.axf: Error: L6406E: No space in execution regions with .ANY selector matching stdio_streams.o(.data). STM32F103DIMIAN\STM32F103DIMIAN.axf: Error: L6406E: No space in execution regions with .ANY selector matching stdio_streams.o(.data). STM32F103DIMIAN\STM32F103DIMIAN.axf: Error: L6407E: Sections of aggregate size 0x2793c bytes could not fit into .ANY selector(s). Not enough information to list image symbols. Not enough information to list load addresses in the image map. Finished: 2 information, 0 warning and 15 error messages. "STM32F103DIMIAN\STM32F103DIMIAN.axf" - 15 Error(s), 5 Warning(s). Target not created. Build Time Elapsed: 00:00:03
08-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值