Lambda简单使用

/**
 * 所有想要具有比较功能的类,都建议实现这个接口 Comparable 此接口强行对实现它的每个类的对象进行整体排序。这种排序被称为类的自然排序,类的
 * compareTo 方法被称为它的自然比较方法。
 *
 */
public class Hero implements Comparable<Hero> {

	private String vcName;
	private float floHp;
	private int intDamage;

	public String getVcName() {
		return vcName;
	}

	public void setVcName(String vcName) {
		this.vcName = vcName;
	}

	public float getFloHp() {
		return floHp;
	}

	public void setFloHp(float floHp) {
		this.floHp = floHp;
	}

	public int getIntDamage() {
		return intDamage;
	}

	public void setIntDamage(int intDamage) {
		this.intDamage = intDamage;
	}
	
	

	public Hero() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	
	public Hero(String vcName) {
		this.vcName = vcName;
	}

	public Hero(String vcName, float floHp, int intDamage) {
		this.vcName = vcName;
		this.floHp = floHp;
		this.intDamage = intDamage;
	}

	/**
	 * compareTo(T o) 比较此对象与指定对象的顺序。O为要比较的对象
	 * 
	 */
	@Override
	public int compareTo(Hero anotherHero) {
		if (intDamage < anotherHero.getIntDamage())
			return 1;
		else
			return -1;
	}

	@Override
	public String toString() {
		return "Hero [vcName=" + vcName + ", floHp=" + floHp + ", intDamage=" + intDamage + "]\r\n";
	}
	
	
public interface HeroCheck {
	public boolean test(Hero hero);
}
/**
 *与匿名类相比,LAmbda就是匿名方法
 *弊端:
 *	1。可读性差,Lambda一旦变长,就难以理解
 *	2. 不便于调试,很难在Lambda表达式中增加调试信息,比如日志
 *	3.版本支持,Lambda在JDK8才支持, 
 *
 *不适合用在复杂的业务中
 *
 */
public class TestLambda {
	public static void main(String[] args) {
		Random random = new Random();
		List<Hero> heros = new ArrayList<Hero>();
		for (int i = 0; i < 10; i++) {
			heros.add(new Hero("hero" + i, random.nextInt(1000), random.nextInt(1000)));
		}
		System.out.println("初始化后的集合:");
		System.out.println(heros);
		
//		System.out.println("筛选出 hp>100 && damage<50 的英雄");
//		filter(heros);
		
//		System.out.println("使用匿名内部类的方式,筛选出 hp>100 && damage<50 的英雄");
//		HeroCheck heroCheck = new HeroCheck() {
//			@Override
//			public boolean test(Hero hero) {
//				return (hero.getFloHp() > 100 && hero.getIntDamage() < 50);
//			}
//		};
//		filter(heros, heroCheck);
		
		//从匿名内部类演化成Lambda表达式
		//只保留方法参数和方法体,参数和方法体之间加上符号
		HeroCheck c2 = (Hero hero) -> {
			return hero.getFloHp()>100 && hero.getIntDamage()<50;
		};
		
		//把return和{}去掉
		HeroCheck c3 = (Hero hero) ->hero.getFloHp()>100 && hero.getIntDamage()<50;
		
		//把参数类型和圆括号去掉(只有一个参数时,才可以去掉圆括号)
		HeroCheck c4 = hero -> hero.getFloHp()>100 && hero.getIntDamage()<50;
		
		//把c4作为参数传递进去
		filter(heros, c4);
		
		//直接把表达式代进去
		filter(heros, hero -> hero.getFloHp() > 100 && hero.getIntDamage() < 50);
		
//		System.out.println("使用Lambda的方式:筛选出 hp>100 && damage<50 的英雄 ");
//		filter(heros, hero->hero.getFloHp() > 100 && hero.getIntDamage() < 50);
		
	}
	
	//使用匿名内部类的方式
	private static void filter(List<Hero> heros, HeroCheck heroCheck) {
		for (Hero hero : heros) {
			if (heroCheck.test(hero)) {
				System.out.println(hero);
			}
		}
	}

//	private static void filter(List<Hero> heros) {
//		for (Hero hero : heros) {
//			if (hero.getFloHp() > 100 && hero.getIntDamage() < 50) {
//				System.out.println(hero);
//			}
//		}
//	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值