1、 lambda简介
Lambda 表达式是 JDK8 的一个新特性,可以取代大部分的匿名内部类,写出更优雅的 Java 代码,尤其在集合的遍历和其他集合操作中,可以极大地优化代码结构。 JDK 也提供了大量的内置函数式接口供我们使用,使得 Lambda 表达式的运用更加方便、高效。
2、 lambda表达式使用
@FunctionalInterface
@FunctionalInterface
public interface PersonService {
String getData(String data);
}
public class PersonServiceTest {
public static void main(String[] args) {
PersonService personService = (x) -> {
System.out.println(x);
x = x + ":aaa";
return x;
};
String result = personService.getData("aa");
System.out.println(result);
}
}
Consumer使用
public class ConsumerDemo {
public static void main(String[] args) {
Consumer<String> consumer = (x) -> {
x = x + ":aa";
System.out.println(x);
};
consumer.accept("aaa");
}
}
Supplier使用
public class SupplierDemo {
public static void main(String[] args) {
Supplier<String> supplier = () -> {
System.out.println("===========================");
return "success";
};
String result = supplier.get();
System.out.println(result);
}
}
Function使用
public class FunctionDemo {
public static void main(String[] args) {
Function<String, String> function = (x) -> {
x = x + ":aa";
System.out.println(x);
return x;
};
String result = function.apply("aa");
System.out.println(result);
}
}
filter使用
public class FilterDemo {
public static void main(String[] args) {
List<Integer> list = List.of(1, 2, 3, 4, 5, 6);
List<Integer> integerList = list.stream().filter(e -> e % 2 == 0).collect(Collectors.toList());
System.out.println(integerList);
}
}
map使用
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Map1Test {
public static void main(String[] args) {
List<Integer> integerList = new ArrayList<>(10);
integerList.add(1);
integerList.add(2);
integerList.add(3);
List<Integer> list = integerList.stream().map(e -> e + 1).collect(Collectors.toList());
System.out.println(list);
}
}
allMatch使用
public class AllMatchDemo {
public static void main(String[] args) {
List<String> strList = Arrays.asList("a", "b", "c", "d");
boolean a = Optional.ofNullable(strList).orElseGet(ArrayList::new)
.stream()
.allMatch("a"::equals);
System.out.println("allMatch()测试多元素结果:" + a);
}
}
anyMatch使用
public class AnyMatchDemo {
public static void main(String[] args) {
List<String> strList = Arrays.asList("a", "b", "c", "d");
boolean a = Optional.ofNullable(strList).orElseGet(ArrayList::new)
.stream()
.anyMatch("a"::equals);
System.out.println("anyMatch()测试多元素结果:" + a);
}
}
flatMap使用
public class FlatMapTest {
public static void main(String[] args) {
List<Integer> list = List.of(1, 2, 3, 4, 5, 6);
List<Integer> list1 = List.of(7, 8, 9, 10, 11, 12);
List<List<Integer>> list2 = new ArrayList<>(10);
list2.add(list);
list2.add(list1);
List<Integer> list3 = list2.stream().flatMap(Collection::stream).collect(Collectors.toList());
System.out.println(list3);
}
}
List转Map
(1)对象定义
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private Long id;
private String name;
}
(2)程序转换
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class UserMapDemo {
public static void main(String[] args) {
List<User> userList = new ArrayList<>(10) {{
add(new User(1L, "aa"));
add(new User(2L, "bb"));
add(new User(3L, "cc"));
add(new User(4L, "dd"));
add(new User(5L, "ee"));
add(new User(6L, "ff"));
}};
Map<Long, User> map = userList.stream().collect(Collectors.toMap(User::getId, Function.identity(), (x, y) -> y));
System.out.println(map);
}
}
List转Map,value是数组
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class ListToMapListDemo {
public static void main(String[] args) {
List<User> userList = new ArrayList<>(10) {{
add(new User(1L, "aa"));
add(new User(2L, "bb"));
add(new User(3L, "cc"));
add(new User(4L, "dd"));
add(new User(5L, "ee"));
add(new User(6L, "aa"));
}};
Map<String, List<User>> map = userList.stream().collect(Collectors.groupingBy(User::getName, Collectors.toList()));
System.out.println(map);
}
}
总结
jdk8还提供了许多的方法,简化操作