首页 > 编程语言 >java8 stream匹配 anyMatch,allMatch,noneMatch

java8 stream匹配 anyMatch,allMatch,noneMatch

时间:2023-05-29 10:35:48浏览次数:36  
标签:anyMatch stream noneMatch list System allMatch println

anyMatch:判断的条件里,任意一个元素成功,返回true

allMatch:判断条件里的元素,所有的都是,返回true

noneMatch:与allMatch相反,判断条件里的元素,所有的都不是,返回true

count方法,跟List接口中的 .size() 一样,返回的都是这个集合流的元素的长度,不同的是,流是集合的一个高级工厂,中间操作是工厂里的每一道工序,我们对这个流操作完成后,可以进行元素的数量的和;

举例:

public static void main(String[] args) {
 list = Arrays.asList(1, 2, 1, 1, 1);
    
    boolean anyMatch = list.stream().anyMatch(f -> f == (1));
    boolean allMatch = list.stream().allMatch(f -> f == (1));
    boolean noneMatch = list.stream().noneMatch(f -> f == (1));
    long count = list.stream().filter(f -> f == (1)).count();
    
    System.out.println(anyMatch);  // true
    System.out.println(allMatch);  // false
    System.out.println(noneMatch); // false
    System.out.println(count);     // 4
}


标签:anyMatch,stream,noneMatch,list,System,allMatch,println
From: https://blog.51cto.com/chengzheng183/6368173

相关文章

  • StreamLink
    Usagecat>docker-compose.yml<<-'EOF'#https://hub.docker.com/r/chigusa/bililive-go#Port:7150version:"3"services:streamlink:image:chigusa/bililive-gorestart:alwaysports:-7150:8080volu......
  • C++ write batch files via filstream
    #include<assert.h>#include<atomic>#include<chrono>#include<fstream>#include<iomanip>#include<iostream>#include<mutex>#include<numeric>#include<thread>#include<unistd.h>#includ......
  • centos stream 安装JDK Development Kit 20.0.1
    在最新版本centosstream9安装 JDKDevelopmentKit20.0.1我选择安装最新版本、等2023年9月份出来 JDKDevelopmentKit21后、再体验一把。在官网直接下载rpm包、当然也可以选择其它的包、根据实际情况选用下载包到本地后、再上传到服务端、用rpm-ivhjdk-20_linux-x64......
  • java函数式编程stream流操作lambda表达式使用方法引用用法等练习
    java函数式编程stream流操作lambda表达式使用方法引用用法等练习 @Testvoidtest01(){System.out.println("111");List<Author>authors=getAuthor();//stream流打对象中一个字段authors.stream().distinct().forEach(author......
  • 【Java基础】Java8 使用 stream().filter()过滤List对象(查找符合条件的对象集合)
    本篇主要说明在Java8及以上版本中,使用stream().filter()来过滤List对象,查找符合条件的集合。一、集合对象定义集合对象以学生类(Student)为例,有学生的基本信息,包括:姓名,性别,年龄,身高,生日几项。我的学生类代码如下:packagecom.iot.productmanual.controller;importio.swagger.annota......
  • Stream流体系
    视频地址https://www.bilibili.com/video/BV1Cv411372m?1Stream流概述目的:简化集合和数组操作的API,结合了Lambda表达式。Stream流式思想的核心:先得到集合或者数组的Stream流(就是一根传送带)把元素放上去用这个Stream流简化的API来方便的操作元素2Stream流获取......
  • Java的Stream流的分页,Stream的skip和limit实现分页
    1、工具类packagecom.cc.testproject.utils;importcom.github.pagehelper.PageInfo;importorg.springframework.stereotype.Component;importjava.util.List;importjava.util.stream.Collectors;/**List分页工具类*@authorCC*@since2022/2/16**/@Compon......
  • 学习笔记-Java8新特性-第四节-StreamAPI
    StreamAPI利用StreamAPI可以像流水线一样操作处理数据源(数组、集合……)Stream自己不会存储元素Stream不会改变数据源,而是会返回一个持有处理结构的新StreamSteam操作时延迟执行的,他们会等到需要结果的时候才执行称为惰性求值Stream操作的三个步骤创建Stream......
  • java list.stream 多条件去重(分组)
    List<EmEventConfigPointExcelDto>listNew=list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()->newTreeSet<>(Comparator.comparing((o)-......
  • rabbitmq:pika.exceptions.IncompatibleProtocolError: StreamLostError: ('Transport
    本地连接rabbitmq出现这个问题: 是因为我把port写成了15672,改成5672即可 ......