通过Comparator接口实现比较
2016-07-11 19:42
316 查看
//通过Comparator接口实现比较
import java.util.* ;
class Student{ // 指定类型为Student
private String name ;
private int age ;
public Student(String name,int age){
this.name = name ;
this.age = age ;
}
public boolean equals(Object obj){
// 覆写equals方法
if(this==obj){
return true ;
}
if(!(obj instanceof Student)){
return false ;
}
Student stu = (Student) obj ;
if(stu.name.equals(this.name)&&stu.age==this.age){
return true ;
}else{
return false ;
}
}
public void setName(String name){
this.name = name ;
}
public void setAge(int age){
this.age = age ;
}
public String getName(){
return this.name ;
}
public int getAge(){
return this.age ;
}
public String toString(){
return name + "\t\t" + this.age ;
}
};
class StudentComparator implements Comparator<Student>{
// 实现比较器
// 因为Object类中本身已经有了equals()方法
public int compare(Student s1,Student s2){
if(s1.equals(s2)){
return 0 ;
}else if(s1.getAge()<s2.getAge()){
// 按年龄比较
return 1 ;
}else{
return -1 ;
}
}
};
public class Test{
public static void main(String args[]){
Student stu[] = {new Student("张三",20),
new Student("李四",22),new Student("王五",20),
new Student("赵六",20),new Student("孙七",22)} ;
java.util.Arrays.sort(stu,new StudentComparator()) ;
// 进行排序操作
for(int i=0;i<stu.length;i++){
// 循环输出数组中的内容
System.out.println(stu[i]) ;
}
}
};
运行结果:
孙七 22
李四 22
赵六 20
王五 20
张三 20
import java.util.* ;
class Student{ // 指定类型为Student
private String name ;
private int age ;
public Student(String name,int age){
this.name = name ;
this.age = age ;
}
public boolean equals(Object obj){
// 覆写equals方法
if(this==obj){
return true ;
}
if(!(obj instanceof Student)){
return false ;
}
Student stu = (Student) obj ;
if(stu.name.equals(this.name)&&stu.age==this.age){
return true ;
}else{
return false ;
}
}
public void setName(String name){
this.name = name ;
}
public void setAge(int age){
this.age = age ;
}
public String getName(){
return this.name ;
}
public int getAge(){
return this.age ;
}
public String toString(){
return name + "\t\t" + this.age ;
}
};
class StudentComparator implements Comparator<Student>{
// 实现比较器
// 因为Object类中本身已经有了equals()方法
public int compare(Student s1,Student s2){
if(s1.equals(s2)){
return 0 ;
}else if(s1.getAge()<s2.getAge()){
// 按年龄比较
return 1 ;
}else{
return -1 ;
}
}
};
public class Test{
public static void main(String args[]){
Student stu[] = {new Student("张三",20),
new Student("李四",22),new Student("王五",20),
new Student("赵六",20),new Student("孙七",22)} ;
java.util.Arrays.sort(stu,new StudentComparator()) ;
// 进行排序操作
for(int i=0;i<stu.length;i++){
// 循环输出数组中的内容
System.out.println(stu[i]) ;
}
}
};
运行结果:
孙七 22
李四 22
赵六 20
王五 20
张三 20
相关文章推荐
- java比较器comparable接口和comparator接口
- JAVA排序(二) Comparator接口
- java中的equals和hashCode方法以及两种方法集合的排序
- JAVA与c++的比较
- 浅析Java中的final关键字
- Java输入输出(IO)和流的基本概念
- iOS 实现扇形统计图
- AIDL 服务支持的数据类型
- ThinkPHP中initialize和construct的不同
- js与flash
- ValueError: unsupported format character 'Y' (0x59) at index 70
- Retrofit2.0使用和解析
- 常用插件总结
- Javascript实现客户端表单校验
- code4906 删数问题
- python常用函数
- (转载)String.IsNullorEmpty()方法的使用
- Android Gradle项目Hotfix热修复技术的接入
- 树莓派上3g模块的使用
- 8. String to Integer (atoi) (重要)