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<UserByWatch> page = new Page<UserByWatch>(1, 20); //无用
        List<UserByWatchVo> mapDate = userByWatchService.selectViewer(userId, page);
        Map<String, Object> map = new HashMap<>();
        map.put("mapDate", mapDate);
        return AjaxResult.success(map);
 }

service :

public List<UserByWatchVo> selectViewer(Long userId, Page<UserByWatch> page) {
//按日期分组
        Map<String,List<UserByWatch>> resultList=list.stream().collect(Collectors.groupingBy(UserByWatch::getFomTime));
        Object[] keyArr=resultList.keySet().toArray();  //获取resultList的所有key值数组
        Arrays.sort(keyArr);
        List<UserByWatchVo> 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<UserByWatch> selectViewer(@Param("userId") Long userId, Page<UserByWatch> page);

mapper.xml:

<select id="selectViewer" parameterType="java.lang.Long" resultType="com.mbyte.easy.admin.entity.UserByWatch">
        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
    </select>

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 List list;

}

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;


}

你可能感兴趣的:(java编程)