美文网首页
JDK1.8方法引用

JDK1.8方法引用

作者: 快点给我想个名 | 来源:发表于2019-05-06 23:12 被阅读0次
    创建实体
    public class Student {
    
        private String name;
        private int age;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public Student(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public Student() {
        }
    
        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    
        public static int compareAge(Student s1,Student s2){
            return s1.getAge() - s2.getAge();
        }
    
        public static int compareName(Student s1,Student s2){
            return s1.getName().compareTo(s2.getName());
        }
    
        public int compareByAge(Student s1){
            return this.getAge() - s1.getAge();
        }
    
        public int compareByName(Student s1){
            return this.getName().compareTo(s1.getName());
        }
    
        public void sout(){
            System.out.println("sout");
        }
    
        public String returnName(){
            return this.getName();
        }
        
        public void soutTwo(Student s1){
            System.out.println(this);
            System.out.println(s1);
        }
    }
    
    
    工具类
    public class StudentCompareUtil {
    
        public int compareAge(Student s1, Student s2){
            return s1.getAge() - s2.getAge();
        }
    
        public int compareName(Student s1,Student s2){
            return s1.getName().compareTo(s2.getName());
        }
    }
    
    方法引用
    public class MethodReference {
    
        public static void main(String[] args) {
    
            List<Student> list = Arrays.asList(new Student("zhangsan",46),new Student("lisi",52),
                    new Student("wangwu",12),new Student("zhaoliu",99));
            System.out.println(list);
            /**
             * 方法引用:
             * 1、类名::静态方法名
             */
    
            list.sort((o1, o2) -> Student.compareAge(o1, o2));
            System.out.println(list);
    
            list.sort(Student::compareAge);
            System.out.println(list);
    
            /**
             * 方法引用
             * 2、引用名(对象的引用)::实例方法名
             */
    
            StudentCompareUtil util = new StudentCompareUtil();
    
            list.sort((o1, o2) -> util.compareName(o1, o2));
            System.out.println(list);
    
            list.sort(util::compareName);
            System.out.println(list);
    
             /**
             * 方法引用
             * 方法引用示例1、2相对来说还是比较容易理解,类似以前的:类名.静态方法名、引用名.实例方法名
             * 实例3 类名::实例方法名,相当于我们自定义方法来实现某个函数式接口
             *
             * 3、类名::实例方法名
             */
    
            list.sort(Student::compareByAge);
            System.out.println(list);
            Comparator<Student> compareByAge = Student::compareByAge;
            Consumer<Student> sout = Student::sout;
            Function<Student, String> returnName = Student::returnName;
            BiConsumer<Student, Student> soutTwo = Student::soutTwo;
            
            /**
             * 方法引用
             * 4、构造方法引用
             */
    
            MethodReference methodReference = new MethodReference();
            Student student = methodReference.createStudent(Student::new);
            System.out.println(student);
            
        }
    
        public Student createStudent(Supplier<Student> supplier){
            return supplier.get();
        }
    }
    

    相关文章

      网友评论

          本文标题:JDK1.8方法引用

          本文链接:https://www.haomeiwen.com/subject/pthpnqtx.html