Java基础

Java 常用工具类--集合排序

2018-09-15  本文已影响0人  磊_5d71

集合排序


对整形数据进行排序

package com.alan.sort;

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

public class IntSort {

    public static void main(String[] args) {
        // 对存储在List中对整形数据进行排序
        // 定义泛型只能为对象,这里对int使用相应的装箱对象Integer,输入整数时会自动装箱
        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.print(n+" ");
        }
        System.out.println();
        //使用Collections类对数据进行排序
        Collections.sort(list);
        System.out.println("排序后:");
        for(int n:list) {
            System.out.print(n+" ");
        }

    }

}

对字符串进行排序

package com.alan.sort;

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

public class StringSort {

    public static void main(String[] args) {
        // 对存放在List中的字符串进行排序
        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.print(s + " ");
        }
        System.out.println();
        Collections.sort(list);
        System.out.println("排序后:");
        for (String s : list) {
            System.out.print(s + " ");
        }

    }

}

Comparator接口介绍


对自定义宠物猫类分别按名字升序、年龄降序进行排列

package com.alan.sort;

public class Cat {
    
    //属性:名字、年龄、品种
    private String name;
    private int month;
    private String species;
    
    //无参构造
    public Cat() {
        
    }
    //带参构造
    public Cat(String name, int month, String species) {
        this.name = name;
        this.month = month;
        this.species = species;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getMonth() {
        return month;
    }
    public void setMonth(int month) {
        this.month = month;
    }
    public String getSpecies() {
        return species;
    }
    public void setSpecies(String species) {
        this.species = species;
    }
    
    //由于使用了HashSet这里重写ToString方法
    @Override
    public String toString() {
        return "[名字:" + name + ", 年龄:" + month + ", 品种:" + species +"]";
    }
}

package com.alan.sort;

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);
        return n;
    }

}

package com.alan.sort;

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();
        //方法一:逻辑运算复杂
//      if (age1 > age2) {
//          return -1;
//      } else if (age1 < age2) {
//          return 1;
//      } else {
//          return 0;
//      }
        //方法二:
        return age2-age1;

    }

}

package com.alan.sort;

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

public class CatSort {

    public static void main(String[] args) {
        // 对宠物猫对象进行排序
        Cat huahua = new Cat("huahua", 2, "英国短毛猫");
        Cat fanfan = new Cat("fanfan", 5, "中华田园猫");
        Cat maomao = new Cat("maomao", 3, "中华田园猫");
        // 通过ArrayList存放Cat对象
        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中的Comparatror接口的compara方法。对其进行排序
        Collections.sort(catList, new NameComparator());
        System.out.println("按名字升序排序后:");
        for (Cat cat : catList) {
            System.out.println(cat);
        }
        
        //调用AgeComparator类,实现按年龄降序排序
        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);
        }
    }

}

Comparable接口

使用Comparable接口对商品价格进行降序排列

package com.alan.sort;

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;
    }


    @Override
    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();
        int n = (int) (price2-price1);
        return n;
    }
}

package com.alan.sort;

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

public class GoodsTest {

    public static void main(String[] args) {
        // 对商品对价格进行降序排列
        Goods goods1 = new Goods("goods001","手机",888.88);
        Goods goods2 = new Goods("goods002","手表",588.88);
        Goods goods3 = new Goods("goods003","电脑",1888.88);
        
        //将商品存放到ArrayList中
        List<Goods> goodsList = new ArrayList<Goods>();
        goodsList.add(goods1);
        goodsList.add(goods2);
        goodsList.add(goods3);
        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
上一篇下一篇

猜你喜欢

热点阅读