java数据按时间分组_java list集合按日期分组

结果:

{

"code": 1,

"data": {

"mapDate": [

{

"fomTime": "2019-11-14",

"list": [

{

"id": 0,

"userId": 1,

"viewerId": 3,

"nickname": "用户3",

"headImage": "http://cdn.duitang.com/uploads/blog/201404/22/20140422142715_8GtUk.thumb.600_0.jpeg",

"isMaster": 0,

"code": null,

"pendantIcon": null,

"isFriend": 0,

"createTime": "2019-11-14T10:53:05",

"friendId": null,

"fomTime": "2019-11-14"

}

]

},

{

"fomTime": "2019-11-13",

"list": [

{

"id": 0,

"userId": 1,

"viewerId": 2,

"nickname": "用户2",

"headImage": "http://cdn.duitang.com/uploads/blog/201404/22/20140422142715_8GtUk.thumb.600_0.jpeg",

"isMaster": 0,

"code": 1000,

"pendantIcon": null,

"isFriend": 1,

"createTime": "2019-11-13T10:52:58",

"friendId": 1,

"fomTime": "2019-11-13"

}

]

},

{

"fomTime": "2019-11-12",

"list": [

{

"id": 0,

"userId": 1,

"viewerId": 4,

"nickname": "用户4",

"headImage": "http://cdn.duitang.com/uploads/blog/201404/22/20140422142715_8GtUk.thumb.600_0.jpeg",

"isMaster": 0,

"code": 1001,

"pendantIcon": null,

"isFriend": 1,

"createTime": "2019-11-12T10:53:44",

"friendId": 1,

"fomTime": "2019-11-12"

}

]

}

]

}

}

代码:

controller :

public AjaxResult selectViewer(Long userId){

Page page = new Page(1, 20); //无用

List mapDate = userByWatchService.selectViewer(userId, page);

Map map = new HashMap<>();

map.put("mapDate", mapDate);

return AjaxResult.success(map);

}

service :

public List selectViewer(Long userId, Page page) {

//按日期分组

Map> resultList=list.stream().collect(Collectors.groupingBy(UserByWatch::getFomTime));

Object[] keyArr=resultList.keySet().toArray(); //获取resultList的所有key值数组

Arrays.sort(keyArr);

List userByWatches=new ArrayList<>();

for(int i=keyArr.length-1;i>=0;i--){

UserByWatchVo userByWatchVo=new UserByWatchVo();

userByWatchVo.setFomTime((String)keyArr[i]);

userByWatchVo.setList(resultList.get(keyArr[i]));

userByWatches.add(userByWatchVo);

}

return userByWatches;

}

mapper:

List selectViewer(@Param("userId") Long userId, Page page);

mapper.xml:

SELECT

t_user_by_watch.user_id as userId,

t_user_data.id as viewerId,

t_user_data.nickname as nickname,

t_friend_user.friend_id as friendId,

t_user_by_watch.create_time as createTime,

date_format(t_user_by_watch.create_time,'%Y-%m-%d' ) as fomTime

FROM

t_user_by_watch

left JOIN t_user_data ON t_user_by_watch.viewer_id = t_user_data.id

left JOIN t_friend_user ON (t_user_data.id = t_friend_user.user_id and t_friend_user.friend_id=#{userId})

where t_user_by_watch.user_id=#{userId}

order by createTime desc

UserByWatchVo 类:

package com.mbyte.easy.admin.vo;

import com.mbyte.easy.admin.entity.UserByWatch;

import lombok.Data;

import java.util.List;

@Data

public class UserByWatchVo {

private String fomTime;

private Listlist;

}

UserByWatch 类:

package com.mbyte.easy.admin.entity;

import com.baomidou.mybatisplus.annotation.TableField;

import com.baomidou.mybatisplus.annotation.TableName;

import com.mbyte.easy.common.entity.BaseEntity;

import java.time.LocalDateTime;

import io.swagger.annotations.ApiModel;

import io.swagger.annotations.ApiModelProperty;

import lombok.Data;

import lombok.EqualsAndHashCode;

import lombok.experimental.Accessors;

@Data

@TableName("t_user_by_watch")

public class UserByWatch extends BaseEntity {

private static final long serialVersionUID = 1L;

/**

* 用户id

*/

@ApiModelProperty(value = "用户id",name = "userId")

private Long userId;

/**

* 观看者id

*/

@ApiModelProperty(value = "观看者id",name = "viewerId")

private Long viewerId;

/**

* 观看者昵称

*/

@ApiModelProperty(value = "观看者昵称",name = "nickname")

@TableField(exist = false)

private String nickname;

/**

* 创建时间

*/

@ApiModelProperty(value = "创建时间",name = "createTime")

private LocalDateTime createTime;

@TableField(exist = false)

private Long friendId;

/**

* 用于分类的时间

*/

@TableField(exist = false)

private String fomTime;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值