栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java Lambda 匹配 anyMatch allMatch noneMatch

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java Lambda 匹配 anyMatch allMatch noneMatch

Java Lambda 匹配 anyMatch allMatch noneMatch

public class Menu {
	
	
	private String name;
	
	private Double price;
	
	private Double kilo;
	
	private String type;
	
	public Menu() {
		
	}

	public Menu(String name, Double price, Double kilo, String type) {
		super();
		this.name = name;
		this.price = price;
		this.kilo = kilo;
		this.type = type;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Double getPrice() {
		return price;
	}

	public void setPrice(Double price) {
		this.price = price;
	}

	public Double getKilo() {
		return kilo;
	}

	public void setKilo(Double kilo) {
		this.kilo = kilo;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	
	
}
Menu pork    = new Menu("猪肉", 9.9, 10.0, "肉类");
Menu beef    = new Menu("牛肉", 38.8, 5.0, "肉类");
Menu chicken = new Menu("鸡肉", 6.5, 30.0, "肉类");
Menu tomato  = new Menu("土豆", 3.5, 30.0, "蔬菜");
Menu potato  = new Menu("西红柿", 7.5, 20.0, "蔬菜");
Menu apple   = new Menu("苹果", 3.5, 20.0, "水果");
Menu orange  = new Menu("橙子", 4.0, 20.0, "水果");
List menuList = Arrays.asList(pork, beef, chicken, tomato, potato, apple, orange);

anyMatch检查谓词是否至少匹配一个元素

boolean isVegetables = menuList.stream().anyMatch(menu -> menu.getType().equals("蔬菜"));
System.out.println(isVegetables);
true

allMatch检查谓词是否匹配所有元素

boolean isAllVegetables = menuList.stream().allMatch(menu -> menu.getType().equals("蔬菜"));
System.out.println(isAllVegetables);
false

noneMatch确保流中没有任何元素与给定的谓词匹配 

boolean isNoneDrink = menuList.stream().noneMatch(menu -> menu.getType().equals("酒水"));
System.out.println(isNoneAllVegetables);

 

true

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/758854.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号