函数式编程+比较器

@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("========================\n"); } public static void main(String[] args) { printArr(create()); } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zygzpf.html