首页 > 编程语言 >JAVA8 - 流 - 查找和匹配

JAVA8 - 流 - 查找和匹配

时间:2024-01-19 23:14:23浏览次数:31  
标签:匹配 name vegetarian public 查找 Dish new Type JAVA8

查找和匹配

Dish 类:

package com.demo3;

public class Dish {

    private final  String name;
    private final boolean vegetarian; //素食注意
    private final int calories; 
    private final Type type;

    public Dish(String name, boolean vegetarian, int calories, Type type) {
        this.name = name;
        this.vegetarian = vegetarian;
        this.calories = calories;
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public boolean isVegetarian() {
        return vegetarian;
    }

    public int getCalories() {
        return calories;
    }

    public Type getType() {
        return type;
    }

    @Override
    public String toString() {
        return name;
    }

    public enum  Type {MEAT, FISH, OTHER}

}

anyMatch、allMatch、noneMatch 这三个操作都用到了我们所谓的短路,这就是大家熟悉的JAVA中 && 和 || 运算符短路在流中的版本

package com.demo3;

import java.util.Arrays;
import java.util.List;

public class Test1 {

    public  static List<Dish> menu = Arrays.asList(
            new Dish("pork",false,800,Dish.Type.MEAT),
            new Dish("beef",false,700,Dish.Type.MEAT),
            new Dish("chicken",false,400,Dish.Type.MEAT),
            new Dish("french fries",true,530,Dish.Type.OTHER),
            new Dish("rice",true,350,Dish.Type.OTHER),
            new Dish("reason fruit",true,120,Dish.Type.OTHER)
    );

    public static void main(String[] args) {
        test1();
    }
    
    public static void test1(){
        //检查谓词是否至少匹配一个元素
        if (menu.stream().anyMatch(Dish::isVegetarian)){
            System.out.println("The menu is (somewhat) vegetarian friendly!!");
        }

        //检查谓词是否匹配所有元素
        boolean b = menu.stream().anyMatch(d -> d.getCalories() < 1000);
        System.out.println(b);  //ture


        boolean b1 = menu.stream().noneMatch(d -> d.getCalories() >= 1000);
        System.out.println(b1); //true
    }
}

标签:匹配,name,vegetarian,public,查找,Dish,new,Type,JAVA8
From: https://www.cnblogs.com/czzz/p/17975818

相关文章

  • ABAP根据事务码查找增强程序
    *&---------------------------------------------------------------------**&ReportZ_FIND_ENHANCEMENT*&---------------------------------------------------------------------**&*&-------------------------------------------------------......
  • Java开发之Java8 新特性--流式数据处理学习
    一.流式处理简介在我接触到java8流式处理的时候,我的第一感觉是流式处理让集合操作变得简洁了许多,通常我们需要多行代码才能完成的操作,借助于流式处理可以在一行中实现。比如我们希望对一个包含整数的集合中筛选出所有的偶数,并将其封装成为一个新的List返回,那么在java8之前,我们需......
  • oracle查找字符串中指定字符的位置
    INSTR(string,search_string,start_position,nth_appearance)其中:string是要查找的字符串。search_string是要查找的字符或字符串。start_position是开始查找的位置,默认为1。nth_appearance是要查找的字符或字符串在string中出现的次数,可以省略,默认为1......
  • 1978:扩号匹配问题C
    #include<stdio.h>intmain(){chars[101];while(scanf("%s",s)!=EOF){printf("%s\n",s);chartem[101];inta[101]={0};inttop=0;inti=0;for(;s[i]!='\0';i++){......
  • 关于linux系统查找规定时间点的文件和文件数(find)
    find./-mtime-5-typef-empty-execls-lt{}\;|morefindlinux查找命令find./当前目录下查找-mtime1表示文件修改时间距离当前为1天的文件,即距离当前时间1天(24小时-48小时)的文件-mtime0表示文件修改时间距离当前为0天的文件,即距离当前时间不到1天(24小时)以内的文......
  • 手机崩溃日志的查找与分析
    手机崩溃日志的查找与分析摘要本文介绍了一款名为克魔助手的iOS应用日志查看工具,该工具可以方便地查看iPhone设备上应用和系统运行时的实时日志和崩溃日志。同时还提供了崩溃日志的分析查看模块,可以对苹果崩溃日志进行符号化、格式化和分析,极大地简化了开发者的调试工作。引言......
  • 匹配统计
    这个是对\(f[i]\)数组的扩展,\(f[i]\)求的是\(A\)的后缀与\(B\)的前缀匹配的最大长度,而这道题目是求\(A\)的前缀与\(B\)的前缀匹配的最大长度这道题目有hash+二分的做法,比较好想,下面介绍KMP的做法我们先考虑一个暴力的做法,我们把\(B\)挨个挨个从\(A\)的某个位置开始进行匹配,看最......
  • 代码随想录算法训练营第一天| LeetCode704 二分查找,LeetCode35,LeetCode34,leetcode27.
    LeetCode704题目链接:704.二分查找-力扣(LeetCode)第一时间的想法:简单来说,二分法给我的印象就是想一条绳子上打很多的结,每次对折正好是一个结点,我们需要找到想要的结点比如(a)代码思路就是不断对折一直到绳子两端重合中间没有结点,最后剩下的就是要找的结点a了。......
  • 基础算法(三)二分查找---以“数的三次方”为例
    数的三次方根给定一个浮点数 n,求它的三次方根。输入格式共一行,包含一个浮点数 n。输出格式共一行,包含一个浮点数,表示问题的解。注意,结果保留 6 位小数。数据范围−10000≤n≤10000输入样例:1000.00输出样例:10.000000题解如下#include<iostream>usingnamespace......
  • python 在排序数组中查找元素的第一个和最后一个位置 多种解法
    二分查找:基于二分查找的算法可以在O(logn)的时间复杂度内解决该问题。具体实现方式是,先使用二分查找找到该元素的位置,然后向左和向右扩展,直到找到第一个和最后一个位置。代码如下:defsearchRange(nums,target):defbinarySearch(nums,target,lower):left,righ......