欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java排序和去重的几种常用方式

程序员文章站 2024-03-22 19:29:58
...

花了几天空闲功夫,亲自整理了一下,绝对实用!!

各位亲自跑一下,保证排序和去重就全懂了!(哪里不懂可以留言)

package com.hd.utility.vmda.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import usedByUtils.EmployeeEntity;
import usedByUtils.EmployeeImplComparableEntity;

public class ComparatorUtils {

    /**
     * 排序(基本数据类型):
     * 
     * 方式一
     * 
     * 默认方式
     */
    public static void ComparatorSortBase1(List<Integer> list) {
        System.out.println("原:" + list.toString() + System.getProperty("line.separator"));
        //1.直接排序,默认是正序
        Collections.sort(list);
        System.out.println("新:" + list.toString());

        Collections.reverse(list);//反转
        System.out.println("再反转:" + list.toString());

    }
    
    /**
     * 排序(基本数据类型):
     * 
     * 方式二
     * 
     * 顺序(正序或倒序)取决于“自定义实体类”中重写的compare()方法
     */
    public static void ComparatorSortBase2(List<Integer> list) {
        System.out.println("原:" + list.toString() + System.getProperty("line.separator"));
        //2.或者可以自定义顺序
        Collections.sort(list, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                int result = o2 - o1;// 倒序
                //int result = o1 - o2;// 正序
                return result;
            }
        });
        System.out.println("新:" + list.toString());
    }
    
    /**
     * 排序(根据“自定义实体类型”的某个属性):
     * 
     * 方式一
     * 
     * 顺序(正序或倒序)取决于“自定义实体类”中重写的compare()方法
     */
    public static void ComparatorSort1(List<EmployeeEntity> list) {
        System.out.println("原:" + list.toString() + System.getProperty("line.separator"));
        Collections.sort(list, new Comparator<EmployeeEntity>() {
            @Override
            public int compare(EmployeeEntity o1, EmployeeEntity o2) {
                int result = o2.getEname().compareTo(o1.getEname());// 倒序
                //int result = o1.getEname().compareTo(o2.getEname());// 正序
                return result;
            }
        });
        System.out.println("新:" + list.toString());
    }
    
    /**
     * 排序(根据“自定义实体类型”的某个属性):
     * 
     * 方式二
     * 
     * 实体类实现Comparable接口,并重写compare()方法
     * 顺序(正序或逆序)取决于“自定义实体类”中重写的compare()方法
     */
    public static void ComparatorSort2(List<EmployeeImplComparableEntity> list) {
        System.out.println("原:" + list.toString() + System.getProperty("line.separator"));
        Collections.sort(list);
        System.out.println("新:" + list.toString());
    }
    
    
    
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

    
    
    /**
     * 去重(基本数据类型):
     * 
     * 方式一
     * 
     * 根据set特性去重(3种set都可以)
     */
    public static void ComparatorRemoveDuplicateBase(List<Integer> list) {
        System.out.println("原:" + list.toString() + System.getProperty("line.separator"));
        //HashSet、TreeSet、LinkedHashSet都可以
        HashSet<Integer> set =new HashSet<Integer>(list);
        //或
        //HashSet<Integer> set =new HashSet<Integer>();
        //set.addAll(list);
        
        //再赋值到“原”list
        list.clear();
        list.addAll(set);
        
        //再赋值到“新”list
        List<Integer> listNew = new ArrayList<Integer>(set);
        
        System.out.println("去重(基本数据类型)set:\n" + set.toString());
        System.out.println("去重(基本数据类型)list:\n" + list.toString());
        System.out.println("去重(基本数据类型)listNew:\n" + listNew.toString());
    }
    
    /**
     * 去重并排序(根据“自定义实体类型”的某个属性):
     * 
     * 方式一
     * 
     * 根据set特性去重(3种set中,只有TreeSet提供了参数是Comparator的构造方法)
     * 构造方法参数是Comparator,重写compare()方法
     */
    public static void ComparatorRemoveDuplicate1(List<EmployeeEntity> list) {
        System.out.println("原:" + list.toString() + System.getProperty("line.separator"));
        
        //只有TreeSet可以//HashSet、LinkedHashSet都不行
        Set<EmployeeEntity> set =new TreeSet<>(new Comparator<EmployeeEntity>() {
            @Override
            public int compare(EmployeeEntity o1, EmployeeEntity o2) {
                //正序,将把所有员工按照自然顺序正序排列(这里是String,则按照ascii码升序排列,即字母顺序)
                return o1.getEname().compareTo(o2.getEname());
                //倒序
                //return o2.getEname().compareTo(o1.getEname());
            }
        });
        //list.clear();
        //list.addAll(set);
        
        set.addAll(list);
        
        System.out.println("去重(自定义实体类型)set:\n" + set.toString());
    }
    
    /**
     * 去重并排序(根据“自定义实体类型”的某个属性):
     * 
     * 方式二
     * 
     * 实体类实现Comparable接口,并重写compare()方法
     * 顺序(正序或逆序)取决于“自定义实体类”中重写的compare()方法
     */
    public static void ComparatorRemoveDuplicate2(List<EmployeeImplComparableEntity> list) {
        System.out.println("原:" + list.toString() + System.getProperty("line.separator"));
        
        Set<EmployeeImplComparableEntity> set =new TreeSet<>();
        //list.clear();
        //list.addAll(set);
        
        set.addAll(list);
        
        System.out.println("去重(自定义实体类型)LinkedHashSet:\n" + set.toString());
    }
    
    
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

    
    public static void main(String[] args) {

        List<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list.add(3);
        list.add(2);
        list.add(4);
        
        List<EmployeeEntity> empList = new ArrayList<EmployeeEntity>();
        EmployeeEntity emp1 = new EmployeeEntity(2,"Guan YunChang");
        EmployeeEntity emp2 = new EmployeeEntity(3,"Zhang Fei");
        EmployeeEntity emp3 = new EmployeeEntity(1,"Liu Bei");
        empList = Arrays.asList(emp1,emp2,emp3);
        
        List<EmployeeImplComparableEntity> empComparableList = new ArrayList<EmployeeImplComparableEntity>();
        EmployeeImplComparableEntity empC1 = new EmployeeImplComparableEntity(2,"Guan YunChang");
        EmployeeImplComparableEntity empC2 = new EmployeeImplComparableEntity(3,"Zhang Fei");
        EmployeeImplComparableEntity empC3 = new EmployeeImplComparableEntity(1,"Liu Bei");
        empComparableList = Arrays.asList(empC1,empC2,empC3);
        
        
        
        /**
         * 看这里!!
         * 
         * 释放下文对应的注释,即可查看对应的代码执行
         * 
         * 看这里!!
         */
        
        /*~~~~~~~~~~~~~~~~~排序~~~~~~~~~~~~~~~~~~*/
        
        //一、一
        //1.1.1排序(基本数据类型)//默认
        //ComparatorUtils.ComparatorSortBase1(list);
        //1.1.2排序(基本数据类型)
        //ComparatorUtils.ComparatorSortBase2(list);
        
        //一、二
        //1.2.1排序(根据“自定义实体类型”的某个属性)
        //ComparatorUtils.ComparatorSort1(empList);
        //1.2.2排序(根据“自定义实体类型”的某个属性)//实体类通过实现Comparable接口并重写compare方法来实现!!
        //ComparatorUtils.ComparatorSort2(empComparableList);
        
        /*~~~~~~~~~~~~~~~~~排序~~~~~~~~~~~~~~~~~~*/
        
        
        /*~~~~~~~~~~~~~~~~~去重~~~~~~~~~~~~~~~~~~*/
        
        //二、一
        //2.1.1去重(基本数据类型)
        //list.add(2);
        //ComparatorUtils.ComparatorRemoveDuplicateBase(list);
        
        //二、二
        //2.2.1去重并排序(根据“自定义实体类型”的某个属性)
        //EmployeeEntity emp4 = new EmployeeEntity(4,"Liu Bei");
        //List<EmployeeEntity> empListNew = new ArrayList<EmployeeEntity>(empList);//Arrays.asList生成的是不可变List
        //empListNew.add(emp4);
        //ComparatorUtils.ComparatorRemoveDuplicate1(empListNew);
        //2.2.2去重并排序(根据“自定义实体类型”的某个属性)//实体类通过实现Comparable接口并重写compare方法来实现!!
        //EmployeeImplComparableEntity empC4 = new EmployeeImplComparableEntity(4,"Liu Bei");
        //List<EmployeeImplComparableEntity> empListCNew = new ArrayList<EmployeeImplComparableEntity>(empComparableList);//Arrays.asList生成的是不可变List
        //empListCNew.add(empC4);
        //ComparatorUtils.ComparatorRemoveDuplicate2(empListCNew);
        
        /*~~~~~~~~~~~~~~~~~去重~~~~~~~~~~~~~~~~~~*/
    }

}

附上,两个实体类:

EmployeeEntity.java

package usedByUtils;

public class EmployeeEntity {
    private int empno;
    private String ename;

    public int getEmpno() {
        return empno;
    }

    public void setEmpno(int empno) {
        this.empno = empno;
    }

    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    public EmployeeEntity(int empno, String ename) {
            super();
            this.empno = empno;
            this.ename = ename;
        }

    @Override
    public String toString() {
        return  System.getProperty("line.separator") + "empno:" + empno + ";ename:" + ename + System.getProperty("line.separator");
    }
}

EmployeeImplComparableEntity.java

package usedByUtils;

public class EmployeeImplComparableEntity implements Comparable<EmployeeImplComparableEntity>{
    private int empno;
    private String ename;

    public int getEmpno() {
        return empno;
    }

    public void setEmpno(int empno) {
        this.empno = empno;
    }

    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    public EmployeeImplComparableEntity(int empno, String ename) {
            super();
            this.empno = empno;
            this.ename = ename;
        }

    @Override
    public String toString() {
        return  System.getProperty("line.separator") + "empno:" + empno + ";ename:" + ename + System.getProperty("line.separator");
    }

    @Override
    public int compareTo(EmployeeImplComparableEntity o) {
        //员工姓名逆序
        return o.getEname().compareTo(this.getEname());
    }
}

 

花了几天空闲功夫,辛苦整理了一下,绝对实用!亲自跑一下保证排序和去重就全懂了。

 

觉得实用的点个赞哈!!鼓励我继续趟路,哈哈哈哈哈哈~~~~~~

 

灵感来自于以下两篇文章:

https://blog.csdn.net/qq_27409289/article/details/85316107

https://www.cnblogs.com/yw0219/p/7222108.html?utm_source=itdadao&utm_medium=referral