Collections.binarySearch用法

项目中的一个需求,APP留言列表需要锚点功能,点击留言要跳转到留言列表的具体位置,Collections工具类提供了二分法查找的实现

public class DirectGoodMessageVo extends JrdsGoodMessage implements Serializable,Comparable<DirectGoodMessageVo>{

    private Boolean sex;

    private String smallIcon;

    private String bigIcon;

    private String listType;

    private Integer start;

    private Integer end;

    private Long stationMessageId;

    public Long getStationMessageId() {
        return stationMessageId;
    }

    public void setStationMessageId(Long stationMessageId) {
        this.stationMessageId = stationMessageId;
    }

    public Integer getStart() {
        return start;
    }

    public void setStart(Integer start) {
        this.start = start;
    }

    public Integer getEnd() {
        return end;
    }

    public void setEnd(Integer end) {
        this.end = end;
    }

    public String getListType() {
        return listType;
    }

    public void setListType(String listType) {
        this.listType = listType;
    }

    public String getSmallIcon() {
        return smallIcon;
    }

    public void setSmallIcon(String smallIcon) {
        this.smallIcon = smallIcon;
    }

    public String getBigIcon() {
        return bigIcon;
    }

    public void setBigIcon(String bigIcon) {
        this.bigIcon = bigIcon;
    }

    public Boolean getSex() {
        return sex;
    }

    public void setSex(Boolean sex) {
        this.sex = sex;
    }

    /**
     * 序列化
     */
    private static final long serialVersionUID = -629484448752499648L;
    **//实现比较规则**
    public int compareTo(DirectGoodMessageVo o) {
        Long diff = this.getCreated().getTime() - o.getCreated().getTime();
        if(diff > 0)return -1;
        else if(diff < 0) return 1;
        else return 0;
    }
}
//查询对象所在的集合msgList 
List<DirectGoodMessageVo> msgList = directGoodMessageService.getDirectGoodMessageList(directGoodMessageVo);
//要查的对象
DirectGoodMessageVo message = directGoodMessageService.getMessageById(messageId);
            List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
            for (DirectGoodMessageVo dvo : msgList) {
                Map<String, Object> map = new HashMap<String, Object>();
                if (dvo.getInitiator() != null) {
                    map.put("msgFromUserName", dvo.getInitiator());
                }
                if (dvo.getReplyor() != null) {
                    map.put("msgToUserName", dvo.getReplyor());
                }
                if (dvo.getInitiatorId() != null) {
                    map.put("msgFromUserID", dvo.getInitiatorId());
                }
                if (dvo.getReplyorId() != null) {
                    map.put("msgToUserID", dvo.getReplyorId());
                }
                if (dvo.getMessage() != null) {
                    map.put("msgContent", dvo.getMessage());
                }
                if (dvo.getCreated() != null) {
                    map.put("msgTime", HandlerUtil.getShowTime(dvo.getCreated()));
                }
                map.put("icon", Commons.PIC_DOMAIN + dvo.getSmallIcon());
                if(userId != null)
                map.put("isMine", userId.equals(dvo.getInitiatorId()));
                map.put("msgID", dvo.getId());
                map.put("source", dvo.getSource());
                resultList.add(map);
            }
            //锚点下标
            int index = Collections.binarySearch(msgList, message);
            //返回锚点 
            responseBody.setAnchor(index + 1);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值