question_007-JAVA之对List的排序???

来源:互联网 发布:网络视频直播技术架构 编辑:程序博客网 时间:2024/05/17 06:56

question_007-JAVA之对List的排序


-------------------------------------------------------

对List的排序,需要使用到Collections中的sort()


············1················

T必须实现comparable接口

static <T extends Comparable<? super T>> void

sort(List<T> list)

Sorts the specified list into ascending order, according to thenatural ordering of its elements.


············2················

T不必实现comparable接口,但是必须指定比较器Comparator,然后实现比较器的compare(o1,o2)方法

static <T> void

sort(List<T> list,Comparator<? super T> c)

Sorts the specified list according to the order induced by the specified comparator.

-------------------------------------------------------

以下是两种方法:

1、list中对象实现Comparable接口
T必须实现comparable接口】
package com.ly;
public class Person implements Comparable<Person>{
private String name;
private Integer age;

public Person() {}
public Person(String name,Integer age) {
this.name=name;
this.age=age;
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public Integer getAge(){
return this.age;
}
public void setAge(Integer age){
this.age = age;
}
@Override // 此处实现了comparaTo接口
public int compareTo(Person o) {
returnthis.getAge().compareTo(o.getAge());
}
}


----main---------
package com.ly;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class PersonDemo {
public static void main(String[] args) {

Person p1 = new Person("三哥", 11);
Person p2 = new Person("四个", 15);
Person p3 = new Person("五个", 23);
Person p4 = new Person("六个", 9);

List<Person> lst1 = newArrayList<
Person>();

lst1.add(p1);
lst1.add(p2);
lst1.add(p3);
lst1.add(p4);

for(Person p: lst1){
System.out.println(p.getName()+" -- " + p.getAge());
}
System.out.println("------------");
Collections.sort(lst1);
for(Person p: lst1){
System.out.println(p.getName()+" -- " + p.getAge());
}
}
}
---------------------
三哥 -- 11
四个 -- 15
五个 -- 23
六个 -- 9
------------
六个 -- 9
三哥 -- 11
四个 -- 15
五个 -- 23
------------------------------




2、重载Collections中的sort()方法

T不必实现comparable接口,但是必须指定比较器Comparator,然后实现比较器的compare(o1,o2)方法

package com.lyp;

public class Student {
private String name;
private Integer age;

public Student() {
}

public Student(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}

--------main----------------

package com.lyp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


public class StudentDemo {
public static void main(String[] args) {

Student s1 = new Student("学1",90);
Student s2 = new Student("学2",23);
Student s3 = new Student("学3",30);
Student s4 = new Student("学4",40);
Student s5 = new Student("学5",50);

List<Student> lst1 = new ArrayList<Student>();

lst1.add(s1);
lst1.add(s2);
lst1.add(s3);
lst1.add(s4);
lst1.add(s5);

for(Student s : lst1){
System.out.println(s.getName()+"---"+s.getAge());
}
System.out.println("--------------");
Collections.sort(lst1, new Comparator<Student>() {

@Override // 在比较器中重写compare(o1,o2)方法。
public int compare(Student o1, Student o2) {
return o1.getAge().compareTo(o2.getAge());
}
});

for(Student s : lst1){
System.out.println(s.getName()+"---"+s.getAge());
}
}
}



-结果---------------


学1---90
学2---23
学3---30
学4---40
学5---50
--------------

学2---23
学3---30
学4---40
学5---50
学1---90


---------------------



0 0