1. java8
1.1 Lambda 表达式简化实现接口
注:只适用于函数式接口(就是一个有且仅有一个抽象方法,但是可以有多个非抽象方法的接口。)
(1) 基本语法
参数 -> 返回值|{处理代码}
参数: 参数在'()'中定义,单个参数可省略'()',参数可申明类型,也可不进行申明
返回值|{处理代码}: 不带'{}',为返回值及输出参数等;带'{}',则写代码块,返回值用return返回
(2) 简单实例
//无参
MyInterface1 myInterface1= () -> 1;
//一个参数
MyInterface2 myInterface2=(a) -> a;
MyInterface2 myInterface3= a -> a;//一个参数时可省略()
//多个参数
MyInterface3 myInterface4=(int a, int b) -> a + b;
MyInterface3 myInterface5=( a, b) -> a + b;
MyInterface3 myInterface6=( a, b) -> { return a + b;};
//无返回值
MyInterface4 myInterface7= (a) -> System.out.println(a);
(3) 简单实例
Arrays.sort(players, (String s1, String s2) -> (s1.compareTo(s2)));//数组排序
1.2 方法引用
(1) 分类
类型 | 语法 | 对应的Lambda表达式 |
---|---|---|
静态方法引用 | 类名::staticMethod | (args) -> 类名.staticMethod(args) |
实例方法引用 | inst::instMethod | (args) -> inst.instMethod(args) |
对象方法引用 | 类名::instMethod | (inst,args) -> 类名.instMethod(args) |
构建方法引用 | 类名::new | (args) -> new 类名(args) |
(2)简单示例
//引用Integer的compare()静态方法
List<Integer> list = Arrays.asList(82,22,34,50,9);
list.sort(Integer::compare);
// 实例方法引用,先创建实例再调用
User user = new User();
Supplier<String> supplier1 = () -> user.getName();
Supplier<Integer> supplier2 =user::getAge;
Integer integer = supplier2.get();
//对象方法引用,l类似 boolean test = a.method(b)方式调用
BiPredicate<String,String> bp1 = (x, y) -> x.equals(y);
BiPredicate<String,String> bp2 = String::equals;
boolean test = bp2.test("string1", "string2");
// 构造方法引用写法
Supplier<User> userSupplier1 = () -> new User();
Supplier<User> userSupplier2 = User::new;
User user2 = userSupplier2.get();
1.3 StreamAPI
1. 创建流API
stream() //创建流方法
2. 中间操作
filter((p) -> p> 5) //过滤操作(获取p>5的元素)
limit(n) //截断流,使其元素不超过给定数量
skip(n) //跳过元素,返回一个扔掉了前n个元素的流,若流中元素不足n个,则返回一个空流,与limit(n)互补
distinct() //筛选,通过流所生成元素的hashCode()和equals()去除重复元素。
map((p) -> {
NewBean newBean = new NewBean();
newBean.setFiled(p.getFiled());
return newBean;
}) //把实体字段映射到其他实体中
map(TestStreamAPI::change) //引用方法映射,返回值为流集合
//映射类及方法
public class TestStreamAPI {
public static Stream<Bean> change(Bean bean) {
List<NewBean> list = new ArrayList<>();
NewBean newBean=new NewBean();
newBean.setFiled(bean.getFiled)
list.add(newBean)
return list.stream();
}
}
flatMap(TestStreamAPI::change) //引用方法映射,返回值为一个集合流
sorted() //自然排序
sorted((a,b)->a.compareTo(b)) //定制排序
3. 终止操作
count() //获取集合元素数量
forEach(System.out::println) //遍历输出
allMatch(p -> p > 1) //检查是否匹配所有元素(元素是否都大于1)
anyMatch(p -> p > 1) //检查是否没有匹配所有元素(是否有大于1的元素)
noneMatch(p -> p > 1) //检查是否没有匹配所有元素(是否没有大于1的元素)
findFirst() //返回第一个元素
findAny() //返回当前流中的任意元素
max((p1, p2) -> p1.compareTo(p2))//获取最大元素p1,p2可为Bean
min((p1, p2) -> p1.compareTo(p2))//获取最小元素p1,p2可为Bean
reduce(0, (x, y) -> x + y) //求和
map(Bean::getFiled).reduce(Integer::sum) // 求Bean集合中数字字段之和
map(Bean::getFiled).distinct().collect(Collectors.toList()); //Bean集合转字段集合(去重)
collect(Collectors.averagingInt(p -> p.getFiled())) //Bean中字段平均值
map(Bean::getFiled).collect(Collectors.maxBy(Integer::compareTo)) //Bean中字段最大值
map(Bean::getFiled).collect(Collectors.minBy(Integer::compareTo)) //Bean中字段最小值
1.4 应用
//list转Map
Map<Integer, Bean> map = list.stream().collect(Collectors.toMap(Bean::getFiled, a -> a,(k1,k2)->k1));
//list分组
Map<Integer, List<Bean>> groupBy = list.stream().collect(Collectors.groupingBy(Bean::getFiled));
//Bean集合转字段集合(去重)
List<String> list=list.stream().map(Bean::getFiled).distinct().collect(Collectors.toList());