zoukankan      html  css  js  c++  java
  • 函数式编程+比较器

    比较器

    @FunctionalInterface
    public interface Comparator<T> {

        int compare(T o1, T o2);

    }

    @FunctionalInterface  指的是功能性函数接口,里面只有一个方法。    

    对于Comparable接口来说,它往往是进行比较类需要实现的接口,它仅包含一个有compareTo()方法,只有一个参数,返回值为int,返回值大于0表示对象大于参数对象;小于0表示对象小于参数对象;等于0表示两者相等

    public class Demo {
        public static class Student {
            public String name;
            public int id;
            public int age;
    
            public Student(String name, int id, int age) {
                this.name = name;
                this.id = id;
                this.age = age;
            }
            @Override
            public String toString() {
                return "Name : " + this.name + ", Id : " + this.id + ", Age : " + this.age;
            }
        }
        
        public static Student[] create(){
            return new Student[] { 
                    new Student("A", 1, 23),
                    new Student("B", 2, 21),
                    new Student("C", 2, 20),
                    new Student("E", 2, 19),
                    new Student("D", 2, 29),
                    new Student("F", 1, 24)
            };
        }
        
        public static void printArr(Student[] array){
            for (int i = 0; i < array.length; i++) {
                System.out.println(array[i]);
            }
            System.out.println("========================
    ");
        }
    
        public static void main(String[] args) {
            printArr(create());
        }
    }

    基础类Student被create()实例化为数组形式,通过printArr打印Student的信息。

    比较器

    在Demo中实现Comparator接口

        public static class AgeDescendingComparator implements Comparator<Student> {
    
            @Override
            public int compare(Student o1, Student o2) {
                return o2.age - o1.age;
            }
    
        }

    然后就是在main方法中引用,

        public static void main(String[] args) {
            Student[] students = create();
            printArr(students);
            Arrays.sort(students, new AgeDescendingComparator());
            printArr(students);
        }

    结果为学生年龄降序排列:

    Name : A, Id : 1, Age : 23
    Name : B, Id : 2, Age : 21
    Name : C, Id : 2, Age : 20
    Name : E, Id : 2, Age : 19
    Name : D, Id : 2, Age : 29
    Name : F, Id : 1, Age : 24
    ========================

    Name : D, Id : 2, Age : 29
    Name : F, Id : 1, Age : 24
    Name : A, Id : 1, Age : 23
    Name : B, Id : 2, Age : 21
    Name : C, Id : 2, Age : 20
    Name : E, Id : 2, Age : 19
    ========================

    函数值编程

    但是每次都要自定义类实现comparator接口,过于繁杂,有关函数式接口的其他应用,网上有很多。

    在main中

    Comparator<Student> nameAort = (st1,st2) -> {return st1.name.compareTo(st2.name);};

    在构建以年龄排序的小根堆

            PriorityQueue<Student> heap = new PriorityQueue<>(nameAort);// 小根堆
            for (int i = 0; i < students.length; i++) {
                heap.add(students[i]);
            }
            while (!heap.isEmpty()) {
                Student student = heap.poll();
                System.out.println(student);
            }

    现在到底结果

    Name : A, Id : 1, Age : 23
    Name : B, Id : 2, Age : 21
    Name : C, Id : 2, Age : 20
    Name : D, Id : 2, Age : 29
    Name : E, Id : 2, Age : 19
    Name : F, Id : 1, Age : 24

  • 相关阅读:
    模板库
    LCT小结
    BZOJ1502: [NOI2005]月下柠檬树
    技术资料分享
    ORM介紹及ORM優點、缺點
    依賴注入入門——Unity(二)
    面向切面編程入門(一)
    依賴注入入門——Unity(一)
    WCF、.Net Remoting、Web Service概念及区别
    深入设计模式(四)——建造者模式
  • 原文地址:https://www.cnblogs.com/fengxilee/p/9552214.html
Copyright © 2011-2022 走看看