将People对象存入Vector或ArrayList中按身高(某种属性)排序

一、People类属性Comparable接口

(1)People类重写compareTo方法。

(2)调用Collections.sort()方法排序。

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;

public class test{
    public static void main(String[] args) throws Exception {
        People person1 = new People("Alice", 25, 1.65);
        People person2 = new People("Bob", 30, 1.78);
        People person3 = new People("Charlie", 22, 1.70);
        People person4 = new People("David", 28, 1.75);
        People person5 = new People("Eva", 35, 1.68);
        People person6 = new People("Frank", 27, 1.80);
        People person7 = new People("Grace", 31, 1.72);
        People person8 = new People("Henry", 24, 1.68);
        People person9 = new People("Ivy", 29, 1.76);
        People person10 = new People("Jack", 33, 1.79);

        Vector<People> ve = new Vector<>();
        ArrayList<People> list = new ArrayList<>();

        ve.add(person1);
        ve.add(person2);
        ve.add(person3);
        ve.add(person4);
        ve.add(person5);
        ve.add(person6);
        ve.add(person7);
        ve.add(person8);
        ve.add(person9);
        ve.add(person10);
        
        for (int i=0;i<10;i++){
            list.add(ve.get(i));
        }
        
        System.out.println("排序前");
        
        for(int i=0;i<10;i++){
            System.out.println(ve.get(i));
        }
        System.out.println("-------------------");

        //(1)方法一
        Collections.sort(ve); 
        //Collections.sort(list); 

        System.out.println("排序后");
        for(int i=0;i<10;i++){
            System.out.println(ve.get(i));
        }
    }
}


//(1)方法一
class People implements Comparable<People>{
    String name;
    int age;
    double heigh;

    public People() {
    }

    public People(String name, int age, double heigh) {
        this.name = name;
        this.age = age;
        this.heigh = heigh;
    }

    //升序
    @Override
    public int compareTo(People o) {
        if(this.heigh-o.heigh>0){
            return 1;
        }else if(this.heigh-o.heigh<0){
            return -1;
        }
        return 0;
    }
    
     //降序
//    @Override
//    public int compareTo(People o) {
//        if(this.heigh-o.heigh>0){
//            return -1;
//        }else if(this.heigh-o.heigh<0){
//            return 1;
//        }
//        return 0;
//    }

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







二、Collections.sort()中第二参数new Comparator<People> 并重写compare方法

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;

public class test{
    public static void main(String[] args) throws Exception {
        People person1 = new People("Alice", 25, 1.65);
        People person2 = new People("Bob", 30, 1.78);
        People person3 = new People("Charlie", 22, 1.70);
        People person4 = new People("David", 28, 1.75);
        People person5 = new People("Eva", 35, 1.68);
        People person6 = new People("Frank", 27, 1.80);
        People person7 = new People("Grace", 31, 1.72);
        People person8 = new People("Henry", 24, 1.68);
        People person9 = new People("Ivy", 29, 1.76);
        People person10 = new People("Jack", 33, 1.79);
        Vector<People> ve = new Vector<>();
        ArrayList<People> list = new ArrayList<>();
        ve.add(person1);
        ve.add(person2);
        ve.add(person3);
        ve.add(person4);
        ve.add(person5);
        ve.add(person6);
        ve.add(person7);
        ve.add(person8);
        ve.add(person9);
        ve.add(person10);
        
        for (int i=0;i<10;i++){
            list.add(ve.get(i));
        }
        
        for(int i=0;i<10;i++){
            System.out.println(ve.get(i));
        }
        System.out.println("-------------------");


        //(2)方法二
        Collections.sort(ve, new Comparator<People>() {
            @Override
            public int compare(People o1, People o2) {
                if(o1.heigh-o2.heigh>0){
                    return 1;//升序
                }else if(o1.heigh-o2.heigh<0){
                    return -1;//降序
                }
                    return 0;
                }
        });
//        Collections.sort(list, new Comparator<People>() {
//            @Override
//            public int compare(People o1, People o2) {
//                if(o1.heigh-o2.heigh>0){
//                    return 1;
//                }else if(o1.heigh-o2.heigh<0){
//                    return -1;
//                }
//                return 0;
//            }
//        });

        for(int i=0;i<10;i++){
            System.out.println(ve.get(i));
        }
    }
}




//(2)方法二
class People{
    String name;
    int age;
    double heigh;

    public People() {
    }

    public People(String name, int age, double heigh) {
        this.name = name;
        this.age = age;
        this.heigh = heigh;
    }

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





提示:

以上比较身高是double类型,如果比较的是int类型重写的方法可以简化如下

1、重写方法

   @Override
    public int compareTo(People o) {
        return this.age-o.age;//升序
        //return o.age-this.age;//降序
    }
 @Override
public int compare(People o1, People o2) {
        return o1.age-o2.age;//升序
        //return o2.age-o1.age;//降序
}

2、Lambda表达式 

//Lambda表达式简化后
Collections.sort(ve, (o1,o2) ->{ return o1.age-o2.age;});//升序
Collections.sort(ve, (o1,o2) ->{ return o2.age-o1.age;});//降序

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

motu2

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值