Java集合排序

2021-04-22  本文已影响0人  媛猿YY

集合排序

整型数据排序

public static void main(String[] args) {
//        对存储在list中的整型数据进行排序
        List<Integer> list = new ArrayList<Integer>();
        list.add(5);
        list.add(9);
        list.add(3);
        list.add(1);
        System.out.println("排序前:");
        for (int n : list) {
            System.out.println(n + " ");
        }
        System.out.println();
//        对list中的数据进行排序
        Collections.sort(list);
        System.out.println("排序后");
        for (int n : list) {
            System.out.println(n + " ");
        }
    }

字符串排序

public static void main(String[] args) {
//        对存放在list中的字符串进行排序,按照ASCII码进行排序
        List<String> list=new ArrayList<String>();
        list.add("orange");
        list.add("blue");
        list.add("yellow");
        list.add("gray");
        System.out.println("排序之前:");
        for(String s:list){
            System.out.println(s+" ");
        }
        System.out.println();
        Collections.sort(list);
        System.out.println("排序后:");
        for(String s:list){
            System.out.println(s +" ");
        }
    }

自定义对象排序

  1. Comparator接口

实现思路:
先定义个排序的Comparator接口,定义排序的规则是什么,在调用Collections.sort()

public static void main(String[] args){
//        按名字升序;排序
        Cat huahua=new Cat("huahua",2,"英短");
        Cat fanfan=new Cat("fanfan",3,"田园猫");
        Cat maomao=new Cat("maomao",4,"田园猫");
        List<Cat> catList = new ArrayList<Cat>();
        catList.add(huahua);
        catList.add(fanfan);
        catList.add(maomao);
//        排序前
        System.out.println("按名字排序前:");
        for(Cat cat:catList){
            System.out.println(cat);
        }
        //  按名字升序排序
        Collections.sort(catList,new NameComparator());
        //        排序后
        System.out.println("按名字升序排序后:");
        for(Cat cat:catList){
            System.out.println(cat);
        }

//        按年龄进行降序排序
        Collections.sort(catList,new AgeComparator());
        //        排序后
        System.out.println("按年龄降序排序后:");
        for(Cat cat:catList){
            System.out.println(cat);
        }
    }

AgeComparator.java

package com.example.javabase.jihe.jihepaixu;

import java.util.Comparator;

public class AgeComparator implements Comparator<Cat> {
    @Override
    public int compare(Cat o1, Cat o2) {
//        按年龄降序排序
        int age1 = o1.getMonth();
        int age2 = o2.getMonth();
        return age2-age1;
    }
}

NameComparator.java

package com.example.javabase.jihe.jihepaixu;

import java.util.Comparator;

public class NameComparator implements Comparator<Cat> {
    @Override
    public int compare(Cat o1, Cat o2) {
        String name1 = o1.getName();
        String name2 = o2.getName();
        //        按名字升序排序
        int n = name1.compareTo(name2);
        //        按名字倒序排序
//        int n2 = name2.compareTo(name1);
        return n;
    }
}

  1. Comparable接口
package com.example.javabase.jihe.jihepaixu;

public class Goods implements Comparable<Goods> {
    private String id;//商品编号
    private String name;//商品名称
    private double price;//商品价格


    public Goods(String id, String name, double price) {
        this.id = id;
        this.name = name;
        this.price = price;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String toString() {
        return "商品编号:" + id + ",商品名称" + name + ",商品价格:" + price;
    }

    @Override
    public int compareTo(Goods o) {
//        取出商品价格
        double price1 = this.getPrice(); //当前的价格
        double price2 = o.getPrice();//当前对象的价格
        int n = new Double(price2 - price1).intValue();
        return n;
    }
}

package com.example.javabase.jihe.jihepaixu;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class GoodsTest {
    public static void main(String[] args) {
        Goods g1 = new Goods("s0001", "手机", 2000);
        Goods g2 = new Goods("s0002", "冰箱", 3000);
        Goods g3 = new Goods("s0003", "手机02", 4000);
        List<Goods> goodsList = new ArrayList<Goods>();
        goodsList.add(g1);
        goodsList.add(g2);
        goodsList.add(g3);
//        排序前
        System.out.println("排序前:");
        for (Goods goods : goodsList) {
            System.out.println(goods);
        }
        Collections.sort(goodsList);
        //        排序后
        System.out.println("排序后:");
        for (Goods goods : goodsList) {
            System.out.println(goods);
        }
    }
}

image.png
上一篇下一篇

猜你喜欢

热点阅读