【BigData】Java基础_通用排序工具类的实现

简介

实现一个通用排序工具类,根据用户所选择的排序对象进行排序;例如通过年龄排序、通过工资排序、或者工资相等的情况下使用年龄排序

代码实现

代码结构图:

 

 

package cn.test.logan.day08;

import java.util.ArrayList;

public class SortUtilTest {
    public static void main(String[] args) {
        User u1 = new User("001","张三",30,1000);
        User u2 = new User("002","李四",29,1200);
        User u3 = new User("003","王五",27,1000);
        User u4 = new User("004","狗蛋",24,1400);
        
        ArrayList<User> list = new ArrayList<>();
        list.add(u1);
        list.add(u2);
        list.add(u3);
        list.add(u4);
        
        // 根据年龄排序
        //SortUtil.sort(list, new AgeCompare());
        
        // 根据工资排序
        //SortUtil.sort(list, new SalaryCompare());
        
        //  先根据工资,工资相等则根据年龄
        SortUtil.sort(list, new SalaryAgeCompare());
        
        
        
        for(User u:list) {
            System.out.println(u);
        }
    }
}
SortUtilTest.java
package cn.test.logan.day08;

import java.util.ArrayList;

public class SortUtil {
    public  void sort(ArrayList<User> uList,Compare com) {
        for(int i=0;i<uList.size()-1;i++) {
            for(int j=0;j<uList.size()-1-i;j++) {
                // 判断大小
                boolean res = com.compare(uList.get(j), uList.get(j+1));
                if(res) {
                    User tmp = uList.get(j);
                    uList.set(j,uList.get(j+1));
                    uList.set(j+1, tmp);
                }
            }
        }
    }
}
SortUtil.java
package cn.test.logan.day08;

public interface Compare {
    public boolean compare(User u1,User u2);
}
Compare.java
package cn.test.logan.day08;
/**
 * 定义一个User类
 * 包含:ID,姓名,年龄,薪水
 * @author QIN
 *
 */
public class User {


    private String id;
    private String name;
    private int age;
    private float salary;
    
    public User() {
        
    }
    
    public User(String id, String name, int age, float salary) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    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 int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public float getSalary() {
        return salary;
    }

    public void setSalary(float salary) {
        this.salary = salary;
    }
    
    // 定义toString方法
    @Override
    public String toString() {
        return this.id + "," + this.name + "," + this.age + "," + this.salary;
    }
    
    
}
User.java

以下三个分别是对年龄、工资、先工资后年龄排序的类

package cn.test.logan.day08;

public class AgeCompare implements Compare{

    @Override
    public boolean compare(User u1, User u2) {
        if(u1.getAge() > u2.getAge()) {
            return true;
        }
        return false;
    }

}
AgeCompare .java
package cn.test.logan.day08;

public class SalaryCompare implements Compare{
    @Override
    public boolean compare(User u1, User u2) {
        if(u1.getSalary() > u2.getSalary()) {
            return true;
        }
        return false;
    }

}
SalaryCompare.java
package cn.test.logan.day08;

public class SalaryAgeCompare implements Compare{

    @Override
    public boolean compare(User u1, User u2) {
        // 先根据工资排序,如果工资相同再根据年龄排序
        if(u1.getSalary() > u2.getSalary()) {
            return true;
        }
        if(u1.getSalary() == u2.getSalary()) {
            if(u1.getAge() > u2.getAge() ) {
                return true;
            }
        }
        return false;
        
    }

}
SalaryAgeCompare.java

 第一次优化:使用泛型

在上面的代码中,很容易发现,如果我需要对一个商品类排序,那么就得修改SortUtil.java中的代码,这样是极其不方便的,所以此处使用泛型优化,让该排序程序更加适应不同类

以下是代码:(此处所有代码都在同一个目录下)

package cn.test.logan.day08;

import java.util.ArrayList;

public class SortUtilTest {
    public static void main(String[] args) {
        User u1 = new User("001","张三",30,1000);
        User u2 = new User("002","李四",29,1200);
        User u3 = new User("003","王五",27,1000);
        User u4 = new User("004","狗蛋",24,1400);
        
        // 快捷键ctrl + alt + 向下箭头可以复制上一行
        ArrayList<User> ulist = new ArrayList<>();
        ulist.add(u1);
        ulist.add(u2);
        ulist.add(u3);
        ulist.add(u4);
        
        // 构造排序工具
        SortUtil<User> sortutil = new SortUtil<>();
        
        // 根据用户类排序
        sortutil.sort(ulist, new AgeCompare());
        
        // 打印排序后结果
        for(User u:ulist){
            System.out.println(u);
        }
        System.out.println("---------------------------");
        Product pd1 = new Product("p001","苹果",10f,5);
        Product pd2 = new Product("p002","橘子",3f,2);
        Product pd3 = new Product("p003","西瓜",9f,1);
        Product pd4 = new Product("p004","火龙果",15f,8);
        
        ArrayList<Product> plist = new ArrayList<>();
        plist.add(pd1);
        plist.add(pd2);
        plist.add(pd3);
        plist.add(pd4);
        // 构造排序工具
        SortUtil<Product> psort = new SortUtil<>();
        psort.sort(plist, new PriceCompare());
        
        // 打印排序后结果
        for(Product p:plist){
            System.out.println(p);
        }
    }
}
SortUtilTest.java
package cn.test.logan.day08;
/*
 * SortUtil<T>里面的T是声明了一个代表“类型”的参数符号
 * 然后在SortUtil类中,就可以用T这个符号来代替具体的类型名,这就是所谓的泛型
 * 
 * 
 */
import java.util.ArrayList;

public class SortUtil<T> {
    public void sort(ArrayList<T> uList,Compare<T> com){
        for(int i=0;i<uList.size()-1;i++){
            for(int j=0;j<uList.size()-1-i;j++){
                boolean res = com.Compare(uList.get(j), uList.get(j+1));
                if(res){
                    T tmp=uList.get(j);
                    uList.set(j, uList.get(j+1));
                    uList.set(j+1, tmp);
                }
            }
        }
    }
}
SortUtil.java
package cn.test.logan.day08;

/**
 * 此处使用泛型,下面Compare中的对象类型M相当于一个变量
 * @author QIN
 *
 * @param <T>
 */
public interface Compare<M> {
    public boolean Compare(M o1,M o2);
}
Compare.java
package cn.test.logan.day08;

public class User {
    String id;
    String name;
    int age;
    float salary;
    
    // 生成空参构造函数
    //生成步骤:右键—>source—>Generate Constructors from Superclass
    public User() {
        super();
    }

    // 生成有参构造函数
    // 生成步骤:右键—>source—>Generate Constructors using Fields
    public User(String id, String name, int age, float salary) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    // get与set方法
    // 右键->source->Generate Getters and Setters
    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 int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public float getSalary() {
        return salary;
    }

    public void setSalary(float salary) {
        this.salary = salary;
    }
    
    // toString方法重写
    @Override
    public String toString() {
        return this.id  +","+ this.name  +","+ this.age  +","+ this.salary;
    }
    
}
User.java
package cn.test.logan.day08;
/**
 * 商品类:商品ID、商品名称、商品价格、商品数量
 * @author QIN
 *
 */
public class Product {
    String pId;
    String pName;
    float price;
    int num;
    
    // 空参构造函数
    public Product(){
        
    }
    // 有参构造函数
    public Product(String pId, String pName, float price, int num) {
        super();
        this.pId = pId;
        this.pName = pName;
        this.price = price;
        this.num = num;
    }
    
    // get与set方法
    public String getpId() {
        return pId;
    }
    public void setpId(String pId) {
        this.pId = pId;
    }
    public String getpName() {
        return pName;
    }
    public void setpName(String pName) {
        this.pName = pName;
    }
    public float getPrice() {
        return price;
    }
    public void setPrice(float price) {
        this.price = price;
    }
    public int getNum() {
        return num;
    }
    public void setNum(int num) {
        this.num = num;
    }
    // toString方法
    @Override
    public String toString() {
        return "Product [pId=" + pId + ", pName=" + pName + ", price=" + price + ", num=" + num + "]";
    }
    
}
Product.java

以下两个类是针对每个类需要排序字段而定制的

package cn.test.logan.day08;

public class AgeCompare implements Compare<User>{

    @Override
    public boolean Compare(User o1, User o2) {
        if(o1.getAge() > o2.getAge()){
            return true;
        }
        return false;
    }

    
}
AgeCompare.java
package cn.test.logan.day08;

public class PriceCompare implements Compare<Product> {

    @Override
    public boolean Compare(Product o1, Product o2) {
        if(o1.getPrice() > o2.getPrice()){
            return true;
        }
        return false;
    }

}
PriceCompare.java

第二次优化:匿名类的使用

在第一次优化后的代码中我们不难发现,同一个类中,需要对不同字段排序的时候,我们每次都要写一个实现类,这样感觉文件很多,很麻烦,于是在此处使用了匿名类

 

posted @ 2019-12-25 22:49  OLIVER_QIN  阅读(324)  评论(0编辑  收藏  举报