Java中Collections的sort方法和Comparable与Comparator的比较

来源:互联网 发布:速达软件好吗? 编辑:程序博客网 时间:2024/06/06 00:42

一.新建Student1类,类实现Comparable接口,并重写compareTo方法

二、Comparator



  //方式一        Collections.sort(list2, new MyComparator());        for (Student2 s : list2) {            System.out.println(s.getName() + "---" + s.getAge());        }
//逆序
//方式二:匿名类 Collections.sort(list2,
new Comparator<Student2>() { @Override public int compare(Student2 s1, Student2 s2) { int num = s2.getAge() - s1.getAge(); int num1 = (num == 0 ? s2.getName().compareTo(s1.getName()) : num); return num1; } });

1.

package sdutt;import java.util.ArrayList;import java.util.Collection;import java.util.Collections;import java.util.Comparator;import java.util.HashSet;import java.util.List;import java.util.Scanner;import java.util.Set;import java.util.TreeSet; class Student {    String id;String name;public int age;String sex;public Student(String id,String name,int age,String sex) {this.id=id;this.name=name;this.age=age;this.sex=sex;}@Overridepublic String toString() {return  id +" "+ name +" "+ age +" "+ sex;}@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result + age;result = prime * result + ((id == null) ? 0 : id.hashCode());result = prime * result + ((name == null) ? 0 : name.hashCode());result = prime * result + ((sex == null) ? 0 : sex.hashCode());return result;}@Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;Student other = (Student) obj;if (age != other.age)return false;if (id == null) {if (other.id != null)return false;} else if (!id.equals(other.id))return false;if (name == null) { if (other.name != null)return false;} else if (!name.equals(other.name))return false;if (sex == null) {if (other.sex != null)return false;} else if (!sex.equals(other.sex))return false;return true;}} public class Main {public static void main(String[] args) {Scanner sc=new Scanner(System.in);int n=sc.nextInt();Set<Student> set=new HashSet<Student>();while(n-->0) {Student stu=new Student(sc.next(),sc.next(),sc.nextInt(),sc.next());set.add(stu);       }List<Student> list=new ArrayList<Student>(set);Collections.sort(list,new Comparator<Student>() {@Overridepublic int compare(Student o1, Student o2) {// TODO Auto-generated method stubreturn o1.age-o2.age;}});System.out.println(list.size());for(Student s:list){System.out.println(s);}}}
2.
package sdutt;import java.util.ArrayList;import java.util.Collection;import java.util.Collections;import java.util.Comparator;import java.util.HashSet;import java.util.List;import java.util.Scanner;import java.util.Set;import java.util.TreeSet; class Student implements Comparable<Student> {    String id;String name;public int age;String sex;public Student(String id,String name,int age,String sex) {this.id=id;this.name=name;this.age=age;this.sex=sex;}@Overridepublic String toString() {return  id +" "+ name +" "+ age +" "+ sex;}@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result + age;result = prime * result + ((id == null) ? 0 : id.hashCode());result = prime * result + ((name == null) ? 0 : name.hashCode());result = prime * result + ((sex == null) ? 0 : sex.hashCode());return result;}@Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;Student other = (Student) obj;if (age != other.age)return false;if (id == null) {if (other.id != null)return false;} else if (!id.equals(other.id))return false;if (name == null) { if (other.name != null)return false;} else if (!name.equals(other.name))return false;if (sex == null) {if (other.sex != null)return false;} else if (!sex.equals(other.sex))return false;return true;}@Overridepublic int compareTo(Student o) {// TODO Auto-generated method stubreturn this.age-o.age;}} public class Main {public static void main(String[] args) {Scanner sc=new Scanner(System.in);int n=sc.nextInt();Set<Student> set=new HashSet<Student>();while(n-->0) {Student stu=new Student(sc.next(),sc.next(),sc.nextInt(),sc.next());set.add(stu);       }List<Student> list=new ArrayList<Student>(set);Collections.sort(list);System.out.println(list.size());for(Student s:list){System.out.println(s);}}}
3.

package sdutt;import java.util.ArrayList;import java.util.Collection;import java.util.Collections;import java.util.Comparator;import java.util.HashSet;import java.util.List;import java.util.Scanner;import java.util.Set;import java.util.TreeSet; class Student  {    String id;String name;public int age;String sex;public Student(String id,String name,int age,String sex) {this.id=id;this.name=name;this.age=age;this.sex=sex;}@Overridepublic String toString() {return  id +" "+ name +" "+ age +" "+ sex;}@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result + age;result = prime * result + ((id == null) ? 0 : id.hashCode());result = prime * result + ((name == null) ? 0 : name.hashCode());result = prime * result + ((sex == null) ? 0 : sex.hashCode());return result;}@Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;Student other = (Student) obj;if (age != other.age)return false;if (id == null) {if (other.id != null)return false;} else if (!id.equals(other.id))return false;if (name == null) { if (other.name != null)return false;} else if (!name.equals(other.name))return false;if (sex == null) {if (other.sex != null)return false;} else if (!sex.equals(other.sex))return false;return true;}} class myage implements Comparator<Student>{@Overridepublic int compare(Student o1, Student o2) {// TODO Auto-generated method stubreturn o1.age-o2.age;}  }public class Main {public static void main(String[] args) {Scanner sc=new Scanner(System.in);int n=sc.nextInt();Set<Student> set=new HashSet<Student>();while(n-->0) {Student stu=new Student(sc.next(),sc.next(),sc.nextInt(),sc.next());set.add(stu);       }List<Student> list=new ArrayList<Student>(set);Collections.sort(list,new myage());System.out.println(list.size());for(Student s:list){System.out.println(s);}}}



阅读全文
0 0
原创粉丝点击