Java基础-实现Comparator方式排序
2018-03-21 09:19
411 查看
当元素自身不具备比较性,或者具备的比较性不是所需要的。
这时需要让容器自身具备比较性。定义比较器,将比较器对象作为参数传递给TreeSet集合的构造函数。
当两种排序都存在时,以比较器为主。
定义一个类,实现Comparator接口,覆盖compare方法。
代码:import java.util.*;
public class code
{
public static void main(String[] args) {
TreeSet ts = new TreeSet(new MyCompare());//
ts.add(new Student("wangwu01", 21));
ts.add(new Student("wangwu03", 21));
ts.add(new Student("wangwu02", 21));
ts.add(new Student("wangwu04", 23));
ts.add(new Student("wangwu02", 24));
ts.add(new Student("wangwu03", 22));
Iterator it = ts.iterator();
while(it.hasNext()) {
Student s = (Student)it.next();
sop(s.getName()+"--"+s.getAge());
}
}
public static void sop(Object obj) {
System.out.println(obj);
}
}
class MyCompare implements Comparator<Object>
{
public int compare(Object o1,Object o2) {
Student s1 = (Student)o1;
Student s2 = (Student)o2;
int num = s1.getName().compareTo(s2.getName());
if(num == 0)
return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
// if(num == 0) {
// if(s1.getAge() > s2.getAge())
// return 1;
// if(s1.getAge() == s2.getAge())
// return 0;
// return -1;
// }
return num;
}
}
class Student implements Comparable//该接口强制让学生具备比较性
{
private String name;
private int age;
Student(String name,int age){
this.name=name;
this.age=age;
}
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;
}
@Override
public int compareTo(Object obj) {
if(!(obj instanceof Student))
throw new RuntimeException("不是学生对象");
Student s = (Student)obj;
if(this.age>s.age)
return 1;
if(this.age==s.age)
return this.name.compareTo(s.name);
return -1;
}
}
这时需要让容器自身具备比较性。定义比较器,将比较器对象作为参数传递给TreeSet集合的构造函数。
当两种排序都存在时,以比较器为主。
定义一个类,实现Comparator接口,覆盖compare方法。
代码:import java.util.*;
public class code
{
public static void main(String[] args) {
TreeSet ts = new TreeSet(new MyCompare());//
ts.add(new Student("wangwu01", 21));
ts.add(new Student("wangwu03", 21));
ts.add(new Student("wangwu02", 21));
ts.add(new Student("wangwu04", 23));
ts.add(new Student("wangwu02", 24));
ts.add(new Student("wangwu03", 22));
Iterator it = ts.iterator();
while(it.hasNext()) {
Student s = (Student)it.next();
sop(s.getName()+"--"+s.getAge());
}
}
public static void sop(Object obj) {
System.out.println(obj);
}
}
class MyCompare implements Comparator<Object>
{
public int compare(Object o1,Object o2) {
Student s1 = (Student)o1;
Student s2 = (Student)o2;
int num = s1.getName().compareTo(s2.getName());
if(num == 0)
return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
// if(num == 0) {
// if(s1.getAge() > s2.getAge())
// return 1;
// if(s1.getAge() == s2.getAge())
// return 0;
// return -1;
// }
return num;
}
}
class Student implements Comparable//该接口强制让学生具备比较性
{
private String name;
private int age;
Student(String name,int age){
this.name=name;
this.age=age;
}
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;
}
@Override
public int compareTo(Object obj) {
if(!(obj instanceof Student))
throw new RuntimeException("不是学生对象");
Student s = (Student)obj;
if(this.age>s.age)
return 1;
if(this.age==s.age)
return this.name.compareTo(s.name);
return -1;
}
}
相关文章推荐
- JAVA之旅(二十)—HashSet,自定义存储对象,TreeSet,二叉树,实现Comparator方式排序,TreeSet小练习
- JAVA之旅(二十)—HashSet,自定义存储对象,TreeSet,二叉树,实现Comparator方式排序,TreeSet小练习
- JAVA 利用Comparator实现自定义排序
- 120941138:java list sort()排序各种方式,实现接口
- 使用 java 的反射 和 comparator 实现java bean 的简单排序
- Java基础之一组有用的类——使用比较器对数组排序(TrySortingWithComparator)
- 排序基础算法汇总-java实现
- 最简单的对Java List列表按中文拼音排序的实现方式
- java实现各种基础排序
- 集合框架-二叉树-实现Comparator方式排序
- Java 基础小知识二:使用Comparator 对ArrayList 进行排序
- java基础知识回顾之java Thread类--java线程实现常见的两种方式(一)
- [基础回顾-JAVA类集框架篇] 之 集合排序Comparable与Comparator(上)
- java 数组比较,元素的比较,Comparable,Comparator比较的应用实现,排序,查找示例
- java实现各种基础排序(冒泡排序、快速排序、直接选择排序、堆排序、直接插入排序、归并排序)
- [基础回顾-JAVA类集框架篇] 之 集合排序Comparable与Comparator(下)
- JAVA基础之理解JNI原理(java方式实现dll实例)
- java中给map按照值value排序输出,用Comparator实现
- java 两种方式实现自定义排序
- 选择,插入,希尔,快速,堆,归并排序六种排序方式的Java 实现和性能对比(付代码)