自己实现一个list比较器 实现Comparator()接口
/**
* @author lizhibiao
* @date 2018/11/27 17:21
*/
public class User
{
private String userName;
private int age;
private int cutScore;
public String getUserName()
{
return userName;
}
public void setUserName(String userName)
{
this.userName = userName;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public int getCutScore() {
return cutScore;
}
public void setCutScore(int cutScore) {
this.cutScore = cutScore;
}
}
public class UserComparator implements Comparator<User> {
@Override
public int compare(User o1, User o2)
{
if (o1.getAge() > o2.getAge())
{
return -1;
}
else if (o1.getAge() < o2.getAge())
{
return 1;
}
else if (o1.getCutScore() < o2.getCutScore())
{
return 1;
}
else if (o1.getCutScore() > o2.getCutScore())
{
return -1;
}
return 0;
}
}
这里注意:
if (o1.getAge() > o2.getAge())
{
return -1;
}
else if (o1.getAge() < o2.getAge())
{
return 1;
}
else if (o1.getCutScore() < o2.getCutScore())
{
return 1;
}
else if (o1.getCutScore() > o2.getCutScore())
{
return -1;
}
return 0;
public static void main(String[] args)
{
User user1 = new User();
user1.setUserName("小二");
user1.setAge(10);
user1.setCutScore(99);
User user2 = new User();
user2.setUserName("清水");
user2.setAge(12);
user2.setCutScore(98);
User user3 = new User();
user3.setUserName("小李");
user3.setAge(10);
user3.setCutScore(100);
List<User> list = new ArrayList<>();
list.add(user1);
list.add(user2);
list.add(user3);
Collections.sort(list, new UserComparator());
for (User user : list)
{
System.out.println(user.getUserName()+" "+user.getAge()+" "+user.getCutScore());
}
}


自己实现一个list比较器 实现Comparator()接口的更多相关文章
- Java 之 比较器( Comparator接口与 Comparable 接口)
一.定制排序:java.util.Comparator 接口 强行对某个对象 collection 进行整体排序 的比较函数.可以将 Comparator 传递给 sort 方法(如 Collecti ...
- Java TreeSet集合排序 && 定义一个类实现Comparator接口,覆盖compare方法 && 按照字符串长度排序
package TreeSetTest; import java.util.Iterator; import java.util.TreeSet; import javax.management.Ru ...
- Java中Comparable和Comparator接口区别分析
Java中Comparable和Comparator接口区别分析 来源:码农网 | 时间:2015-03-16 10:25:20 | 阅读数:8902 [导读] 本文要来详细分析一下Java中Comp ...
- Java.lang.Comparable接口和Java.util.Comparator接口的区别
Java的Comparator和Comparable当需要排序的集合或数组不是单纯的数字型时,通常可以使用Comparator或Comparable,以简单的方式实现对象排序或自定义排序. 1.Com ...
- java Comparable 和 Comparator接口区别
Comparable 简介 Comparable 是排序接口. 若一个类实现了Comparable接口,就意味着“该类支持排序”. 即然实现Comparable接口的类支持排序,假设现在存在“实现C ...
- JAVA Comparator 接口排序用法
java的比较器有两类,分别是Comparable接口和Comparator接口. 在为对象数组进行排序时,比较器的作用非常明显,首先来讲解Comparable接口. 让需要进行排序的对象实现Comp ...
- Comparable比较器和Comparator比较器
1.Comparable比较器 在Arrays类中存在sort()排序方法,此方法可以直接对对象数组进行排序. public static void sort(Object[] a 根据元素的自然顺序 ...
- 比较器Comparable Comparator
一. Comparable Comparable 是排序接口,若一个类实现了 Comparable 接口,就意味着该类支持排序.实现了Comparable 接口的类的对象的列表或者数组可以通过 Col ...
- 比较器:Compare接口与Comparator接口区别与理解
一.实现Compare接口与Comparator接口的类,都是为了对象实例数组排序的方便,因为可以直接调用 java.util.Arrays.sort(对象数组名称),可以自定义排序规则. 不同之处: ...
随机推荐
- Spring-data-jpa操作数据库环境配置
application.xml文件 <?xml version="1.0" encoding="UTF-8"?><beans xmlns=&q ...
- windows下遍历文件夹下的文件
#include <io.h>#include <stdio.h>#include <iostream>using namespace std;int ReadSt ...
- constant read 和 current read
来自网络,并且在本机实验完成: onsistent read :我的理解,就是通过scn来读取. 读取的过程中要保证 scn是一致的.举个例子,一个SELECT 语句在SCN=100的时刻开始读取一 ...
- 三大方面,分析 to B和 to C产品的区别
作为互联网从业者,我们经常听到to B(或2B)和to C(或2C)两个概念.to B即面向企业客户,to C即面向普通用户.只要是互联网人基本都懂知道这两个概念,但如果别人再问“to B和to C产 ...
- CF293E Close Vertices 点分治+树状数组
开始zz写了一个主席树,后来发现写个树状数组就行~ #include <cstdio> #include <vector> #include <algorithm> ...
- 51 Nod 1020 逆序排列
1020 逆序排列 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 收藏 关注 在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么 ...
- matplotlib绘图时显示额外的“figure”浮窗
引自 https://blog.csdn.net/weixin_41571493/article/details/82690052 问题:现在默认的Pycharm绘图时,都会出现下面的情况: 不能弹出 ...
- Android 一般动画animation和属性动画animator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- python爬取智联招聘职位信息(多进程)
测试了下,采用单进程爬取5000条数据大概需要22分钟,速度太慢了点.我们把脚本改进下,采用多进程. 首先获取所有要爬取的URL,在这里不建议使用集合,字典或列表的数据类型来保存这些URL,因为数据量 ...
- WordPress过滤器(Filters):apply_filters和add_filter
过滤器(Filters)对于WordPress来说是非常重要的,它极大地扩展了WordPress的定制能力,提高了WordPress的灵活性.无论是制作主题还是开发插件,我们基本上都会或多或少地使用到 ...