在java中对LIst集合的两种排序方法(即sort的使用方法)

List集合的排序:
java提供了两种排序方式,分别是Collections.sort(List)和Collections.sort(List,Commparator),下面就这两种方法的使用做详细的说明:


-Collections.sort(List);
sort的参数是一个List集合,对List集合中的数据排序,这个方法有两种情况.
第一种情况是:如果List集合中的元素内容只有一个数据,就直接比较即可,前提是保证这个数据对应的类中有CompareTo方法
例如:(//)

package www.lxk.day15.commparable;

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

public class CommparableClass {
    /**
     * 用Collections.sort(List)排序
     * list元素的类型是String类型
     * String 类型实现了Commparable接口,并重写了CompareTo方法
     * CompareTo方法中写的是比较原则
     */
    public void sortString(){
        ArrayList<String> strs=new ArrayList<String>();
        strs.add("123");
        strs.add("987");
        strs.add("abc");
        strs.add("ABC");

        System.out.println("---------------原始顺序,没有排序-------------");
        for(String str:strs){
            System.out.println(str);
        }
        System.out.println("----------------------------------------");
        Collections.sort(strs);
        System.out.println("--------------- 经过排序后输出   --------------");
        for(String str:strs){
            System.out.println(str);
        }

    }

}

如果List集合中的元素内容有多个数据,就需要这个元素类型必须实现Comparable接口,并重写CompareTo方法,在此方法中指定排序原则
例如:
Student类中重写了CompareTo方法,代码如下:

package www.lxk.day15.demo1;

import www.lxk.day15.demo1.CommonPerson;

public class Student extends CommonPerson implements Comparable<Student>{
    private String stuNo;

    public Student(){}

    public Student(String name,int age,String stuNo) {
        super(name,age);
        this.stuNo = stuNo;
    }

    public String getStuNo() {
        return stuNo;
    }

    public void setStuNo(String stuNo) {
        this.stuNo = stuNo;
    }

    @Override
    public void study() {
        System.out.println("Student.study()");
    }

    @Override
    public void eat() {
        System.out.println("Student.eat()");
    }
    public void exam(){
        System.out.println("Student.exam()");
    }

    @Override
    public int compareTo(Student o) {
        // TODO Auto-generated method stub
        //按照年龄排序
        int result1=this.getAge()-o.getAge();
        return result1;

    }

}

排序的代码如下:

package www.lxk.day15.commparable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CommparableClass {
/**
     * 用Collections.sort(List)排序
     * list元素的类型是Student类型
     * String 类型实现了Commparable接口,并重写了CompareTo方法
     * CompareTo方法中写的是比较原则
     */
    public void sortStudent(){
        ArrayList<Student> stus=new ArrayList<Student>();
        Student stu1=new Student("张三",20,"S001");
        Student stu2=new Student("李四",21,"S002");
        Student stu3=new Student("王五",22,"S003");
        Student stu4=new Student("张四",22,"S004");

        stus.add(0,stu1); 
        stus.add(1,stu2);
        stus.add(2,stu3);
        stus.add(3,stu4);

        System.out.println("---------------原始顺序,没有排序-------------");
        for(Student stu:stus){
            System.out.println("name="+stu.getName()+"age="+stu.getAge()+"stuNo="+stu.getStuNo());
        }
        System.out.println("----------------------------------------");
        Collections.sort(stus);
        System.out.println("--------------- 经过排序后输出   --------------");
        for(Student str:stus){
            System.out.println("name="+str.getName()+"age="+str.getAge()+"stuNo="+str.getStuNo());
        }
    }
    }

-Collections.sort(List,Commparator);
sort方法的参数有两个,一个是要排序的List集合,另一个参数是Comparator接口,在比较器中,指定要排序的原则,
使用比较器方式就不用对要比较的集合的类类型实现Comparable接口
可以实现多个比较器,每个比较器就是一种排序原则
例如:

package www.lxk.day15.commparator;

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

import www.lxk.day15.demo1.Student;

public class ComparatorClass {
    /**
     * 此方法用于获取一个List集合
     * @return
     */
    public List<Student> getStudents(){
        List<Student> stus=new ArrayList<Student>();
        Student stu1=new Student("张三",20,"S001");
        Student stu2=new Student("李四",21,"S002");
        Student stu3=new Student("王五",22,"S003");
        Student stu4=new Student("张四",22,"S004");

        stus.add(0,stu1);
        stus.add(1,stu2);
        stus.add(2,stu3);
        stus.add(3,stu4);
        return stus;
    }
    /**
     * 根据Comparator接口的子实现来指定排序的原则,策略模式
     * 按照姓名排序
     * @param stus
     */
    public void sortName(List<Student> stus){
        Collections.sort(stus, new Comparator<Student>(){

            @Override
            public int compare(Student stu1, Student stu2) {
                // TODO Auto-generated method stub

                return stu1.getName().compareTo(stu2.getName());
            }

        });
        for(Student stu:stus){
            System.out.println("name="+stu.getName()+"age="+stu.getAge()+"stuNo="+stu.getStuNo());
        }
    }
    /**
     * 根据Comparator接口的子实现来指定排序的原则,策略模式
     * 按照年龄排序
     * @param stus
     */
    public void sortAge(List<Student> stus){
        Collections.sort(stus, new Comparator<Student>(){

            @Override
            public int compare(Student stu1, Student stu2) {
                // TODO Auto-generated method stub
                return stu1.getAge()-stu2.getAge();
            }

        });
        for(Student stu:stus){
            System.out.println("name="+stu.getName()+"age="+stu.getAge()+"stuNo="+stu.getStuNo());
        }
    }
    /**
     * 根据Comparator接口的子实现来指定排序的原则,策略模式
     * 按照年龄排序
     * @param stus
     */
    public void sortstuNo(List<Student> stus){
        Collections.sort(stus, new Comparator<Student>(){

            @Override
            public int compare(Student stu1, Student stu2) {
                // TODO Auto-generated method stub
                return stu1.getStuNo().compareTo(stu2.getStuNo());
            }

        });
        for(Student stu:stus){
            System.out.println("name="+stu.getName()+"age="+stu.getAge()+"stuNo="+stu.getStuNo());
        }
    }
}

这其中还涉及到了策略设计模式,不过这并不是我们现在讨论的重点,就不做详细描述了.
**总结:如果就是一种比较原则,就用Commparable接口
如果有多个比较原则就用Comparator接口**

  • 14
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java集合,要对英文进行排序有多种方式。其一种常见的方法使用`Collections.sort()`方法List集合的元素进行排序。 首先,我们需要确保List集合已经包含了要排序的字符串元素。例如: ```java List<String> list = new ArrayList<>(); list.add("apple"); list.add("banana"); list.add("carrot"); list.add("dog"); list.add("elephant"); ``` 然后,我们可以使用`Collections.sort()`方法对这个List集合进行排序。例如: ```java Collections.sort(list); ``` 这将会根据英文字母的顺序对列表的元素进行排序排序完成后,我们可以使用循环遍历这个已经排序好的List集合,以查看排序结果。例如: ```java for (String element : list) { System.out.println(element); } ``` 输出结果将会是按照字母顺序排列的英文单词: ``` apple banana carrot dog elephant ``` 除了使用`Collections.sort()`方法外,还可以使用`TreeSet`或`TreeMap`这两个基于红黑树实现的集合类来对英文进行排序。这是因为红黑树会根据元素的自然顺序进行排序。例如,我们可以用`TreeSet`来对英文单词进行排序: ```java Set<String> set = new TreeSet<>(); set.add("apple"); set.add("banana"); set.add("carrot"); set.add("dog"); set.add("elephant"); ``` 使用`TreeSet`后,我们可以直接使用循环遍历来查看排序结果,无需再进行排序操作。例如: ```java for (String element : set) { System.out.println(element); } ``` 输出结果将会是按照字母顺序排列的英文单词。 综上所述,我们可以使用`Collections.sort()`方法List集合进行排序,也可以使用`TreeSet`或`TreeMap`类来直接对英文进行排序。这些都是Java集合常用的英文排序方法

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值