List 集合排序
程序员文章站
2022-03-19 22:21:09
...
建立一个Student JavaBean 的这样的一个class. 并且生成Getter和Setter方法.
package pack.java.demo.test;
import java.util.Comparator;
public class Student implements Comparable<Student>{
public int id;
public String name;
public int age;
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return super.hashCode();
}
@Override
public String toString() {
// TODO Auto-generated method stub
return super.toString();
}
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
return super.equals(obj);
}
public int compareTo(Student o){
if(o == null){
return -1;
}
if (this == o)
return 0;
return this.name.compareTo(o.name);
}
}
//新建一个Teacher 类.并且生成Getter和Setter方法.
package pack.java.demo.test;
import java.util.Comparator;
public class Teacher implements Comparator<Teacher>{
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int 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 int compare(Teacher o1, Teacher o2) {
//return o1.getAge()>o2.getAge()?-1:1;
return o1.getName().equalsIgnoreCase(o2.getName())?1:-1;
}
public Teacher(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public Teacher() {
super();
// TODO Auto-generated constructor stub
}
}
//测试类;
package pack.y2.ssh.test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
new Test().listStudentSort();
new Test().listTeacher();
}
public void listStudentSort(){
Student stu1=new Student(1,"ZHT",20);
Student stu2=new Student(6,"AP",19);
Student stu3=new Student(3,"HF",12);
Student stu4=new Student(4,"BDF",18);
List<Student> list=new ArrayList<Student>();
list.add(stu1);
list.add(stu2);
list.add(stu3);
list.add(stu4);
Collections.sort(list);
for(Student s:list){
System.out.println(s.id+","+s.name);
}
}
public void listTeacher(){
List<Teacher> list=new ArrayList<Teacher>();
list.add(new Teacher(1,"VS",23));
list.add(new Teacher(3,"FS",83));
list.add(new Teacher(2,"AV",28));
Collections.sort(list, new Teacher());
for(Iterator<Teacher> it=list.iterator();it.hasNext();){
Teacher t=it.next();
System.out.println(t.getId()+","+t.getName()+","+t.getAge());
}
}
public void listSort(){
// TODO Auto-generated method stub
List list=new ArrayList();
list.add("D");
list.add("A");
list.add("C");
list.add("B");
Collections.sort(list);
Iterator<Object> it= list.iterator();
while (it.hasNext()) {
Object obj=it.next();
System.out.println(obj);
}
}
}
上一篇: SFTP服务器的文件管理