java实现List中对象排序的方法
作者:5iasp 时间:2023-11-08 21:36:29
本文实例讲述了java实现List中对象排序的方法。分享给大家供大家参考,具体如下:
package com.test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class NewsManager {
/**
* @param args
*/
public static void main(String[] args) {
List newss=getNewsList();
for(int i=0;i<newss.size();i++)
{
News news=(News)newss.get(i);
System.out.println("id:"+news.getId());
System.out.println("title:"+news.getTitle());
System.out.println("hits:"+news.getHits());
}
}
public static List getNewsList()
{
List list=new ArrayList();
News news1=new News();
news1.setHits(1);
news1.setId(1);
news1.setTitle("test1");
list.add(news1);
News news2=new News();
news2.setHits(7);
news2.setId(2);
news2.setTitle("test2");
list.add(news2);
News news3=new News();
news3.setHits(3);
news3.setId(3);
news3.setTitle("test3");
list.add(news3);
News news4=new News();
news4.setHits(5);
news4.setId(4);
news4.setTitle("test4");
list.add(news4);
// 按点击数倒序
Collections.sort(list, new Comparator<News>() {
public int compare(News arg0, News arg1) {
int hits0 = arg0.getHits();
int hits1 = arg1.getHits();
if (hits1 > hits0) {
return 1;
} else if (hits1 == hits0) {
return 0;
} else {
return -1;
}
}
});
return list;
}
}
希望本文所述对大家Java程序设计有所帮助。
标签:java,List,排序
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
基于Java实现缓存Cache的深入分析
2023-08-15 20:53:59
Spring Boot 项目发布到 Tomcat 服务器的操作步骤
2023-10-28 09:39:05
Android封装实现短信验证码的获取倒计时
2023-06-28 22:57:23
![](https://img.aspxhome.com/file/2023/9/82399_0s.gif)
浅谈SpringMVC+Spring3+Hibernate4开发环境搭建
2023-08-06 13:49:19
![](https://img.aspxhome.com/file/2023/6/58036_0s.png)
springboot嵌套子类使用方式—前端与后台开发的注意事项
2023-09-16 12:37:22
C#串口通信模块使用方法示例
2023-06-19 12:13:57
![](https://img.aspxhome.com/file/2023/7/66577_0s.jpg)
java.net.SocketException: Connection reset 解决方法
2023-06-17 01:28:43
![](https://img.aspxhome.com/file/2023/6/57416_0s.png)
Java在web页面上的编码解码处理及中文URL乱码解决
2023-08-25 11:10:19
![](https://img.aspxhome.com/file/2023/6/58206_0s.gif)
在Maven下代理服务器设定的方式
2023-10-15 02:17:13
![](https://img.aspxhome.com/file/2023/9/58569_0s.png)
Android仿新浪微博分页管理界面(3)
2023-08-04 19:14:02
![](https://img.aspxhome.com/file/2023/0/85450_0s.jpg)
Java使用IO流实现音频的剪切和拼接
2023-09-11 08:47:46
![](https://img.aspxhome.com/file/2023/9/58189_0s.jpg)
如何使用MybatisPlus快速进行增删改查详解
2023-11-03 06:58:13
![](https://img.aspxhome.com/file/2023/1/58741_0s.png)
IDEA不编译除了.java之外的文件的解决办法(推荐)
2023-09-16 15:50:44
![](https://img.aspxhome.com/file/2023/0/58570_0s.png)
Android 5秒学会使用手势解锁功能
2023-07-11 13:48:32
![](https://img.aspxhome.com/file/2023/1/83511_0s.gif)
android 6.0 写入SD卡的权限申请实例讲解
2023-07-27 03:12:37
SpringBoot集成Mybatis-plus并实现自动生成相关文件的示例代码
2023-08-15 04:12:46
java时间戳转日期格式的实现代码
2023-08-07 21:44:31
C++实现LeetCode(5.最长回文子串)
2023-07-03 06:07:13
Android下拉列表spinner的实例代码
2023-07-31 20:39:47
![](https://img.aspxhome.com/file/2023/2/86012_0s.jpg)
C语言/C++中如何产生随机数
2023-06-25 08:48:57
![](https://img.aspxhome.com/file/2023/2/103092_0s.jpg)