streams - Matching

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

.\project.axf: Error: L6406E: No space in execution regions with .ANY selector matching main.o(.bss). .\project.axf: Error: L6406E: No space in execution regions with .ANY selector matching rtx_conf_cm.o(.bss). .\project.axf: Error: L6406E: No space in execution regions with .ANY selector matching startup_stm32f40_41xxx.o(STACK). .\project.axf: Error: L6406E: No space in execution regions with .ANY selector matching startup_stm32f40_41xxx.o(HEAP). .\project.axf: Error: L6406E: No space in execution regions with .ANY selector matching libspace.o(.bss). .\project.axf: Error: L6406E: No space in execution regions with .ANY selector matching stdio_streams.o(.bss). .\project.axf: Error: L6406E: No space in execution regions with .ANY selector matching stdio_streams.o(.bss). .\project.axf: Error: L6406E: No space in execution regions with .ANY selector matching stdio_streams.o(.bss). .\project.axf: Error: L6406E: No space in execution regions with .ANY selector matching as608.o(.bss). .\project.axf: Error: L6406E: No space in execution regions with .ANY selector matching rt_task.o(.bss). .\project.axf: Error: L6406E: No space in execution regions with .ANY selector matching rt_list.o(.bss). .\project.axf: Error: L6406E: No space in execution regions with .ANY selector matching rtx_conf_cm.o(.data). .\project.axf: Error: L6406E: No space in execution regions with .ANY selector matching system_stm32f4xx.o(.data). .\project.axf: Error: L6406E: No space in execution regions with .ANY selector matching keypad4x4.o(.data). .\project.axf: Error: L6406E: No space in execution regions with .ANY selector matching stm32f4xx_rcc.o(.data). .\project.axf: Error: L6406E: No space in execution regions with .ANY selector matching rt_cmsis.o(.data). .\project.axf: Error: L6406E: No space in execution regions with .ANY selector matching rt_task.o(.data). .\project.axf: Error: L6406E: No space in execution regions with .ANY selector matching rt_system.o(.data). .\project.axf: Error: L6406E:
07-25
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值