- 认识函数式接口
@FunctionalInterface
public interface Lamdbatest {
Integer getdouble(Integer s);
}
- Lamdba表达式
lamdba可以实现函数式接口创建出一个对象,
lamdba允许把一个函数作为一个方法的参数,或是将一个方法赋值给一个属性
-实现类实现接口
public class impllamdba implements Lamdbatest {
@Override
public Integer getdouble(Integer s) {
return s+=s;
}
}
-- 测试和lamdba表达式的区别
//创建出函数式接口的是实现类对象
Lamdbatest impllamdba = new impllamdba();
//通过对象使用::符号来调用,返回property对象通过apply方法来实现
Property<Integer, Integer> getdouble1 = impllamdba::getdouble;
System.out.println(getdouble1.apply(2));
//调用实现类来调用方法
System.out.println("这是函数式接口实现类使用"+impllamdba.getdouble(5));
//通过lamdba表达式来是实现接口
Integer getdouble = ((Lamdbatest) (s) -> s += s).getdouble(2);
System.out.println("这个是lamdba表达式使用实现"+getdouble);
通过上述可以看出lamdba表达式可以实现函数式接口不用在自定义实现类了,可以自定义实现,
如果有实现类可以创建对象使用::来调用方法
- stream流的使用
- 什么是stream?
一般是对集合进行使用,可以把集合中的元素看做是一种流,在流的过程中可以通过stream api来对元素进行操作
数组和集合都可以创建成流,流的操作分为两种;
1 中间操作 每次操作返回一个新的流,
2 终端操作 操作一次就无法再次操作了 - stream的特点
1 不会存储数据
2 不会改变数据源
3 有延迟性只有调用终端操作时中间操作才会执行 - 创建流
Collection.stream()方法来创建顺序流
Collection.parallelStream() 创建并行流
Arrays.stream(new array[]) 数组变成流
- stream中的静态方法
1 of()
2 iterate()
3 generate()
都可以返回成流
- stream和parallelStream的区分
1 stream是顺序流,由主线程处理
2 parallelStream是并行流,多线程运行处理 - Optional是对象容器
- 示例
public static void main(String[] args) {
List<Person> personList = new ArrayList<Person>();
personList.add(new Person("Tom", 8900,4, "boy", "New York"));
personList.add(new Person("Jack", 7000, 5,"boy", "Washington"));
personList.add(new Person("Lily", 7800, 6,"girl", "Washington"));
personList.add(new Person("Anni", 8200, 8,"girl", "New York"));
personList.add(new Person("Owen", 9500, 8,"boy", "New York"));
personList.add(new Person("Alisa", 7900,9, "girl", "New York"));
//遍历
personList.stream().forEach(System.out::println);
//filter过滤 符合条件的元素查询 只看女生
personList.stream().filter(s->"female".equals(s.getSex())).forEach(System.out::println);
// 去重 分页 跳过
long count = personList.stream().skip(5).count();
System.out.println(count);
personList.stream().distinct();
personList.stream().limit(1).forEach(System.out::println);
//查找 findfirst() 查找第一个 findany任意一个
personList.stream().filter(s->"female".equals(s.getSex())).findFirst();
personList.stream().filter(s->"female".equals(s.getSex())).findAny();
// 聚合操作 min max count
Integer integer = Stream.of(1, 2, 3, 4).min(Integer::compareTo).get();
System.out.println(integer);
Optional<Person> max = personList.stream().max(Comparator.comparingInt(Person::getAge));
System.out.println(max.get());
//映射map,对数据进行处理 返回的是处理后的结果 会变成一个新的流
List<Integer> collect = personList.stream().map(s -> s.getSalary() + 1000).collect(Collectors.toList());
System.out.println(collect);
//reduce归约 将一个流变成一个值,实现对集合的求和乘积和边界
Integer integer1 = personList.stream().map(s -> s.getAge()).reduce((x, y) -> x + y).get();
System.out.println("输出求和"+integer1);
Integer integer2 = personList.stream().map(s -> s.getAge()).reduce(Integer::sum).get();
System.out.println("输出求和"+integer2);
Integer integer3 = personList.stream().map(s -> s.getAge()).reduce((x, y) -> x > y ? x : y).get();
System.out.println("求最大值"+integer3);
Integer integer4 = personList.stream().map(s -> s.getAge()).reduce((x, y) -> x * y).get();
System.out.println("求乘积"+integer4);
//收集collect就是把流收集起来可以收集成一个值或是一个集合
//归集 tolist/toSet/toMap 因为流是不存储数据的,所以要将处理后的数据流重新放到一个新的集合中
//查询女生年龄小于等于5
personList.stream().filter(s -> s.getAge() <= 5).collect(Collectors.toList()).forEach(System.out::println);
//统计 collectors提供了一系列用于计算的静态方法
Long collect1 = personList.stream().collect(Collectors.counting());
System.out.println("求总数"+collect1);
Double collect2 = personList.stream().collect(Collectors.averagingDouble(Person::getSalary));
System.out.println("求平均数"+collect2);
long count1 = personList.stream().map(s -> s.getSalary()).count();
System.out.println(count1);
//分组groupingBy将集合分成多个map
Map<String, List<Person>> group = personList.stream().collect(Collectors.groupingBy(Person::getSex));
System.out.println("分组");
System.out.println("输出男孩"+group.get("boy"));
System.out.println("输出女孩"+group.get("girl"));
//拼接joining 使用指定的符号来连接返回一个字符串
String collect3 = personList.stream().map(s -> s.getSex()).collect(Collectors.joining("-手拉手-"));
System.out.println("连接符号=="+collect3);
//排序
List<Person> collect4 = personList.stream().sorted(Comparator.comparingInt(Person::getAge)).collect(Collectors.toList());
System.out.println("排序年龄"+collect4);
List<Person> collect5 = personList.stream().sorted(Comparator.comparingInt(Person::getAge).reversed()).collect(Collectors.toList());
System.out.println("倒序年龄"+collect5);
}