Lambda + Arrays---小练习

题目

按照要求进行排序

定义数组并存储一些学生对象,利用 Arrays 中的 sort 方法进行排序

要求 1:属性有姓名、年龄、身高。

要求 2:按照年龄的大小进行排序,年龄一样,按照身高排序,身高一样按照姓名的字母进行排序。

思路

利用之前提到的Arrays.sort底层逻辑来实现排序:自定义对象数组的多级排序,按照年龄、身高、姓名依次排序,注意:在sort方法中,o1-o2是升序、o2-o1是降序,因为sort底层逻辑是插入排序+二分查找,拿未排序数组的元素和已排序数组的元素进行比较,若o1-o2是正数表示当前插入元素是大的,放在已排好序数组元素的后面,0表示插入元素和现在比是一样的,也放在后面,负数表示当前插入元素是小的,放在前面

代码

这里给大家写了两种方式:匿名内部类、Lambda

对象类:

java 复制代码
public class Student {
    private String name;
    private int age;
    private double height;

    public Student() {
    }

    public Student(String name, int age, double height) {
        this.name = name;
        this.age = age;
        this.height = height;
    }

    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 double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", height=" + height +
                '}';
    }
}

实现类:

java 复制代码
import java.util.Arrays;
import java.util.Comparator;

public class test1 {
    public static void main(String[] args) {
        
        //compareTo方法来比较字符串大小
        Student gf1=new Student("z",18,1.68);
        Student gf2=new Student("y",18,1.69);
        Student gf3=new Student("l",19,1.70);

        Student[] arr={gf1,gf2,gf3};

        Arrays.sort(arr, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                //按照年龄大小排序,年龄一样按身高排序,身高一样按照姓名的字母排序
                double temp=o1.getAge()- o2.getAge();
                temp=temp==0?o1.getHeight()-o2.getHeight():temp;
                temp=temp==0?o1.getName().compareTo(o2.getName()):temp;

               
                if(temp>0){
                    return 1;
                }else if(temp<0){
                    return -1;
                }else{
                    return 0;
                }
            }
        });

        //Lambda表达式
        Arrays.sort(arr, (Student o1, Student o2)->{
                //按照年龄大小排序,年龄一样按身高排序,身高一样按照姓名的字母排序
                double temp=o1.getAge()- o2.getAge();
                temp=temp==0?o1.getHeight()-o2.getHeight():temp;
                temp=temp==0?o1.getName().compareTo(o2.getName()):temp;

                if(temp>0){
                    return 1;
                }else if(temp<0){
                    return -1;
                }else{
                    return 0;
                }
            }
        );

        //Lambda极简表达式
        Arrays.sort(arr, ( o1, o2)->{
                    //按照年龄大小排序,年龄一样按身高排序,身高一样按照姓名的字母排序
                    double temp=o1.getAge()- o2.getAge();
                    temp=temp==0?o1.getHeight()-o2.getHeight():temp;
                    temp=temp==0?o1.getName().compareTo(o2.getName()):temp;

               
                    if(temp>0){
                        return 1;
                    }else if(temp<0){
                        return -1;
                    }else{
                        return 0;
                    }
                }
        );

        System.out.println(Arrays.toString(arr));
    }
}
相关推荐
不知名的老吴7 小时前
案例教学:最长递增子序列问题
数据结构·算法·动态规划
样例过了就是过了7 小时前
LeetCode热题100 杨辉三角
c++·算法·leetcode·动态规划
念越7 小时前
算法每日一题 Day05|双指针解决盛最多水的容器问题
算法·力扣
_小草鱼_7 小时前
【数据结构】栈和队列
数据结构·数组··队列
eggrall7 小时前
Leetcode 最大连续 1 的个数 III(medium)
算法·leetcode·职场和发展
啊我不会诶7 小时前
Educational Codeforces Round 120 (Rated for Div. 2) vp补题
c++·算法
贾斯汀玛尔斯8 小时前
每天学一个算法--图算法(Graph Algorithms)
数据结构·算法
埃伊蟹黄面8 小时前
C++ —— 智能指针
开发语言·c++·算法
董董灿是个攻城狮8 小时前
马斯克在用炸火箭的方式训练 AGI。。。
算法
Pentane.8 小时前
【力扣hot100】【Leetcode 54】螺旋矩阵|边界控制 算法笔记及打卡(19/100)
算法·leetcode·矩阵