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
- Java中Collections的sort方法和Comparable与Comparator的比较
- java.util.Collections.sort(List list)类的应用(与Comparable,Comparator比较)
- java中Collections.sort(list)排序和Comparator和Comparable的区别
- Java中Comparable和Comparator的比较
- Java 中Collection和Collections 、Comparable 和Comparator的区别
- java Comparable 和 Comparator 的 Collections 排序
- Java Collections.sort和Arrays.sort使用comparator或者comparable接口的compare函数返回值
- Collections.sort()方法的使用及Comparable和comparator的qubie
- 【Java】Collections中sort方法Comparator的重写
- java 中Comparator和Comparable的区别和比较
- Java中常用的比较器Comparable与Comparator
- Java的比较接口 Comparable与Comparator
- Java中comparable和comparator接口的比较
- Collections的Comparable,Comparator
- Java中的Comparable和Comparator的比较
- Java 用Collections.sort()对对象集合排序之comparable接口和comparator比较器
- java.util.Collections.sort(List list)与Comparable,Comparator 接口
- java.util.Collections.sort(List list)与Comparable,Comparator
- sql基本操作1
- 练习5: 输入一个一维数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
- 读
- 一个简单的极光推送
- angular返回上一页
- Java中Collections的sort方法和Comparable与Comparator的比较
- 树莓派-硬件和功能-硬件和功能
- 纯前端实现文件下载功能
- java split方法使用注意事项
- 简单的时钟代码
- 滚动条的应用
- eclipse设置有颜色的主题背景
- How do I deploy a Flask application in IIS?
- 前端中的HTTP协议