• Java8新特性


    1. 接口默认方法和静态方法

    1. public interface Inter1 {
    2. default void one() {
    3. System.out.println("接口默认方法");
    4. }
    5. static void two() {
    6. System.out.println("接口静态方法");
    7. }
    8. }
    9. public interface Inter2 {
    10. default void one() {
    11. System.out.println("接口默认方法2");
    12. }
    13. }
    14. public class Father {
    15. public void one() {
    16. System.out.println("父类方法");
    17. }
    18. }
    19. public class Son extends Father implements Inter1{
    20. }
    21. public class Son2 implements Inter1,Inter2{
    22. @Override
    23. public void one() {
    24. System.out.println("子类重写接口默认方法,解决接口冲突");
    25. }
    26. }
    27. public class Demo1 {
    28. public static void main(String[] args) {
    29. Inter1 s = new Son();
    30. s.one();
    31. Inter1.two();
    32. }
    33. }

    Son继承Father,同时实现Inter1

    必须是先写基础,然后再写实现

    接口和父类中有同名同参数默认方法,默认相当于父类重写了接口中的方法。

    若子类实现了两个接口,两个接口有同名同参数默认方法,这就需要解决接口冲突,子类必须重写。

    2. Lambda表达式

    Lambda允许把函数方法作为方法参数,即行为参数化,也是面向函数编程。

    Lambda表达式可以用来简化匿名内部类的写法,搭配函数式接口可以让代码更加美观。

    2.1 函数式接口

    接口中只有一个抽象方法。

    用@Functionallnterface来标注函数式接口。

    1. /*函数式接口,必须为接口,里面只能有一个抽象方法*/
    2. @FunctionalInterface
    3. public interface Swimming {
    4. void swim();
    5. }
    6. //Lambda表达式来简化匿名内部类的写法,搭配函数式接口
    7. public class LambdaDemo {
    8. public static void main(String[] args) {
    9. Swimming s1 = new Swimming() {
    10. @Override
    11. public void swim() {
    12. System.out.println("s1运行了");
    13. }
    14. };
    15. s1.swim();
    16. //Lambda表达式法
    17. Swimming s2 = ()->System.out.println("s2运行了");
    18. s2.swim();
    19. }
    20. }

    2.1.1 常见函数式接口

    Supplier:生产型接口,T get()方法

    Consumer:消费型接口,void accept(T t)

    Predicate:判断型接口,boolean test(T t)

    Function:功能型接口,R apply(T t)

    2.2 方法引用

    用来简化Lambda表达式,在MyBatis-Plus中就有应用。

    四种方法引用

    • 引用静态方法: ContainingClass::staticMethodName
    • 引用某个对象的实例方法: containingObject::instanceMethodName
    • 引用某个类型的任意对象的实例方法:ContainingType::methodName
    • 引用构造方法: ClassName::new

    2.3 三种方式对比

    1. public class CompareBYData {
    2. public static int ageDesc(Student s1,Student s2) {
    3. return s1.getAge()-s2.getAge();
    4. }
    5. }
    6. public static void main(String[] args) {
    7. Student s1 = new Student("zs",21);
    8. Student s2 = new Student("ls",18);
    9. Student s3 = new Student("ww",33);
    10. Student[] ss = {s1,s2,s3};
    11. System.out.println(Arrays.toString(ss));
    12. //匿名内部类
    13. Arrays.sort(ss, new Comparator() {
    14. @Override
    15. public int compare(Student o1, Student o2) {
    16. return o1.getAge()-o2.getAge();
    17. }
    18. });
    19. System.out.println(Arrays.toString(ss));
    20. System.out.println("----------");
    21. //Lambda
    22. Arrays.sort(ss,(o1, o2) -> o2.getAge()- o1.getAge());
    23. System.out.println(Arrays.toString(ss));
    24. //方法引用
    25. Arrays.sort(ss,CompareBYData::ageDesc);
    26. System.out.println(Arrays.toString(ss));
    27. }

    3. Stream流

    用于对集合和数组进行便捷操作。

    3.1 获取Stream流

    1. 如何获取List集合的Stream流?
    2. 如何获取Set集合的Stream流?
    3. 如何获取Map集合的Stream流?
    4. 如何获取数组的Stream流? 
    1. /**
    2. * 目标:掌握Stream流的创建。
    3. */
    4. public class StreamTest2 {
    5. public static void main(String[] args) {
    6. // 1、如何获取List集合的Stream流?
    7. List names = new ArrayList<>();
    8. Collections.addAll(names, "张三丰","张无忌","周芷若","赵敏","张强");
    9. Stream stream = names.stream();
    10. // 2、如何获取Set集合的Stream流?
    11. Set set = new HashSet<>();
    12. Collections.addAll(set, "刘德华","张曼玉","蜘蛛精","马德","德玛西亚");
    13. Stream stream1 = set.stream();
    14. stream1.filter(s -> s.contains("德")).forEach(s -> System.out.println(s));
    15. // 3、如何获取Map集合的Stream流?
    16. Map map = new HashMap<>();
    17. map.put("古力娜扎", 172.3);
    18. map.put("迪丽热巴", 168.3);
    19. map.put("马尔扎哈", 166.3);
    20. map.put("卡尔扎巴", 168.3);
    21. Set keys = map.keySet();
    22. Stream ks = keys.stream();
    23. Collection values = map.values();
    24. Stream vs = values.stream();
    25. Set> entries = map.entrySet();
    26. Stream> kvs = entries.stream();
    27. kvs.filter(e -> e.getKey().contains("巴"))
    28. .forEach(e -> System.out.println(e.getKey()+ "-->" + e.getValue()));
    29. // 4、如何获取数组的Stream流?
    30. String[] names2 = {"张翠山", "东方不败", "唐大山", "独孤求败"};
    31. Stream s1 = Arrays.stream(names2);
    32. Stream s2 = Stream.of(names2);
    33. }
    34. }

    3.2 Stream流中间方法

    中间方法指的是:调用完方法之后其结果是一个新的Stream流,于是可以继续调用方法,这样一来就可以支持链式编程(或者叫流式编程)。

    3.3 Stream流的终结方法 

    其他的后续补上。 

  • 相关阅读:
    document.activeELement和antdesign-useForm
    CrossOver2023mac切换win双系统虚拟机
    STM8的C语言编程(3)+――+GPIO输出
    Redis深度历险
    Android 播放mp3文件
    antv系列图引擎X6、G6比对选择,并实现vue实例ER图
    OpenLDAP 自助修改密码系统——筑梦之路
    Qt中使用QDomDocument和QDomnode来读取xml
    Android图片涂鸦,Kotlin(1)
    C语言:求一个字符串里面有效个数
  • 原文地址:https://blog.csdn.net/weixin_45734473/article/details/132715665