Collections.binarySearch用法

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

public class DirectGoodMessageVo extends JrdsGoodMessage implements Serializable,Comparable{

    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);

你可能感兴趣的:(app服务端开发)