java获取list最大值_利用java如何实现获取List集合的最大值

利用java如何实现获取List集合的最大值

发布时间:2020-11-09 16:07:44

来源:亿速云

阅读:53

作者:Leah

利用java如何实现获取List集合的最大值?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

取List集合中最大的日期, 可以用Date max = Collections.max(dateList);, 传入一个日期集合, 就可以获取, 工作中有这个需求, 就查找到这个,

代码如下

} else {

/** 获取此专题下的所有内容的最新时间 */

Long featureId = this.communityFeatureMapper.selectFeatureIdByLabelId(labelId);

List communityFeatureRelationList = this.communityFeatureRelationMapper.selectByFeatureId(featureId);

List dateList = Lists.newArrayList();

for (CommunityFeatureRelation communityFeatureRelation : communityFeatureRelationList) {

CommunityProduct communityProduct =this.communityProductMapper.selectContentIdByProductIdAndType(communityFeatureRelation.getProductId(),BaseConstans.ARTICLE_YIFABU);

CommunityArticle communityArticle = this.communityArticleMapper.selectByPrimaryKey(communityProduct.getContentId());

dateList.add(communityArticle.getReleaseTime());

}

if (!CollectionUtils.isEmpty(dateList)) {

Date max = Collections.max(dateList);

/** 内容->添加专题-此专题下的合伙人getReleaseTime 更新, 若此专题下不存在合伙人, 则不更新 */

Long productId = this.communityProductMapper.selectIdByContentIdAndType(featureId, BaseConstans.FOUR);

CommunityPartner communityPartner = this.communityPartnerMapper.selectByPartnerId(productId);

if (!StringUtils.isEmpty(communityPartner)) {

communityPartner.setCreateTime(max);

communityPartnerMapper.updateByPrimaryKeySelective(communityPartner);

}

}

CommunityProduct communityProduct = this.communityProductMapper.selectContentIdByProductIdAndType(prodId, proType);

补充知识:java自定义List中的sort()排序方法,用于日期排序

1、问题描述

List是java中一个有序可重复的的集合,其中自带的.sort()排序方法,该方法在针对纯数字类型List集合排序的时候很有效。但是对于装入其他类型的List集合,自带的sort()方法排序我们很难控制,比如一个日期集合的排序。

2、解决方法:

java中List允许我们自定义sort()排序方法,以下自定义了List集合的sort排序方法,用于对一个字符串类型的日期集合进行排序。

//待排序的集合

List list=new ArrayList();

list.add("2019-06");

list.add("2019-11");

list.add("2019-02");

list.add("2019-09");

list.add("2019-05");

//自定义list排序,集合数据(月份)按升序排序;

final SimpleDateFormat sdft = new SimpleDateFormat("yyyy-MM");

Collections.sort(list, new Comparator(){

@Override

public int compare(String month2, String month3) {

int mark = 1;

try {

Date date1 = sdft.parse(month2);

Date date2 = sdft.parse(month3);

if(date1.getTime() < date2.getTime()){

mark = -1;//调整顺序,-1为不需要调整顺序;

}

if(month2.equals(month3)){

mark = 0;

}

} catch (ParseException e) {

LOG.error("日期转换异常", e);

e.printStackTrace();

}

return mark;

} //compare

});

3、其他

另外java两个日期类型的对象也可以用如下方法进行比较。

Date() date1=new Date();

Date() date2=new SimpleDateFormat("yyyy-MM-dd").parse("2019-06-11");

Boolean flag;

if(date1.before(date2)){

flag=true;

}

a.before(b);该方法是判断a日期是否小于b日期,返回的是一个布尔类型结果。

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值