- public interface Inter1 {
- default void one() {
- System.out.println("接口默认方法");
- }
-
- static void two() {
- System.out.println("接口静态方法");
- }
- }
-
- public interface Inter2 {
- default void one() {
- System.out.println("接口默认方法2");
- }
- }
-
-
- public class Father {
- public void one() {
- System.out.println("父类方法");
- }
- }
-
- public class Son extends Father implements Inter1{
-
- }
-
- public class Son2 implements Inter1,Inter2{
- @Override
- public void one() {
- System.out.println("子类重写接口默认方法,解决接口冲突");
- }
- }
-
-
- public class Demo1 {
- public static void main(String[] args) {
- Inter1 s = new Son();
- s.one();
- Inter1.two();
- }
- }
Son继承Father,同时实现Inter1
必须是先写基础,然后再写实现
接口和父类中有同名同参数默认方法,默认相当于父类重写了接口中的方法。
若子类实现了两个接口,两个接口有同名同参数默认方法,这就需要解决接口冲突,子类必须重写。
Lambda允许把函数方法作为方法参数,即行为参数化,也是面向函数编程。
Lambda表达式可以用来简化匿名内部类的写法,搭配函数式接口可以让代码更加美观。
接口中只有一个抽象方法。
用@Functionallnterface来标注函数式接口。
- /*函数式接口,必须为接口,里面只能有一个抽象方法*/
- @FunctionalInterface
- public interface Swimming {
- void swim();
- }
-
- //Lambda表达式来简化匿名内部类的写法,搭配函数式接口
- public class LambdaDemo {
- public static void main(String[] args) {
- Swimming s1 = new Swimming() {
- @Override
- public void swim() {
- System.out.println("s1运行了");
- }
- };
- s1.swim();
-
- //Lambda表达式法
- Swimming s2 = ()->System.out.println("s2运行了");
- s2.swim();
- }
- }
Supplier
Consumer
Predicate
Function
用来简化Lambda表达式,在MyBatis-Plus中就有应用。
四种方法引用
- public class CompareBYData {
-
- public static int ageDesc(Student s1,Student s2) {
- return s1.getAge()-s2.getAge();
- }
- }
-
-
-
- public static void main(String[] args) {
-
- Student s1 = new Student("zs",21);
- Student s2 = new Student("ls",18);
- Student s3 = new Student("ww",33);
-
- Student[] ss = {s1,s2,s3};
- System.out.println(Arrays.toString(ss));
-
- //匿名内部类
- Arrays.sort(ss, new Comparator
() { - @Override
- public int compare(Student o1, Student o2) {
- return o1.getAge()-o2.getAge();
- }
- });
- System.out.println(Arrays.toString(ss));
-
- System.out.println("----------");
-
- //Lambda
- Arrays.sort(ss,(o1, o2) -> o2.getAge()- o1.getAge());
- System.out.println(Arrays.toString(ss));
-
-
- //方法引用
- Arrays.sort(ss,CompareBYData::ageDesc);
- System.out.println(Arrays.toString(ss));
-
- }
用于对集合和数组进行便捷操作。
- /**
- * 目标:掌握Stream流的创建。
- */
- public class StreamTest2 {
- public static void main(String[] args) {
- // 1、如何获取List集合的Stream流?
- List
names = new ArrayList<>(); - Collections.addAll(names, "张三丰","张无忌","周芷若","赵敏","张强");
- Stream
stream = names.stream(); -
- // 2、如何获取Set集合的Stream流?
- Set
set = new HashSet<>(); - Collections.addAll(set, "刘德华","张曼玉","蜘蛛精","马德","德玛西亚");
- Stream
stream1 = set.stream(); - stream1.filter(s -> s.contains("德")).forEach(s -> System.out.println(s));
-
- // 3、如何获取Map集合的Stream流?
- Map
map = new HashMap<>(); - map.put("古力娜扎", 172.3);
- map.put("迪丽热巴", 168.3);
- map.put("马尔扎哈", 166.3);
- map.put("卡尔扎巴", 168.3);
-
- Set
keys = map.keySet(); - Stream
ks = keys.stream(); -
- Collection
values = map.values(); - Stream
vs = values.stream(); -
- Set
> entries = map.entrySet(); - Stream
> kvs = entries.stream(); - kvs.filter(e -> e.getKey().contains("巴"))
- .forEach(e -> System.out.println(e.getKey()+ "-->" + e.getValue()));
-
- // 4、如何获取数组的Stream流?
- String[] names2 = {"张翠山", "东方不败", "唐大山", "独孤求败"};
- Stream
s1 = Arrays.stream(names2); - Stream
s2 = Stream.of(names2); - }
- }
中间方法指的是:调用完方法之后其结果是一个新的Stream流,于是可以继续调用方法,这样一来就可以支持链式编程(或者叫流式编程)。
其他的后续补上。