博客内容是学习Java8实战书籍后进行总结。
1 解决过滤list的办法
//过滤的行为接口
public interface Predicate<T>{
boolean test(T t);
}
//过滤
public static <T> List<T> filter(List<T> list, Predicate<T> p){
List<T> result = new ArrayList<>();
for(T e: list){
if(p.test(e)){
result.add(e);
}
}
return result;
}
//调用
//过滤苹果,根据苹果颜色进行过滤
List<Apple> redApples =
filter(inventory, (Apple apple) -> "red".equals(apple.getColor()));
//过滤数字,过滤保留偶数
List<Integer> evenNumbers =
filter(numbers, (Integer i) -> i % 2 == 0);
2 Lambda
2.1 概念
先前:
Comparator<Apple> byWeight = new Comparator<Apple>() {
public int compare(Apple a1, Apple a2){
return a1.getWeight().compareTo(a2.getWeight());
}
};
之后(用了Lambda表达式):
Comparator<Apple> byWeight =
(Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight());
Lambda表达式有三个部分:
-
参数列表——这里它采用了Comparator中compare方法的参数,两个Apple。
-
箭头——箭头->把参数列表与Lambda主体分隔开。
-
Lambda主体——比较两个Apple的重量。表达式就是Lambda的返回值了。
2.2 Java 8中有效的Lambda表达式
//第一个Lambda表达式具有一个String类型的参数并返回一个int。Lambda没有return语句,因为已经隐含了return
(String s) -> s.length()
(Apple a) -> a.getWeight() > 150
//第三个Lambda表达式具有两个int类型的参数而没有返回值(void返回)。注意Lambda表达式可以包含多行语句,这里是两行
(int x, int y) -> {
System.out.println("Result:");
System.out.println(x+y);
}
//第四个Lambda表达式没有参数, 返回一个int
() -> 42
//第五个Lambda表达式具有两个Apple类型的参数,返回一个int:比较两个Apple的重量
(Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight())
2.3 在哪里以及如何使用Lambda
在函数式接口上使用Lambda表达式
函数式接口:只定义一个抽象方法的接口
签名:() -> void代表了参数列表为空,且返回void的函数。 是Runnable接口所代表的
表达式签名和接口的抽象方法的参数和返回值一致
2.4 简单使用
用Comparator 来排序
public class Sort {
public static void main(String[] args) {
List<Apple> inventory = Arrays.asList(new Apple(80,"green"), new Apple(155, "green"), new Apple(120, "red"));
//排序
inventory.sort(new Comparator<Apple>() {
@Override
public int compare(Apple o1, Apple o2) {
return compareTo(o1.getWeight(),o2.getWeight());
}
});
System.out.println();
//Lambda
inventory.sort((Apple a1,Apple a2) -> compareTo(a1.getWeight(),a2.getWeight()));
System.out.println();
}
public static int compareTo(int o1,int o2){
if (o1 < o2) {
return 1;
} else if (o1 > o2) {
return -1;
} else {
return 0;
}
}
}
用Runnable 执行代码块
public class ThreadTest {
public static void main(String[] args) {
Thread t = new Thread(new Runnable() {
public void run(){
System.out.println("Hello world");
}
});
//Lambda
Thread t2 = new Thread(() -> System.out.println("Hello world"));
}
}
GUI 事件处理
Button button = new Button("Send");
button.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
label.setText("Sent!!");
}
});
//Lambda
button.setOnAction((ActionEvent event) -> label.setText("Sent!!"));
2.5 环绕执行模式
public class Demo4 {
public static String processFile() throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader("data.txt"))) {
return br.readLine();
}
}
public static String processFile(BufferedReaderProcessor p) throws IOException {
try (BufferedReader br =
new BufferedReader(new FileReader("data.txt"))) {
return p.process(br);
}
}
public static void main(String[] args) throws IOException {
String oneLine = processFile((BufferedReader br) -> br.readLine());
String twoLines = processFile((BufferedReader br) -> br.readLine() + br.readLine());
}
}
@FunctionalInterface
public interface BufferedReaderProcessor {
String process(BufferedReader b) throws IOException;
}
3 常用使用的Lambda
3.1 排序
//对list<bean>的排序 List<Apple> inventory;
//1
inventory.sort((Apple a1,Apple a2) -> compareTo(a1.getWeight(),a2.getWeight()));
//2 方法引用
inventory.sort(Comparator.comparing(Apple::getWeight));
public static int compareTo(int o1,int o2){
if (o1 < o2) {
return 1;
} else if (o1 > o2) {
return -1;
} else {
return 0;
}
}
// 逆序
inventory.sort(Comparator.comparing(Apple::getWeight).reversed());
//比较器链 如果重量一样,根据颜色排序
inventory.sort(Comparator.comparing(Apple::getWeight).reversed().thenComparing(Apple::getColor));
//谓词复合
//3.忽略大小写排序
List<String> str = Arrays.asList("a","b","A","B");
str.sort((s1, s2) -> s1.compareToIgnoreCase(s2));
str.sort(String::compareToIgnoreCase);