jdk1.8新特性
(1)lambda表达式(函数式接口 @FunctionalInterface) --- > 取代 匿名内部类 如 Runnable/Comparator
(2)stream 流式计算,链式编程
Lambda表达式的基础语法:
->:称为箭头操作符,或者lambda操作符,箭头操作符把表达式拆分成两份
左侧:表达式的参数列表
右侧:表达式需要执行的功能, 即Lambda体
JAVA内置的四大函数式接口
Customer<T> 消费型接口
void accept( T t);
Supplier( T t ) 供给型接口
T get();
Function<T, R>:函数型接口
R apply(T t);
Predicate<T>:断言型接口
boolean test(T t);
demo举例(工作中常用)
// @FunctionalInterface Runnable 函数式接口 Runnable runnable = new Runnable() { @Override public void run() { } }; Thread thread = new Thread(new Runnable() { @Override public void run() { } }, "线程A"); thread.start(); Thread thread2 = new Thread(() -> { System.out.println("AAA"); }, "线程A"); thread2.start(); // luma表达式出现的目的就是替换匿名内部类 Animal animal = (name) -> { System.out.println(name); return name; }; animal = new Animal() { @Override public String say(String name) { return null; } }; System.out.println(animal.say("aaa")); List<String> aaList = Arrays.asList("1", "2", "3"); aaList.forEach(new Consumer<String>() { @Override public void accept(String s) { System.out.println(s); } }); aaList.forEach((s) -> { System.out.println(s); }); /*===================================流式编程,链式开发========================================================*/ List<Person> perList = Arrays.asList(new Person(12, "aaa", "2020-04-07", "nanjing"), new Person(12, "aaa", "2020-04-01", "nanjing"), new Person(12, "aaa", "2020-04-02", "nanjing"), new Person(12, "aaa", "2020-04-03", "nanjing"), new Person(12, "aaa", "2020-04-04", "nanjing"), new Person(13, "bbb", "2020-04-05", "beijing"), new Person(14, "aaa", "2020-04-06", "nanjing")); List<Person> perLists = new ArrayList<Person>() { { add(new Person(12, "aaa", "2020-04-01", "nanjing")); add(new Person(12, "aaa", "2020-04-02", "nanjing")); add(new Person(13, "bbb", "2020-04-03", "beijing")); add(new Person(14, "aaa", "2020-04-04", "nanjing")); } }; //(1)求最小值 perLists.stream().min(new Comparator<Person>() { @Override public int compare(Person o1, Person o2) { return 0; } }); System.out.println("年龄最小值;" + perLists.stream().min((o1, o2) -> { return o1.getAge() - o2.getAge(); } )); // 年龄最小值;Optional[Person{age=12, name='aaa', birth='2020-04-01', pro='nanjing'}] //(2)求最大值 System.out.println("年龄最大值对象;" + perLists.stream().max((p1, p2) -> { return p1.getAge() - p2.getAge(); }).get()); // 年龄最大值;Person{age=14, name='aaa', birth='2020-04-04', pro='nanjing'} //(3)过滤 12岁的年纪 perList.stream().filter(new Predicate<Person>() { @Override public boolean test(Person person) { return person.getAge() == 12; } }); List<Person> resList = perList.stream().filter((person) -> { return person.getAge() == 12; }).collect(Collectors.toList()); System.out.println("过滤 12岁的年纪" + resList); /* 过滤 12岁的年纪[Person{age=12, name='aaa', birth='2020-04-01', pro='nanjing'}, Person{age=12, name='aaa', birth='2020-04-02', pro='nanjing'}, Person{age=12, name='aaa', birth='2020-04-02', pro='nanjing'}, Person{age=12, name='aaa', birth='2020-04-02', pro='nanjing'}, Person{age=12, name='aaa', birth='2020-04-02', pro='nanjing'}]*/ //(4)limit 过滤12岁的年纪,取前5条 resList = perList.stream().filter((person) -> { return person.getAge() == 12; }).limit(2).collect(Collectors.toList()); System.out.println("过滤 12岁的年纪==取前两条记录:" + resList); // 过滤 12岁的年纪==取前两条记录:[Person{age=12, name='aaa', birth='2020-04-01', pro='nanjing'}, Person{age=12, name='aaa', birth='2020-04-02', pro='nanjing'}] //(5)sorted 排序后循环 foreach perList.stream().sorted(new Comparator<Person>() { @Override public int compare(Person o1, Person o2) { return 0; } }).forEach(new Consumer<Person>() { @Override public void accept(Person person) { } }); perList.stream().sorted(new Comparator<Person>() { @Override public int compare(Person o1, Person o2) { return 0; } }).forEach(new Consumer<Person>() { @Override public void accept(Person person) { } }); perList.stream().sorted((p1, p2) -> { return p1.getAge() - p2.getAge(); }).forEach((person) ->{ System.out.println(person); }); /*Person{age=12, name='aaa', birth='2020-04-01', pro='nanjing'} Person{age=12, name='aaa', birth='2020-04-02', pro='nanjing'} Person{age=12, name='aaa', birth='2020-04-02', pro='nanjing'} Person{age=12, name='aaa', birth='2020-04-02', pro='nanjing'} Person{age=12, name='aaa', birth='2020-04-02', pro='nanjing'} Person{age=13, name='bbb', birth='2020-04-03', pro='beijing'} Person{age=14, name='aaa', birth='2020-04-04', pro='nanjing'}*/ //(6)map 归类:结果一般是一组数据 perList.stream().map(new Function<Person, Object>() { @Override public Object apply(Person person) { return person.getName(); } }).collect(Collectors.toList()); System.out.println(perList.stream().map((x) -> { return x.getName(); }).collect(Collectors.toList())); // [aaa, aaa, aaa, aaa, aaa, bbb, aaa] List<Integer> a = perList.stream().map((person) -> { return person.getAge(); }).map((x) -> { return x-10; }).collect(Collectors.toList()); List<Integer> b = perList.stream().map((person) -> { return person.getAge(); }).collect(Collectors.toList()); // 7 ) reduce: 用来计算结果,结果是一个数值 Integer ageTotal = perList.stream().map(person -> person.getAge()).reduce(0, new BinaryOperator<Integer>() { @Override public Integer apply(Integer x, Integer y) { return x + y; } }); ageTotal = perList.stream().map(person -> person.getAge()).reduce(0, ( x, y) -> x + y); //求总和,0是基准 System.out.println("ageTotal=======" + ageTotal); //ageTotal=======87 Integer maxNum = perList.stream().map(person -> person.getAge()).reduce(0, (x, y) -> Integer.max(x, y));//求最大值 System.out.println("maxNum=======" + maxNum); // maxNum=======14 // 7) collect : 将处理后的结果输出到新的集合中,如list,set,map等 ;或者返回处理的结果,如求集合的个数,平均值等 //Collectors 计数 Long count = perList.stream().filter(new Predicate<Person>() { @Override public boolean test(Person person) { return person.getAge() == 12; } }).collect(Collectors.counting()); //Collectors set集合 System.out.println(perList.stream().map(new Function<Person, Object>() { @Override public Object apply(Person person) { return person.getName(); } }).collect(Collectors.toSet())); //Collectors 求和 Integer totalNum = perList.stream().collect(Collectors.summingInt(new ToIntFunction<Person>() { @Override public int applyAsInt(Person value) { return value.getAge(); } })); System.out.println("totalNum : " + totalNum); // totalNum : 87 // 8) map(A::getName).distinct : 取其中某一元素然后去重 //取出List集合对象中某个属性集合(方法一) -- 去重distinct List<String> nameList = perList.stream().map(Person::getName).distinct().collect(Collectors.toList()); System.out.println("nameList" + nameList); // nameList[aaa, bbb] //取出List集合对象中某个属性集合(方法二) List<String> aa = perList.stream().map(person -> person.getName()).collect(Collectors.toList()); System.out.println("nameList" + nameList); // nameList[aaa, bbb] // 7) toMap //取出List集合对象Object中某个属性 转换成 Map<String, Object> Map<String, Person> map = perList.stream().collect(Collectors.toMap(Person::getBirth, ps->ps)); System.out.println("map" + map); //map{2020-04-01=Person{age=12, name='aaa', birth='2020-04-01', pro='nanjing'}, 2020-04-03=Person{age=12, // name='aaa', birth='2020-04-03', pro='nanjing'}, 2020-04-02=Person{age=12, name='aaa', birth='2020-04-02', // pro='nanjing'}, 2020-04-05=Person{age=13, name='bbb', birth='2020-04-05', pro='beijing'}, // 2020-04-04=Person{age=12, name='aaa', birth='2020-04-04', pro='nanjing'}, 2020-04-07=Person{age=12, // name='aaa', birth='2020-04-07', pro='nanjing'}, 2020-04-06=Person{age=14, name='aaa', birth='2020-04-06', // pro='nanjing'}} //key=name, value = key对应的对象;注意key重复会抛出异常 // Map<String, Person> personMap1 = perList.stream().collect(Collectors.toMap(Person::getName, Function.identity())); //key=name, value = key对应的对象; key重复, 取新的key Map<String, Person> personMapByNew = perList.stream().collect(Collectors.toMap(Person::getName, Function.identity(),(oldValue, newValue)-> newValue)); System.out.println("personMapByNew====" + personMapByNew); //personMapByNew===={aaa=Person{age=14, name='aaa', birth='2020-04-06', pro='nanjing'}, bbb=Person{age=13, // name='bbb', birth='2020-04-05', pro='beijing'}} //key=name, value = key对应的对象; key重复, 取旧的key Map<String, Person> personMapByOld = perList.stream().collect(Collectors.toMap(Person::getName, Function.identity(), (oldValue, newValue)-> oldValue)); System.out.println("personMapByOld=====" + personMapByOld); //personMapByOld====={aaa=Person{age=14, name='aaa', birth='2020-04-06', pro='nanjing'}, bbb=Person{age=13, // name='bbb', birth='2020-04-05', pro='beijing'}} perList.stream().filter(person -> person.getAge() == 12).limit(3) .collect(Collectors.toMap(Person::getBirth, Person::getName)).forEach((key, value) -> { System.out.println(key + "============" +value); }); /*2020-04-01============aaa 2020-04-02============aaa 2020-04-07============aaa*/ //(9) Optional // Optional的主要用处是可以有效的避免NullPointerException,配合lambda可以减少代码中大量的空指针判断,总之,代码更加优雅。 Optional<Class<Person>> personClass = Optional.ofNullable(Person.class); Person ps = new Person(15, "eee", "2020-02-10", ""); ps.setPro(Optional.ofNullable(ps.getPro()).orElse("yyy")); if (StrUtil.isNotBlank(Optional.ofNullable(ps).map(Person::getName).orElse(null))) { System.out.println("success1"); } if (StrUtil.isNotBlank(Optional.ofNullable(ps).map(person -> person.getName()).orElse(null))) { System.out.println("success2"); }