打开APP
未登录
开通VIP,畅享免费电子书等14项超值服
开通VIP
首页
好书
留言交流
下载APP
联系客服
java.util.Comparator使用示例
WindySky
>《JAVA总结》
2008.09.28
关注
Comparator的具体实现类
public
class
AgeComparator
implements
Comparator
{
public
int
compare(Object op1, Object op2)
{
Employee eOp1
=
(Employee) op1;
Employee eOp2
=
(Employee) op2;
//
按年龄排序
return
eOp1.getAge()
-
(eOp2.getAge());
}
}
public
class
NameComparator
implements
Comparator
{
public
int
compare(Object op1, Object op2)
{
Employee eOp1
=
(Employee) op1;
Employee eOp2
=
(Employee) op2;
//
按姓名排序
return
eOp1.getName().compareTo(eOp2.getName());
}
}
public
class
SalaryComparator
implements
Comparator
{
public
int
compare(Object op1, Object op2)
{
Employee eOp1
=
(Employee) op1;
Employee eOp2
=
(Employee) op2;
//
按薪水排序
return
eOp1.getSalary()
-
(eOp2.getSalary());
}
}
Employee类:
public
class
Employee
{
protected
String name;
protected
int
age;
protected
int
salary;
public
Employee(String name,
int
age,
int
salary)
{
this
.name
=
name;
this
.age
=
age;
this
.salary
=
salary;
}
public
int
getAge()
{
return
age;
}
public
void
setAge(
int
age)
{
this
.age
=
age;
}
public
String getName()
{
return
name;
}
public
void
setName(String name)
{
this
.name
=
name;
}
public
int
getSalary()
{
return
salary;
}
public
void
setSalary(
int
salary)
{
this
.salary
=
salary;
}
}
测试:
public
class
Main
{
public
static
void
main(String[] args)
{
List
<
Employee
>
employees
=
new
ArrayList
<
Employee
>
();
employees.add(
new
Employee(
"
Andy
"
,
21
,
2000
));
employees.add(
new
Employee(
"
Felix
"
,
21
,
3000
));
employees.add(
new
Employee(
"
Bill
"
,
35
,
20000
));
employees.add(
new
Employee(
"
Helen
"
,
21
,
10000
));
employees.add(
new
Employee(
"
Cindy
"
,
28
,
8000
));
employees.add(
new
Employee(
"
Douglas
"
,
25
,
5000
));
//
按名称排序
Collections.sort(employees,
new
NameComparator());
display(employees);
//
按年龄排序
Collections.sort(employees,
new
AgeComparator());
display(employees);
//
按薪水排序
Collections.sort(employees,
new
SalaryComparator());
display(employees);
}
public
static
void
display(List
<
Employee
>
employees)
{
for
(Employee e:employees)
{
System.out.println(
"
雇员名=
"
+
e.getName()
+
"
年龄=
"
+
e.age
+
"
薪水=
"
+
e.getSalary());
}
System.out.println();
}
}
输出:
雇员名
=
Andy 年龄
=
21
薪水
=
2000
雇员名
=
Bill 年龄
=
35
薪水
=
20000
雇员名
=
Cindy 年龄
=
28
薪水
=
8000
雇员名
=
Douglas 年龄
=
25
薪水
=
5000
雇员名
=
Felix 年龄
=
21
薪水
=
3000
雇员名
=
Helen 年龄
=
21
薪水
=
10000
雇员名
=
Andy 年龄
=
21
薪水
=
2000
雇员名
=
Felix 年龄
=
21
薪水
=
3000
雇员名
=
Helen 年龄
=
21
薪水
=
10000
雇员名
=
Douglas 年龄
=
25
薪水
=
5000
雇员名
=
Cindy 年龄
=
28
薪水
=
8000
雇员名
=
Bill 年龄
=
35
薪水
=
20000
雇员名
=
Andy 年龄
=
21
薪水
=
2000
雇员名
=
Felix 年龄
=
21
薪水
=
3000
雇员名
=
Douglas 年龄
=
25
薪水
=
5000
雇员名
=
Cindy 年龄
=
28
薪水
=
8000
雇员名
=
Helen 年龄
=
21
薪水
=
10000
雇员名
=
Bill 年龄
=
35
薪水
=
20000
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报
。
打开APP,阅读全文并永久保存
查看更多类似文章
猜你喜欢
类似文章
【热】
打开小程序,算一算2024你的财运
牛客网数据库SQL实战剖析(11-20)
堆,栈,值类型,引用类型,装箱,拆箱 - worm128的日志 - 网易博客
Spring3:如何配置ContentNegotiatingViewResolver返回JSON或者XML格式数据? | 学步园
c# ArrayList 的排序问题! - 高浓度酱油的日志 - 网易博客
老龄化的日本:退休观念变淡,八九十岁仍在工作
Java 哈希表(google 公司的上机题)
更多类似文章 >>
生活服务
热点新闻
留言交流
回顶部
联系我们
分享
收藏
点击这里,查看已保存的文章
导长图
关注
一键复制
下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!
联系客服
微信登录中...
请勿关闭此页面
先别划走!
送你5元优惠券,购买VIP限时立减!
5
元
优惠券
优惠券还有
10:00
过期
马上使用
×