您的位置:首页 > 编程语言 > Java开发

Lambdas表达式示例

2017-10-09 13:46 148 查看
代码如下:

import java.util.*;

public class Lambdas {
public static void main(String ...args){

// Simple example
Runnable r = () -> System.out.println("Hello!");
r.run();

// Filtering with lambdas
List<Apple> inventory = Arrays.asList(new Apple(80,"green"), new Apple(155, "green"), new Apple(120, "red"));

// [Apple{color='green', weight=80}, Apple{color='green', weight=155}]
List<Apple> greenApples = filter(inventory, (Apple a) -> "green".equals(a.getColor()));
System.out.println(greenApples);

Comparator<Apple> c = (Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight());

// [Apple{color='green', weight=80}, Apple{color='red', weight=120}, Apple{color='green', weight=155}]
inventory.sort(c);
System.out.println(inventory);
}

public static List<Apple> filter(List<Apple> inventory, ApplePredicate p){
List<Apple> result = new ArrayList<>();
for(Apple apple : inventory){
if(p.test(apple)){
result.add(apple);
}
}
return result;
}

public static class Apple {
private int weight = 0;
private String color = "";

public Apple(int weight, String color){
this.weight = weight;
this.color = color;
}

public Integer getWeight() {
return weight;
}

public void setWeight(Integer weight) {
this.weight = weight;
}

public String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}

public String toString() {
return "Apple{" +
"color='" + color + '\'' +
", weight=" + weight +
'}';
}
}

interface ApplePredicate{
public boolean test(Apple a);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息