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();
}
public Hero(String vcName) {
this.vcName = vcName;
}
public Hero(String vcName, float floHp, int intDamage) {
this.vcName = vcName;
this.floHp = floHp;
this.intDamage = intDamage;
}
@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);
}
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);
HeroCheck c2 = (Hero hero) -> {
return hero.getFloHp()>100 && hero.getIntDamage()<50;
};
HeroCheck c3 = (Hero hero) ->hero.getFloHp()>100 && hero.getIntDamage()<50;
HeroCheck c4 = hero -> hero.getFloHp()>100 && hero.getIntDamage()<50;
filter(heros, c4);
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);
}
}
}
}