推荐换一批

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/WO8451401/article/details/81774837

1、可以通过集合的方法

List<GroovyRowResult> hotGroup = commonService.getGDao().query("SELECT * FROM sys_dict where pid in( select id from sys_dict where name = 'HOT_SEARCH_TAG')");//
if (hotGroup.size() < 0) {
    jsonObject.put("hotGroup", "搜索标签没有");
    return jsonObject;
}
Map mapSort = groupList(hotGroup, 10);

List<Integer> list = new ArrayList<>(mapSort.keySet());
//打乱顺序
Collections.shuffle(list);
for (Integer key : list) {
    if (key != Integer.valueOf(mapSortIndex) && list.size() > 1) {
        jsonObject.put("hotGroup", mapSort.get(key));
        jsonObject.put("mapSort", key);
        break;
    } else {
        jsonObject.put("hotGroup", mapSort.get(Integer.valueOf(mapSortIndex)));
        jsonObject.put("mapSort", Integer.valueOf(mapSortIndex));
    }
}

一个标示  和 一组数据

list 集合截取的方法

public Map groupList(List list, Integer size) {

    int listSize = list.size();
    int toIndex = size;
    Map map = new HashMap();     //用map存起来新的分组后数据
    int keyToken = 0;
    for (int i = 0; i < list.size(); i += size) {
        if (i + size > listSize) {        //作用为toIndex最后没有100条数据则剩余几条newList中就装几条
            toIndex = listSize - i;
        }
        List newList = list.subList(i, i + toIndex);
        map.put(keyToken, newList);
        keyToken++;
    }

    return map;
}

猜你喜欢

转载自blog.csdn.net/WO8451401/article/details/81774837
今日推荐